View Full Version : Select


Pauldohert
01-16-2007, 08:18 AM
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
01-16-2007, 08:46 AM
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
01-16-2007, 08:54 AM
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

Pauldohert
01-17-2007, 03:54 AM
I went with this in the end

http://www.sommarskog.se/dyn-search.html

SQL_Hell
01-18-2007, 02:14 AM
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 :)

Pauldohert
01-18-2007, 02:17 AM
Yes got it sorted. Thanks.