We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,617 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

cursor for query passed as parameter

Hi I am trying to write a stored procedure which will execute a query and if resultset is not empty results will be inserted into a table passed as the second parameter to the procedure.

Below is what I wrote but I am having error with declaring the cursor, any help about the code will be very helpful.

Thanks,

Delimiter //
CREATE PROCEDURE evaluateSQL(select_sql TEXT, table_name TEXT)
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE query2 text;
DECLARE res text;
DECLARE cur1 CURSOR FOR select_sql;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cur1;
FETCH cur1 INTO res;
IF done THEN
SET @query2 = CONCAT("CREATE TABLE ", table_name, "(A1 VARCHAR(100))");
PREPARE stmt FROM @query2;
EXECUTE stmt;
insert_loop: LOOP
SET @query3 = CONCAT("INSERT INTO", table_name, "VALUES(", res,")");
PREPARE stmt1 from @query3;
execute stmt1
FETCH cur1 INTO res;
IF done THEN
LEAVE insert_loop;
END IF;
END LOOP insert_loop;
END IF;
CLOSE cur1;
END//
delimiter ;

1
Contributor
0
Replies
1
View
alenD
Newbie Poster
8 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0528 seconds using 2.91MB