Select (1 Viewer)

Pauldohert

Something in here
Local time
Today, 05:30
Joined
Apr 6, 2004
Messages
2,101
I am construction a string as the criteria to be usee in a select statement in a sp.

I can cinstruct the string fine - ie

@string = " Thisfield = 'hello'"

How do I then add this to the line

SELECT * FROM Table

something like SELECT * FROM Table where @string

Cheers
 

SQL_Hell

SQL Server DBA
Local time
Today, 13:30
Joined
Dec 4, 2003
Messages
1,360
Hi,

is it important for the filed name to be a variable? if not then



set @string = 'hello'"

SELECT * FROM Table where ThisField = @string
 

Pauldohert

Something in here
Local time
Today, 05:30
Joined
Apr 6, 2004
Messages
2,101
Thanks - I really want the field name as a variable.

I will let you know what I am doing -
there are5-8 possible values that are passed to the sp - if a value is passed I want to use it as criteria in the select statement.

Hence fieldname1 will sometimes be in my WHERE statement and sometimes not so the fieldnames and vlaues I want are variable.

I am open to other ways of doing this, but would like to do it this way too as a way of learning, if at all possible.

So could have

SELECT * FROM Table where ThisField = 'hello'
or
SELECT * FROM Table where ThisField = 'hello' AND thisfield2 = 'goodbye'

etc etc



Ta
 

SQL_Hell

SQL Server DBA
Local time
Today, 13:30
Joined
Dec 4, 2003
Messages
1,360
Hi there

Sorry I didn't get back to you quicker, I was going to suggest a case statement in a stored procedure or dynamic SQL in your front end

Glad you got it sorted anyway :)
 

Users who are viewing this thread

Top Bottom