Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
in my query
if the input parameter is numeric i want to check one condition otherwise another condition.
for example

SQL
if(case 
when (isnumeric(@parameter)) then exist(select name from table where id=@paramter)
else
length(@parameter)>1
end)
Posted

1 solution

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:
SQL
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



Tested: SQL Server Express 2012
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900