将动态语句从SQL-Server转换为Mysql

时间:2012-06-05 19:40:43

标签: mysql sql-server

有人可以帮我找到一个与以下MS SQL语法相对应的mysql吗?

CREATE PROCEDURE testsp @COLUMN_NAME VARCHAR(20), @COLUMN_VALUE VARCHAR(20)
AS

DECLARE @SQL VARCHAR(40)  
SET @SQL = 'UPDATE TableX set ''+@COLUMN_NAME+ ''=''+@COLUMN_VALUE+'' '
PRINT (@SQL)
EXEC (@SQL)

是否可以将此代码翻译成Mysql?

谢谢!

1 个答案:

答案 0 :(得分:2)

CREATE PROCEDURE testsp (COLUMN_NAME VARCHAR(20), COLUMN_VALUE VARCHAR(20))
BEGIN
    SET @SQL_COMMAND = CONCAT('UPDATE TableX SET ', COLUMN_NAME, ' = ?');
    SELECT @SQL_COMMAND; /* prints the command */
    PREPARE SQL_STMT FROM @SQL_COMMAND;
    EXECUTE SQL_STMT USING COLUMN_VALUE;
    DEALLOCATE SQL_STMT;
END