簡體   English   中英

在選擇語句創建的列上,列名無效

[英]Invalid column name, on select-statement created columns

我已將問題簡化為以下選擇語句。

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語句中獲取值的列。

有沒有一種方法可以執行上述操作而不會導致Invalid column name aVariable錯誤?

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

SELECT必須看起來像這樣:

select 
   u.UserId,
   1 as aVariable
from TblUser u

您選擇正確的陳述是沒有道理的。

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 userid, disabled as aVar
FROM TblUser
WHERE disabled = 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM