快速提问

使用下面的代码,即时通讯能够使用sp从任何表中执行删除操作,但是我不知道如何使用concat来删除where语句。

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


对不起我的英语不好。

最佳答案

可以解决:

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

10-05 20:01