I am having a requirement in which i need to pass 6 sql statements to a procedure and it should return the
output of those 6 statements
I have written as below but it is only working for one sql statement as input
Begin set #c = inputSql;
prepare stmt from #c;
execute stmt;
deallocate prepare stmt;
end
Related
if I want to call a procedure with different input in each time input1,input2, input 3, ... and different output out1,out2,...and so on
proc(input1,out1) proc(input2,out2)...
can I made only one prepared statement inside a while loop to do that job? something like this
while c <= 10 DO
SET #sql = CONCAT('call proc(input',c,',out',c')');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
set c=c+1;
end while;
i am trying to write a stored procedure which updates the field of a table as specified in the argument .i am getting syntax error in #sql statment.please help
create procedure new_upd1(ind int(3),attribute varchar(30),pk int(11),new_value varchar(30))
begin
set #att=attribute;
set #primk=pk;
set #updated=new_value;
if ind=1 then
set #sql='update department set ?=? where departmentid=?';
prepare stmt from #sql;
execute stmt using #att,#updated,#primk;
deallocate prepare stmt;
end if;
end
Parameters don't work for column or table names, only for values. Do it like this:
create procedure new_upd1(ind int(3),attribute varchar(30),pk int(11),new_value varchar(30))
begin
set #att=attribute;
set #primk=pk;
set #updated=new_value;
if ind=1 then
set #sql=CONCAT('update department set ', #att, '=? where departmentid=?;');
prepare stmt from #sql;
execute stmt using #updated,#primk;
deallocate prepare stmt;
end if;
end
I have the following sccenario:
CREATE DEFINER=`test`#`%` PROCEDURE `prTest`()
BEGIN
SET #prepared_sql = 'select field1, field2 from table_data1');
PREPARE stmt FROM #prepared_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
I have to open the "stmt" in a loop. How do I read this loop and each row, getting the values of "field1" and "field2" and insert the values in another table?
Resolved:
CREATE DEFINER=`test`#`%` PROCEDURE `prTest`()
BEGIN
SET #prepared_sql = 'insert into table_data2
select field1, field2 from table_data1');
PREPARE stmt FROM #prepared_sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
I currently am looping through a list of userNames from the DB. as such :
DECLARE
cur1 CURSOR FOR
select user_name
from users
where user_type = 'SP'
and active = 'Y';
OPEN cur1;
read_loop : LOOP
FETCH cur1 INTO userName;
Now for each userName, i am creating tables with data. Obviously, within the above LOOP, i have multiple PREPARE statements like :
set l_table_name = concat("tmp_rec_",userName);
set l_select_cnt = concat("SELECT count(1) into #l_cnt
from information_schema.tables
where table_schema = 'greptlat_db'
and table_name = '", l_table_name, "'");
PREPARE stmt2 FROM #l_select_cnt;
EXECUTE stmt2;
DEALLOCATE PREPARE stmt2;
if l_cnt > 0 then
set droptable = concat("drop table ", l_table_name);
PREPARE stmt1 FROM #droptable;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
END IF;
I have more PREPARE statements that build the data that i want and inserts it into the table that i create for each user.
Now because this is in a LOOP, for each user, i have read that PREPARE statements are global. Which means, the above code, "PREPARE stmt1 FROM #droptable", stmt1 will never change even though it gors through the LOOP for each user. Even if i DEALLOCATE it, it still remains for that stored procedure.
How can i reset this stmt1,stmt2,stmt3 ...etc for each time the LOOP starts again ?
Basically l_table_name will change for each time the LOOP goes through, but stmt1 doesnt change.. I need stmt1 to change so that it will use the new l_table_name everytime
Normally a prepare statement deallocated implicitly before the new statement is prepared. So what is the result if you remove DEALLOCATE PREPARE stmt1;. Did you check the '#droptable' value in each loop?
I have a series of 16 PREPARE, EXECUTE and DEALLOCATE statements (in a stored procedure), each inserting rows into a diffterent table (table 1 to table16). eg:
SET #Command1 = CONCAT("insert into TABLE1" , ...etc.. );
PREPARE stmt1 FROM #Command1 ;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
SET #Command1 = CONCAT("insert into TABLE2" , ...etc.. );
PREPARE stmt1 FROM #Command1 ;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
.
.
.
SET #Command1 = CONCAT("insert into TABLE16" , ...etc.. );
PREPARE stmt1 FROM #Command1 ;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
When I execute the stored procedure, the INSERT works intermittently. Sometimes all the 16 inserts works, but sometimes they don't.
In the last CALL of the stored procedure, the first 2 inserts (into TABLE1 and TABLE2 ) and the last 4 inserts (TABLE 13 to 16) work, but not the inserts into Table 3 to 12.
Can you explain why? Can't be because I'm using the same variable/handle command1 and stmt1?
In MySQL, the combinations of Prepare and Execute commands should be used with placeholders - saving you time with concat strings (maybe one of your concats simply goes wrong) and of course - prevent sql injections (yey!)
So... just build your statements like this one:
-- in MySQL ? is a placeholder
set #sampleQuery = 'select name into #testValue from myTable where id = ?';
set #idParam = 'NT54X9';
prepare sampleStatement from #sampleQuery;
execute sampleStatement using #idParam; -- `using` is the key point here
deallocate prepare sampleStatement;
select #testValue