是否可以在 sql From 子句中使用 Case 语句 [英] Is it possible to use a Case statement in a sql From clause

查看:64
本文介绍了是否可以在 sql From 子句中使用 Case 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在使用 SQL 2005 的 sql From 子句中使用 Case 语句?例如,我正在尝试类似的事情:

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;

推荐答案

假设 SQL Server:

Assuming SQL Server:

您需要使用动态 SQL.构建字符串,然后使用字符串调用 sp_executesql.

You would need to use dynamic SQL. Build the string and then call sp_executesql with the string.

更好的是,只需使用 if 语句来执行适当的语句并将值分配给变量.如果可能,您应该避免使用动态 SQL.

Better yet, just use if statements to execute the appropriate statement and assign the value to a variable. You should avoid dynamic SQL if possible.

这篇关于是否可以在 sql From 子句中使用 Case 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆