user3337941
user3337941

Reputation: 123

How to assign alias to variable declared in SQL

I have declared one variable in SQL Server:

declare @InsuredRaceEthnicId int

And assigning value to it like this:

select @InsuredRaceEthnicId = MAX(InsuredRaceEthnicId) 
from dbo.InsuredRaceEthnic with (nolock)

It's giving me value with no column name but I need a column name.

Upvotes: 1

Views: 2437

Answers (1)

Hart CO
Hart CO

Reputation: 34774

You can alias a variable the same way you'd alias a column:

DECLARE @test VARCHAR(50) = '1222'
SELECT @test AS Test

Your SELECT statement shouldn't be returning anything, it's just setting the value of the variable, you could subsequently SELECT/PRINT the variable if so desired.

Upvotes: 1

Related Questions