views:

149

answers:

4

HI,

I have an insert statement in a Stored Procedure that requires the value data to be a combination of text and variables/inputs. However my SQL Manager is giving an error on this 'camp' value when trying to compile it.

Here is the statement as it currently exists;

   BEGIN TRAN
   INSERT INTO TBL_DONATIONS ([donations_MemberID], [donation_Ref], [donations_Debit]) VALUES (@tmp_Id, "camp"+@tmp_Id+"/Key"+@tmp_IDENTITY, @tmp_Pledge)
   COMMIT

I imagine its probably a case of me using the wrong format for concatenating the parts forming '"camp"+@tmp_Id+"/Key"+@tmp_IDENTITY' ???????

Any help greatly welcomed.

Thanks!

+1  A: 

You need to prepare the values before you stick them into the VALUES clause, or change it to an INSERT INTO ... SELECT ... query. The latter will be easier.

EDIT: And use single-quotes, not double-quotes.

Randolph Potter
Not sure what you mean by that. Please explain. Thanks.
Munklefish
INSERT INTO TBL_DONATIONS ([donations_MemberID], [donation_Ref], [donations_Debit]) SELECT @tmp_Id, 'camp' + @tmp_Id + '/Key' + @tmp_IDENTITY, @tmp_Pledge
Randolph Potter
Thanks for the extra detail, but why is it SELECT rather than VALUES ?
Munklefish
Just another way of doing it, that's all. I wrote that before I noticed you were using double-quotes, which explains the edit.
Randolph Potter
+4  A: 

I think you must use ' instead of ""

EDIT: If you want to "" for values you have to

SET QUOTED_IDENTIFIER OFF

SET QUOTED_IDENTIFIER

Svetlozar Angelov
this works: _print " this is a test " + 'of single quotes'_ However, you need to convert the numeric variables to strings before concatenating, just like @Yoav says.
KM
With what properties of your connection is this test done?
Svetlozar Angelov
default via SQL Server Management Studio
KM
I assume you have SET QUOTED_IDENTIFIER OFF, yes with it is valid
Svetlozar Angelov
however, when I open a new tab, the same command will fail, even with cut/paste of the code that actually ran. strange?
KM
yes it was "SET QUOTED_IDENTIFIER OFF"
KM
+2  A: 

Use single quotes ' not "

astander
+3  A: 

What's the exact error you're getting?

Without further information, you may need to cast the following variables, because the naming convention you use would lead me to believe they are integers, and you are concatenating them with strings:

  • @tmp_Id
  • @tmp_IDENTITY

Try this: 'camp' + CAST(@tmp_Id as varchar) + '/Key' + CAST(@tmp_IDENTITY as varchar)

Yoav
Yes indeed they are integers. I just assumed/hoped they would be implicitly convert in much the same way as in c# /.Net
Munklefish
+1 for noticing that he's using integers.
Randolph Potter