0

快速提问,

使用下面的代码,我可以使用 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

对不起我的英语不好。

4

1 回答 1

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
于 2013-05-15T17:27:04.473 回答