SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Using Declared Variables


Using Declared Variables

Author
Message
JKTower
JKTower
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 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
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16317 Visits: 19067
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.
General Disclaimer:
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?


How to post data/code on a forum to get the best help: Option 1 / Option 2
JKTower
JKTower
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 8
Thanks!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search