Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 ? : (conditional operator) Problem.

Author  Topic 

hdv212
Posting Yak Master

140 Posts

Posted - 2008-05-01 : 07:20:29
hi
i have this script sample :

declare @x nvarchar(30)
declare @a nvarchar(30)
set @x = 'Hi'
set @a = (@x ='Hi' ? @x:'GoodBye') -- Error Message
select @a


in line 4, i want to check value of @x variable, if it has 'Hi' value, then pass @x value to @a, otherwise pass 'GoodBye'
to @a. i want to do this checking by ? conditional operator, but sql server give me error :

Incorrect syntax near '='.


how to solve my problem ?
thanks.

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2008-05-01 : 07:41:18
set @a = case @x when 'Hi' then @x else 'GoodBye' end


Ryan Randall
Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-01 : 07:42:03
[code]declare @x nvarchar(30)
declare @a nvarchar(30)
set @x = 'Hi'
set @a = case when @x ='Hi' then @x else 'GoodBye' end
select @a[/code]
Go to Top of Page

hdv212
Posting Yak Master

140 Posts

Posted - 2008-05-01 : 12:34:22
thanks all
but how to run this query :

declare @a int
declare @b int
declare @c int
set @a = 30
set @b = 26
set @c = case (@a - @b) when >= 0 then (@a - @b) else -1 end
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-05-01 : 12:43:29
quote:
Originally posted by hdv212

thanks all
but how to run this query :

declare @a int
declare @b int
declare @c int
set @a = 30
set @b = 26
set @c = case when (@a - @b) >= 0 then (@a - @b) else -1 end

Go to Top of Page
   

- Advertisement -