Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 SELECT WHERE category= (multiple values)

Author  Topic 

dermotb
Starting Member

2 Posts

Posted - 2005-08-15 : 19:10:33
Hi,

My query is like this

SELECT * FROM books WHERE category = '1st value'
or category = '2nd value' or or category =
...and so on...

But can someone help to explain how I can use this where clause when I don't know beforehand how many values in my WHERE clause?

How do I dynamically generate the SQL.

Thanks a lot for looking.

D

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2005-08-15 : 19:15:30
http://www.sqlteam.com/item.asp?ItemID=11499

Tara
Go to Top of Page

dermotb
Starting Member

2 Posts

Posted - 2005-08-15 : 19:39:38
Thanks for the quick response.
Would you say that's the only way/best way
i.e. is there a simpler (pre-sqlserver2000) way to do it
or would that be a VBScript question?

Thanks,
Dermot
Go to Top of Page

Lumbago
Norsk Yak Master

3271 Posts

Posted - 2005-08-16 : 03:51:16
SELECT * FROM books WHERE category IN ('1st value', '2nd value', '3rd value')

--
Lumbago
"Real programmers don't document, if it was hard to write it should be hard to understand"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-08-16 : 04:02:36
>>SELECT * FROM books WHERE category IN ('1st value', '2nd value', '3rd value')

This is possible only if we know how many values to be included
One other possible way is to insert the values to other table called Category and write query

SELECT * FROM books B WHERE exists (select Category from Category where Category=B.Category)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -