Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Using Declared Variables Expand / Collapse
Author
Message
Posted Monday, September 23, 2013 2:10 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, October 5, 2013 8:02 AM
Points: 2, Visits: 8
Hello all,

I am new to SQL coding. I would like to set my declared global variable like so. However when I do, it says that the column ratecode does not exist. This column is what I use in my "AS" statment. Is there a way to do this?

Declare @ratecodeVar varchar(15)

SELECT COALESCE(NULLIF(over_rate2,''), NULLIF(over_rate,''), std_rate) AS ratecode FROM dbo.matter WHERE clt_code = 'AME021' and mat_code = 'C133244'

SET @ratecodeVar = ratecode
Post #1497545
Posted Monday, September 23, 2013 2:52 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:00 AM
Points: 3,908, Visits: 8,863
That's because you can't use a value from a SELECT, outside the SELECT. To assign the value is easier than you might have thought.

Declare @ratecodeVar varchar(15)

SELECT @ratecodeVar = COALESCE(NULLIF(over_rate2,''), NULLIF(over_rate,''), std_rate)
FROM dbo.matter
WHERE clt_code = 'AME021'
AND mat_code = 'C133244'

By the way, there's no such thing as global variables (user-defined at least) in SQL Server. All of them are local variables.



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1497557
Posted Monday, September 23, 2013 2:54 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Saturday, October 5, 2013 8:02 AM
Points: 2, Visits: 8
Thanks!
Post #1497558
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse