首页 > 代码库 > MySQL存储过程中实现执行动态SQL语句

MySQL存储过程中实现执行动态SQL语句

sql语句中的任何部分都可以作为参数。


DROP PROCEDURE if exists insertdata;

delimiter //
CREATE PROCEDURE insertdata(IN table_name varchar(255))
begin

declare var_sql varchar(800);
set var_sql = concat_ws(‘ ‘, ‘select count(*) from ‘, table_name,‘ where c_purchase_id is null‘);
set @sql = var_sql;
PREPARE s1 from @sql;
EXECUTE s1;
deallocate prepare s1;

end //
delimiter ;

call insertdata(‘table_name‘);

MySQL存储过程中实现执行动态SQL语句