I have a stored procedure with a temp table where I am trying to insert values as shown below but when I do so I am getting an error, it seems that my single quotes are off, does anyone know the correct syntax that I need to do?
the error:
<ERROR>
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ',TestC)'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ',TestC)'.
</ERROR>
and the code that is causing the error:
DECLARE @DynamicINSERTSQL nvarchar(300)
DECLARE @VarA nvarchar(50)
DECLARE @VarB NVARCHAR(2)
DECLARE @VarC nvarchar(50)
--I am setting the variable values after I declare them
SET @VarA = 'Test A'
SET @VarB = 'Test B'
SET @VarC = 'Test C'
--this is the line below that errors out
SET @DynamicINSERTSQL = 'INSERT INTO #MyTempTable(ColumnA, ColumnB, ColumnC) VALUES (''' + @VarA + ''',' + @VarB + ''',' + @VarC + ')'
EXEC(@DynamicINSERTSQL)
MyronCope
Participant
1656 Points
1345 Posts
Getting error when I try to insert into temp table
Feb 24, 2012 12:51 PM|LINK
using sql server 2005
I have a stored procedure with a temp table where I am trying to insert values as shown below but when I do so I am getting an error, it seems that my single quotes are off, does anyone know the correct syntax that I need to do?
the error:
<ERROR>
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ',TestC)'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ',TestC)'.
</ERROR>
and the code that is causing the error:
DECLARE @DynamicINSERTSQL nvarchar(300) DECLARE @VarA nvarchar(50) DECLARE @VarB NVARCHAR(2) DECLARE @VarC nvarchar(50) --I am setting the variable values after I declare them SET @VarA = 'Test A' SET @VarB = 'Test B' SET @VarC = 'Test C' --this is the line below that errors out SET @DynamicINSERTSQL = 'INSERT INTO #MyTempTable(ColumnA, ColumnB, ColumnC) VALUES (''' + @VarA + ''',' + @VarB + ''',' + @VarC + ')' EXEC(@DynamicINSERTSQL)ignatandrei
All-Star
137716 Points
22159 Posts
Moderator
MVP
Re: Getting error when I try to insert into temp table
Feb 24, 2012 12:53 PM|LINK
make same with varb and varc
MyronCope
Participant
1656 Points
1345 Posts
Re: Getting error when I try to insert into temp table
Feb 24, 2012 12:56 PM|LINK
Thats it, thanks for your advice