| Author |
Topic |
|
jamie_pattison
Yak Posting Veteran
65 Posts |
Posted - 2012-04-19 : 14:41:10
|
| I have some SQL code that runs in query designer such asSELECT CustomerName FROM Customers WHERE CustomerName=@CustomerName(so a very simple query that gets the customer name from the customers table where the name is passed in)I need to create a stored proc where the query above needs to be in single quotes but i need to separate the query with the parameter so SQL knows the parameter is being passed in.I tried this'SELECT CustomerName FROM Customers WHERE CustomerName=' + @CustomerNamebut returned an error that the syntax is incorrect. How could i accomplish this?Please ask if this doesnt make senseThanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-19 : 14:43:46
|
| why do you need to make it dynamic? As per explained scenario, you dont need to use dynamic sql------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jamie_pattison
Yak Posting Veteran
65 Posts |
Posted - 2012-04-19 : 14:56:51
|
| It needs to be dynamic so i can send the output of the SP in an email..... since the parameter value can change it needs to be in this format? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-19 : 15:26:17
|
| then make it like'SELECT CustomerName FROM Customers WHERE CustomerName=''' + @CustomerName + ''''------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jamie_pattison
Yak Posting Veteran
65 Posts |
Posted - 2012-04-19 : 15:36:49
|
| Thanks but that gives the errorIncorrect syntax near '+'.Msg 105, Level 15, State 1, Procedure GetCust,Unclosed quotation mark after the character string ', |
 |
|
|
jamie_pattison
Yak Posting Veteran
65 Posts |
Posted - 2012-04-19 : 15:42:45
|
| Sorry i forgot to mention that the end of the above query has another clause i.e.'SELECT CustomerName FROM Customers WHERE CustomerName=' + @CustomerName 'AND Country=USA' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-04-19 : 15:48:42
|
| then it should be'SELECT CustomerName FROM Customers WHERE CustomerName=''' + @CustomerName ''' AND Country=USA'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
jamie_pattison
Yak Posting Veteran
65 Posts |
Posted - 2012-04-19 : 15:58:52
|
| Could i double check the quotes used are they all single quotes or double quote then single quote (or vice versa)? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
|