Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

mysql code stackoverflow

By: a guest on May 13th, 2014  |  syntax: MySQL  |  size: 0.43 KB  |  views: 162  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. DELIMITER //
  2. CREATE PROCEDURE filltable()
  3. BEGIN
  4.  
  5.         DECLARE n INT DEFAULT 0;
  6.         DECLARE i INT DEFAULT 0;
  7.         DECLARE var1 VARCHAR(40);
  8.         DECLARE var2 VARCHAR(40);
  9.  
  10.         SELECT COUNT(*) into n FROM mytable;
  11.         SET i=1;
  12.         WHILE i <= n DO
  13.                 SELECT name FROM mytable WHERE id=i INTO var1;
  14.  
  15.                 IF var1 IS NOT NULL THEN SET var2 = var1;
  16.                 ELSE UPDATE mytable SET name = var2 WHERE id = i;
  17.                
  18.                 SET i = i + 1;
  19.         END WHILE;
  20.  
  21. END//
  22. DELIMITER ;