Storing a string to a variable

Last Post 14 Feb 2006 10:41 AM by nosepicker. 1 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
werice
New Member
New Member

--
14 Feb 2006 09:51 AM
I have the following query!!!

declare @StrConTractType varchar(100)

Set @ContractType = 'CTFR'

if @StrContractType = 'CTFR'
set @StrContractType = ' ('ctf','ctr')'

I get the error (Line 9: Incorrect syntax near 'ctf')

I am sure it is because I have single quotes inside of closing single quotes.

Does anyone know how to set up a string that need single quotes inside of quotes and be able to store this string inside an @variable without getting the above error?

I am trying to do something like this:
select * from table where tableyear = year
and contracttypecode in @StrContractType

Thanks ahead of time
Bill

nosepicker
New Member
New Member

--
14 Feb 2006 10:41 AM
Well, first of all, this would be the correct syntax:

SET @StrContractType = ' (''ctf'',''ctr'')'

(Those are two single quotes next to each other, not double quotes)

Secondly, you're going to need to use dynamic SQL if you want to use this variable correctly:

DECLARE @sql varchar(500)

SET @sql = 'select * from table where tableyear = year
and contracttypecode in ' + @StrContractType

EXEC @sql


You are not authorized to post a reply.

Acceptable Use Policy