使用concat从存储过程中删除表

时间:2013-05-15 17:06:34

标签: mysql

快速提问,

使用下面的代码,我能够使用sp从任何表执行删除,但我不知道如何使用where语句删除concat。

CREATE DEFINER = `root`@`%` PROCEDURE `fn_del_t`(in t_name varchar(50), isrv char(50))
BEGIN
set @table_name = t_name;
set @iserver = isrv;
#  not working with where.
set @sql_text = concat('delete from ',@table_name, 'where iserver =',@iserver);
# ---- not working with where 
prepare stm from @sql_text;
execute stm;
DEALLOCATE prepare stm;
END;

call fn_del_t('the_table','localhost');

我收到的错误是:

[SQL] call fn_del_t('the_table','localhost');
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'iserver =localhost' at line 1

抱歉我的英文。

1 个答案:

答案 0 :(得分:0)

能够解决:

CREATE DEFINER = `root`@`%` PROCEDURE `fn_del_t`(in t_name varchar(50), isrv char(50))
BEGIN
set @table_name = t_name;
set @iserver = isrv;
#
set @sql_text = concat("delete from ",@table_name," where iserver ='",@iserver,"'");
prepare stm from @sql_text;
execute stm;
DEALLOCATE prepare stm;
END