简体   繁体   English

如何将表名传递给函数?

[英]how to pass table name into a function?

is there any method how to make MySQL function that receives table name or field name ? 有什么方法可以使MySQL函数接收表名或字段名?

something like this : 像这样的东西:

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

I tested it with string (SET @table_name="table_name"), but it doesn't works. 我用字符串(SET @ table_name =“ table_name”)测试了它,但是它不起作用。

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

and to make this work on MySQL (as the commenters pointed out) it should look like this: 并使其在MySQL上起作用(如评论者所指出的那样),它应如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 MySQL 中:如何将表名作为存储过程和/或函数参数传递? - In MySQL: How to pass a table name as stored procedure and/or function argument? 如何在gorm model中传递动态表名 - How to pass dynamic table name in gorm model 如何在查询中插入名称字段,但向表中传递该名称的相应 id - How to insert name filed in the query, but to the table to pass the corresponding id of that name 如何用动态表名写一个mysql函数? - How to write a mysql function with dynamic table name? 如何使用jdbc在mysql中动态传递表名? - how to pass a table name dynamically in mysql using jdbc? 如何在 mysql 的存储过程中将表名作为参数传递? - How to pass table name as argument in a stored procedure in mysql? 如何通过此查询将动态表名称传递到mySQL过程? - how to pass dynamic table name into mySQL Procedure with this query? 如何在 MySQL 触发器中传递动态表名和列名以保留在日志表中 - How to pass dynamic table name and column name to keep in log table in MySQL trigger 如何在MySQL中将动态列名传递给函数参数? - How to pass a Dynamic column name to a function parameter in MySql? 如何使用php将使用date()函数创建的变量传递给MYSQL表 - How to pass in a variable that was created with date() function to a MYSQL table with php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM