如何在SQL Server 2008中使用case语句 [英] How do I use case statement in SQL server 2008
本文介绍了如何在SQL Server 2008中使用case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hii,
这是我想要的时候我想要的时候
@ var ='待定'然后字段为空
当@var ='Lotwise'然后field = field
当@ var ='已取消'然后Canflag(另一个字段)= 1
请给我解决方案。
我尝试过:
这是什么我做了
Hii,
This is my codition what i want
when @var='Pending' then field is null
when @var='Lotwise' then field=field
when @var='Canceled' then Canflag(another field)=1
Please give me solution for that.
What I have tried:
This what i did
Declare @status varchar(10);
set @status='Pending'
select EntryNo,entryDt,EntryLMBY,convert(date,EntryLMDT,106)EntryLMDT,LOtNo_Site2HO,Cancelflag from dbo.tbl_Activity
where
CASE @status
WHEN 'Pending' THEN LOtNo_Site2HO IS NULL
WHEN 'LotWise' THEN LOtNo_Site2HO=LOtNo_Site2HO
WHEN 'Canceld' THEN CANCELFLAG=1
end
推荐答案
你会试试这个
you shall try this
Declare @status varchar(10);
set @status='pending'
declare @query varchar(500)
declare @where varchar(100)
set @query = 'select EntryNo,entryDt,EntryLMBY,convert(date,EntryLMDT,106)EntryLMDT,LOtNo_Site2HO,Cancelflag from dbo.tbl_Activity where '
select @where = case @status
when 'pending' then ' LOtNo_Site2HO IS NULL '
WHEN 'LotWise' THEN ' LOtNo_Site2HO=LOtNo_Site2HO '
WHEN 'Canceld' THEN ' CANCELFLAG=1 '
else ' 1=1 '
end
set @query = @query + @where
print @query
--exec(@query)
这篇关于如何在SQL Server 2008中使用case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文