Click here to Skip to main content
15,920,632 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
I'm a novice and I'm totally lost! I'm trying to create a stored procedure with MySQL 5.1 and I don't know where I'm going wrong. Can someone please tell me what's wrong here and what's the correct way to approach this. Here's an example of what I'm trying to do doing:
CREATE PROCEDURE proc_1()
BEGIN
DECLARE counter INT DEFAULT 1;
DECLARE maxRowCount INT;

SELECT COUNT(Column_1) FROM tbl_1 INTO maxRowCount;

WHILE counter < maxRowCount DO
SELECT Column_2 FROM tbl_1 WHERE id_PK = counter INTO @val;
SELECT COUNT(Column_3) FROM tbl_1 WHERE Column_2 = @val INTO @val2;
SELECT COUNT(Column_4) FROM tbl_1 WHERE Column_2 = @val INTO @val3;

UPDATE tbl_2 SET Col_1 = @val, Col_2 = @val2;

SET counter = counter + 1;
END WHILE;
END;
Posted
Updated 2-Dec-10 17:43pm
v2
Comments
wizardzz 3-Dec-10 10:13am    
What issue are you having? Is the stored procedure not being created, or is it not behaving as expected?
d.allen101 3-Dec-10 10:17am    
I ended up scrapping it, it's trash! It wasn't working all together, but thanks for the response Abhinav and your attempt to help me out!
rohit_189 30-Dec-10 1:16am    
here what i miss is a where condition in update statement. if you have more than one row in table

1 solution

HY
First

SELECT Column_2 FROM tbl_1 WHERE id_PK = counter INTO @val;

YOU must declare @val and INTO is before FROM

Ex:

SELECT * into #tmpTbl FROM tbl


Second

SELECT COUNT(Column_3) FROM tbl_1 WHERE Column_2 = @val INTO @val2;

You can say Column_2 = @val , @val is a table, you may say IN instead "="
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900