Reputation: 87
This statement works if goes with only 1 column in a table BUT if more, this statement gives error. Any idea why? Thanks
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)
Upvotes: 0
Views: 6664
Reputation: 69494
Try something like this
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
Upvotes: 1