全部,我有以下动态 SQL 查询
DECLARE @TableName NVARCHAR(255);
SET @TableName = 'BadCodesErrSumm';
DECLARE @DropSql NVARCHAR(MAX);
SET @DropSql =
'IF EXISTS (SELECT *
FROM Report.sys.objects
WHERE name = ''' + @TableName + ''' AND type = ''U'')
DROP TABLE [IPAReport]..[' + @TableName + '];'
PRINT @DropSql;
EXEC @DropSql;
这会产生以下错误
Msg 203, Level 16, State 2, Line 11
The name 'IF EXISTS (SELECT *
FROM Report.sys.objects
WHERE name = 'BadCodesErrSumm' AND type = 'U')
DROP TABLE [Report]..[BadCodesErrSumm];' is not a valid identifier.
但是,打印输出
IF EXISTS (SELECT *
FROM Report.sys.objects
WHERE name = 'BadCodesErrSumm' AND type = 'U')
DROP TABLE [Report]..[BadCodesErrSumm];
执行良好。我错过了什么?
谢谢你的时间。