title: mysql stored procedure
body: <p>I have created a following stored procedure to insert rows in the table panel_user:</p>
  
  <pre><code>CREATE PROCEDURE panel_users()
  BEGIN
  DECLARE i INTEGER;
  DECLARE user_id BIGINT(20);
  DECLARE panel_id BIGINT(20);
  DECLARE wt DECIMAL(10,6);
  DECLARE level DECIMAL(6,2);
  DECLARE total INTEGER;
  SET i=1;
  SELECT COUNT(*) INTO total FROM smartmeter_usage.users_reporting;
  WHILE i&lt;=total DO
    SELECT u.userId INTO user_id, p.panelId INTO panel_id, ur.usage_percent INTO level, ipr.wt_base INTO wt
    FROM mondrainusage.user_d u 
    INNER JOIN smartmeter_usage.imei_phone_reporting ipr ON u.imeiNumber = ipr.IMEI 
    INNER JOIN smartmeter_usage.users_reporting ur ON ur.mobile_number = ipr.mobile_number
    INNER JOIN smartmeter_usage.panel pn ON pn.panel_id = ur.panel_id
    INNER JOIN mondrainusage.panel p ON p.panelName = pn.panel_name
    WHERE u.userId = i;
    INSERT INTO mondrainusage.panel_user (panelId, userId, userWeight, usageLevel)  VALUES(panel_id, user_id, wt, level);
    SET i = i+1;
  END WHILE
  END
  </code></pre>
  
  <p>I am getting following error:</p>
  
  <pre><code>Undeclared variable: p
  </code></pre>
  
  <p>I am not getting what is wrong here in my query. Is there a way out of it?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>k I got the solution for the above problem. here is the solution:</p>
  
  <pre><code>CREATE PROCEDURE panel_users()
  BEGIN
  DECLARE i INTEGER;
  DECLARE user_id BIGINT(20);
  DECLARE panel_id BIGINT(20);
  DECLARE wt DECIMAL(10,6);
  DECLARE level DECIMAL(6,2);
  DECLARE total INTEGER;
  SET i=1;
  SELECT COUNT(*) INTO total FROM smartmeter_usage.users_reporting;
  WHILE i&lt;=total DO
    SELECT u.userId, p.panelId, ur.usage_percent, ipr.wt_base INTO user_id,  panel_id,  level, wt
    FROM mondrainusage.user_d u 
    INNER JOIN smartmeter_usage.imei_phone_reporting ipr ON u.imeiNumber = ipr.IMEI 
    INNER JOIN smartmeter_usage.users_reporting ur ON ur.mobile_number = ipr.mobile_number
    INNER JOIN smartmeter_usage.panel pn ON pn.panel_id = ur.panel_id
    INNER JOIN mondrainusage.panel p ON p.panelName = pn.panel_name
    WHERE u.userId = i;
    INSERT INTO mondrainusage.panel_user (panelId, userId, userWeight, usageLevel) VALUES(panel_id, user_id, wt, level);
    SET i = i+1;
   END WHILE
  END
  </code></pre>
  
  <p>Bit now it is giving me syntax error near 'END' at line 21. </p>
  
  <p>Why is this so?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Got the solution:</p>
  
  <pre><code>END WHILE;
  </code></pre>