Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have a stored procedure which takes in a list of IDs. I want to insert the rows for those IDs into another table.Here's my schema:---------------------------------------------tblEntries---------------------------------------------EntryID------------------------------------------------------------------------------------------tblRoundEntries---------------------------------------------RoundEntryID | RoundID | EntryID---------------------------------------------I'm trying to get rows from tblEntries and put them in tblRoundEntries. Here's my stored procedure:@EntryIDs nvarchar(800),@RoundID intASDECLARE @sqlstr varchar(8000) SET @sqlstr = 'INSERT INTO tblRoundEntries(RoundID, EntryID)SELECT ' + @RoundID + ', EntryIDFROM tblEntriesWHERE EntryID IN (' + @EntryIDs + ')' EXEC(@sqlstr)The error I'm getting says:Conversion failed when converting the varchar value 'INSERT INTO tblRoundEntries(RoundID, EntryID) SELECT ' to data type int.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2008-10-14 : 12:47:26
why use dynamic sql at all? use below
@EntryIDs nvarchar(800),@RoundID intASINSERT INTO tblRoundEntries(RoundID, EntryID)SELECT @RoundID, EntryIDFROM tblEntriesWHERE ','+@EntryIDs +',' LIKE '%,' + CAST(EntryID AS varchar(10)) + ',%'GO
madhivanan
Premature Yak Congratulator
22864 Posts
Posted - 2008-10-15 : 02:52:50
Print @sqlstrand see if it is valid statementMadhivananFailing to plan is Planning to fail