我已将问题简化为以下select语句。
select
u.UserId,
aVariable = cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit),
from TblUser u
where aVariable = 1
aVariable不是表的列,而只是在此select语句中获取值的列。
有没有办法在没有得到的情况下完成上述操作 列名称无效aVariable错误?
答案 0 :(得分:3)
select q.* from (
select
u.UserId,
cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit) as aVar,
from TblUser u
)q
where q.aVar = 1
答案 1 :(得分:1)
SELECT
必须如此:
select
u.UserId,
1 as aVariable
from TblUser u
答案 2 :(得分:1)
你认为正确的陈述是没有意义的。
select q.* from (
select
u.UserId,
cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit) as aVar,
from TblUser u
)q
where q.aVar = 1
上述声明表示如果有一个用户被禁用,请从tbluser中选择所有用户。
我认为您希望看到表中的用户被禁用。如果是这样,那么您需要以下select语句:
SELECT userid, disabled as aVar
FROM TblUser
WHERE disabled = 1
给这一点。
之前的回答被删除,因为这个问题有点不清楚。
答案 3 :(得分:1)
你需要这样做:
select
u.UserId,
aVariable = cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit),
from TblUser u
where cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit) = 1