Msg 102,Level 15,State 1,Line 2','错误附近的语法不正确

时间:2013-12-28 13:34:42

标签: sql-server

如果表中只有1列,则此语句有效但是如果更多,则此语句会出错。知道为什么吗?感谢

DECLARE @TargetDB NVARCHAR(50)
DECLARE @SourceDB NVARCHAR(50)
DECLARE @InsetRecords NVARCHAR(1000)
SET @TargetDB = 'MySSISDb'
SET @SourceDB = 'MySSISDb'

SET @InsetRecords = 'INSERT INTO ' + @TargetDB + '.dbo.Item2(ProductNumber, ProductName) 
             SELECT(ProductNumber, ProductName) 
             FROM ' + @SourceDB + '.dbo.Item'
EXEC (@InsetRecords)

1 个答案:

答案 0 :(得分:1)

尝试这样的事情

DECLARE @TargetDB NVARCHAR(50)
DECLARE @SourceDB NVARCHAR(50)
DECLARE @InsetRecords NVARCHAR(MAX)
SET @TargetDB = N'MySSISDb'
SET @SourceDB = N'MySSISDb'

SET @InsetRecords = N'INSERT INTO ' + QUOTENAME(@TargetDB) + N'.[dbo].[Item2]([ProductNumber], [ProductName]) ' +
             N'SELECT [ProductNumber], [ProductName] ' + 
             N'FROM ' + QUOTENAME(@SourceDB) + N'.[dbo].[Item]'

EXECUTE sp_executesql @InsetRecords