Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a Case statement in a sql From clause

Is it possible to use a Case statement in a sql From clause using SQL 2005? For example, I'm trying something like:

SELECT Md5 FROM  CASE     WHEN @ClientType = 'Employee' THEN @Source = 'HR'     WHEN @ClientType = 'Member' THEN  @Source = 'Other' END CASE  WHERE Current = 2; 
like image 805
Zambian Avatar asked Mar 14 '09 17:03

Zambian


People also ask

Can you use case statement in order by clause?

Although it is most often used there, CASE is not limited to SELECT statements. For example, you can use it in clauses like IN , WHERE , HAVING , and ORDER BY . Using a CASE statement in a query once doesn't mean you have hit your quota for using it. You can use it multiple times in a single query.

Can we use case statement in FROM clause in Oracle?

No, you can't do that.

WHERE do you put CASE statements in SQL?

Using CASE statements with the ORDER BY clause SQL queries use the ORDER BY clause for data sorting in either ascending or descending order. You can use the CASE statements in conjunction with the ORDER BY clause. Suppose from the products table, we retrieve the [ProductName] and [ListPrice].


1 Answers

I don't believe that's possible. For one thing, query optimizers assume a specific list of table-like things in the FROM clause.

The most simple workaround that I can think of would be a UNION between the two tables:

SELECT  md5 FROM    hr WHERE   @clienttype = 'Employee' AND     current = 2 UNION SELECT  md5 FROM    other WHERE   @clienttype = 'Member' AND     current = 2; 

Only one half of the UNION could be True, given the @clienttype predicate.

like image 183
yukondude Avatar answered Sep 19 '22 11:09

yukondude