以下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))
不确定这里发生了什么
答案 0 :(得分:2)
试试这个:
DECLARE @sSQL varchar(1000)
SET @sSQL = @Select + ' WHERE ' + REPLACE(REPLACE(@Where,'CRITERIA','TABLE_NAME'),'VALUE',@Table)
EXEC (@sSQL)
我相信EXEC()接受字符串变量或字符串常量,但不接受组合。