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;
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.
No, you can't do that.
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].
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With