如果表中只有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)
答案 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