0

以下 SQL 语句使用 PRINT 显示可靠的结果,但使用 EXEC,在“REPLACE”附近给出了不正确的语法

USE EPDB

DECLARE @Table as nvarchar(100)
DECLARE @Column as nvarchar(100)
DECLARE @Select as nvarchar(375)
DECLARE @Where as nvarchar(275)

SET @Table = 'TableABC'
SET @Column = 'ColumnABC'`enter code here`
SET @Select = 'SELECT * FROM INFORMATION_SCHEMA.COLUMNS'
SET @Where = 'CRITERIA = ''VALUE'''


-- EXEC ('SELECT * FROM INFORMATION_SCHEMA.COLUMNSWHERETABLE_NAME = ' + @Table + '''')

PRINT @Select + ' WHERE ' + REPLACE(REPLACE(@Where,'CRITERIA','TABLE_NAME'),'VALUE',@Table)

EXEC (@Select + ' WHERE ' + REPLACE(REPLACE(@Where,'CRITERIA','TABLE_NAME'),'VALUE',@Table))

不知道这里发生了什么

4

1 回答 1

2

尝试这个:

DECLARE @sSQL varchar(1000)

SET @sSQL = @Select + ' WHERE ' + REPLACE(REPLACE(@Where,'CRITERIA','TABLE_NAME'),'VALUE',@Table)

EXEC (@sSQL)

我相信 EXEC() 接受字符串变量或字符串常量,但不接受组合。

于 2013-10-08T20:32:08.867 回答