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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with passing variables into stored proc

Status
Not open for further replies.

Jacqui1811

Programmer
Jul 8, 2002
100
SG
I have an issue with the sql for a report and wondered if anbody could help.
I want to pass an entire where clause to a stored procedure to make filtering easy for the report.
So I will have @filter = " where name = 'myname' ",
exec sp_get name @filter

the stored proc reads.
sql = 'select * from table ' + filter + ' order by name'
sp_executesql @sql

the stored proc doesn't seem to like the fact that I have quote marks around 'myname' in @filter,I have tried with two single quotes as well by the way.
any hints ?
Thanks.
Jacqui.
 
That does not even need to by dynamic...

select *
from table
where name = @filter
order by name

Simi
 
It is a little more comples than it would appear.
It does need to be dynamic as I might be passing other values to be in the where clause so it might be.
where name = 'myname' and title = 'mr' and area= 'home'
or it might be just where name = 'myname'.
Or there might not even be a where clause.
If you can think of a better way of implementing that I would be very happy to hear of it :)
Appreciate all your time :)
Thanks.
Jacqui
 
Here would be 2 other ways...

declare @test varchar(100)
set @test = 'where code = ''B'' '
select @test

declare @test2 varchar(100)
set @test2 = 'where code = ' + char(39) +'B' +char(39)
select @test2

I personally like char(39) method better. It is clearer.

Simi
 
Thanks markros I am looking over that link :).
Regards
Jacqui.
 
Thanks Simian will have a try at both of those.
I will let both of you know what I have ended up implementing.
Thanks again you two.
Jacqui.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top