我有列的表稱爲樣本:如何將isnull應用於變量?
Id, Name, Dept, active
查詢:
select Id
from Sample
where Dept = @Dept and active = 1
我想通過傳遞deptment
稱的活躍來從樣品表名ID。可能會出現我獲得2條記錄的情況。兩個部門可能會積極。這就是爲什麼我取得前1名。有些時候可能沒有任何記錄。
這就是爲什麼我在存儲過程中使用這樣的:
declare @TempId int
set top 1 @TempId = Id
from Sample
where Dept = @Dept and active = 1
if(@TempId is null)
begin
@TempId = 0
end
我可以在上面使用isnull
選擇,而不是之後,既適合我的條件是什麼?
提示:'declare @TempId int = 0' –