如何将表名传递给函数?

时间:2011-06-22 16:33:24

标签: mysql sql

有没有任何方法可以使MySQL函数接收表名或字段名?

类似的东西:

CREATE PROCEDURE delete_row(the_id INT UNSIGNED , @table_name )  
BEGIN  
    IF ....... THEN
    BEGIN
        DELETE FROM @table_name WHERE id = the_id ;
        .............
    END
END

我用字符串(SET @ table_name =“table_name”)测试了它,但它不起作用。

1 个答案:

答案 0 :(得分:-1)

declare varchar(max) @mySQL
set @mySQL = 'DELETE FROM ' + @tablename + 'WHERE id = ' + Convert(varchar, @the_id)
sp_executeSQL @mySQL

并且使这项工作在MySQL上(正如评论者指出的那样)它应该如下所示:

mysql> prepare stmt from -> 'DELETE FROM ' + @tablename + 'WHERE id = ' + Convert(varchar, @the_id)
mysql> execute stmt;