如何在-else条件下使用case语句 [英] how to use case statement in if -else condition
本文介绍了如何在-else条件下使用case语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的查询中
如果输入参数是数字我想检查一个条件,否则另一个条件。
例如
in my query
if the input parameter is numeric i want to check one condition otherwise another condition.
for example
if(case
when (isnumeric(@parameter)) then exist(select name from table where id=@paramter)
else
length(@parameter)>1
end)
推荐答案
根据您在问题中显示的代码,它看起来不像CASE语句是一个不错的选择。 exists函数返回一个布尔值(True | False)。您正试图在When语句的Then子句中使用它。 Transact-SQL语法不允许这样做。
尝试这样的事情:
Based on the code that you showed in your question, it does not look like the CASE statement is a good choice. The "exists" function returns a Boolean value (True|False). You are trying to use it in the "Then" clause of a When statement. The Transact-SQL grammar does not allow that.
Try something like this:
declare @parameter varchar(10);
declare @x int;
if isnumeric(@parameter)=1
begin
if exists(select name from [table] where id=@parameter)
Begin
Select @x=1
End
Else
Begin
Select @x=2
End
End
Else
Begin
if len(@parameter)>1
Begin
Select @x=3
End
Else
Begin
Select @x=4
End
End
经测试:SQL Server Express 2012
这篇关于如何在-else条件下使用case语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文