Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Query using quotes and variables ...

Status
Not open for further replies.

jfield817

Programmer
Jun 2, 2000
153
US
Using Sql Server 7 ...
Unable to get this code fragment to work ...
@z is Null when I display it ...Ive tried different
approaches to "set @fcount = 1" with no success ....

declare @z char(300)
declare @list char(30)
declare @fcount char(1)
set @list = "(10,99999,999999)"
set @z = "if exists(select * from titles where royalty in "+@list + ") set " + @fcount + "=1"
exec (@z)

Any help would be appreciated ..
Thank u
john
 
Try this one;


declare @z char(300)
declare @list char(30)
set @list = '(10,99999,999999)'
set @z = 'if exists(select * from titles where royalty in '+@list +') declare @fcount char(1) set @fcount =1'

Print @z -- Optional

exec (@z)

Select @fcount -- Optional

Muhammad Essa Mughal
Software Engineer
essamughal@yahoo.com
 
Essa ...

I tried the exact same code u posted ..
and I get 'variable @fcount must be declared'
so I declared it outside the string ...
when i print the string I get null ....

I have to use the IN operator and I don't see
another way to do this ....

Any more ideas ...

Thank u very much
John

 
Here is some code I used for a similar problem that worked for me


declare @ParameterStr varchar(500)
set @ParameterStr ='in("product1", "product2", "product3")'
--set @ParameterStr ='like "%"'
Declare @sql varchar(8000)

set @sql='SELECT Product FROM Products WHERE Product '+ @ParameterStr

exec (@sql)

I would suggest before you execute statement you print the sql you are trying to execute, it may well be more obvious then what is not quite right


Andy

--Note: if your parameter is a date, you must put
--the date parameter in triple single quotes when
--using it in your select statement string; i.e.,
-- '''+@Date+'''

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top