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)
 CASE statement possible usage

Author  Topic 

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2006-02-14 : 16:41:11
Is it possible to use a case statement similar to this to return from the required select?

CASE
WHEN LEN(@P) > 0 THEN SELECT * FROM MyTab WHERE cust_code = @P
WHEN LEN(@Nm) > 0 THEN SELECT *FROM MyTab WHERE name = @Nm
ELSE
SELECT * FROM MyTab
END

I have tried various forms of passing this case statement to a select statement but just get incorrect syntax

Any ideas?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-14 : 17:01:51
No that is not possible. Please post an explanation of what you are trying to do.

Tara Kizer
aka tduggan
Go to Top of Page

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2006-02-14 : 17:10:16
I have a table with all the formated data in it. The final process is to return to the user the contents of this table based on the filter they pass. So if @P > 0 return filtered by @P if @Nm > o then return filtered by @Nm.

Guess I am looking at multiple IF statements then?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2006-02-14 : 17:11:19
If you are looking to implement a dynamic WHERE clause, then you'll need to use COALESCE function. Here is the article that explains it:
http://www.sqlteam.com/item.asp?ItemID=2077

Tara Kizer
aka tduggan
Go to Top of Page

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2006-02-14 : 17:25:31
Sad it takes me so much brain power to grasp such things, but although I do not fully understand how it works and can see how it will resolve my problem, and help me revamp current procedures I have written.

Thnx :)
Go to Top of Page

hog
Constraint Violating Yak Guru

284 Posts

Posted - 2006-02-14 : 19:15:22
RESULT!

I understand it now and it works perfect :) THNX.....
Go to Top of Page
   

- Advertisement -