【问题标题】:What is use of execute('select * from tablename') in Store Procedure, MS SQL存储过程中执行('select * from tablename')的用途,MS SQL
【发布时间】:2013-01-23 18:21:51
【问题描述】:

我正在尝试弄清楚

的含义
execute('SELECT customerID [CustomerID],customerCode[Customer Code],
firstName +char(32)+ surname AS [Name],street [Street],phone [Phone],*  
FROM tblCustomers  WHERE customerCode like '''+ @custCode +'%''')

在存储过程中。 我试图用谷歌搜索这么多,但无法找到,所以最后我来这里寻求帮助。 因为,我正在将 SP 从 MS SQL 转换为 MySQL,我如何在 MySQL 中翻译它? Execute(' select * from tablename') 和 'select * from tablename' 有什么区别吗?

谢谢

【问题讨论】:

  • ...内联 sql。 sql server 执行命令会将参数编译为 sql 语句并返回结果集。类似于 execsql。 IMO 这不是最佳做法。你是对的,这与在 MySQL 中发出查询文本相同。

标签: mysql sql sql-server stored-procedures


【解决方案1】:
SET @sql = CONCAT(' SELECT      *,
                            customerID,
                            customerCode `Customer Code`,
                            CONCAT(firstName, ' ', surname) AS `Name`,
                            street `Street`,
                            phone `Phone` 
                    FROM tblCustomers  
                    WHERE customerCode like ''', @custCode ,'%''');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

或参数化它,

SET @sql = '    SELECT      *,
                        customerID,
                        customerCode `Customer Code`,
                        CONCAT(firstName, ' ', surname) AS `Name`,
                        street `Street`,
                        phone `Phone` 
                FROM tblCustomers  
                WHERE customerCode LIKE CONCAT(?, ''%'')';

PREPARE stmt FROM @sql;
EXECUTE stmt USING @custCode;
DEALLOCATE PREPARE stmt;

【讨论】:

    猜你喜欢
    • 2015-06-18
    • 1970-01-01
    • 2014-01-27
    • 1970-01-01
    • 2012-04-13
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多