Ronan Masangcay
Ronan Masangcay

Reputation: 43

Assign values to multiple variables from one select statement MS SQL

The code below works. Can this be improved?

declare @rlcstallname varchar(50), @rlcsalesdept varchar(50), @rlcpath varchar(50)

set @rlcstallname   = (select stallname from sometable)
set @rlcsalesdept   = (select salesdept from sometable)
set @rlcpath        = (select [path] from sometable)

I need to know how to get the three values I store into the variables using only one select statement; I find executing 3 select statements excessive.

Thanks

Upvotes: 4

Views: 5097

Answers (1)

Felix Pamittan
Felix Pamittan

Reputation: 31879

SELECT
    @rlcstallname = stallname,
    @rlcsalesdept = salesdept,
    @rlcpath = [path]
FROM sometable

Upvotes: 10

Related Questions