Bogart
Bogart

Reputation: 87

Msg 102, Level 15, State 1, Line 2 Incorrect syntax near ',' Error

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

Answers (1)

M.Ali
M.Ali

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

Related Questions