BruteCode
BruteCode

Reputation: 1173

select and set multiple vars sql2008

How do I set multiple values before an insert statement? The below doesnt work.

declare @foo int
declare @bar int
set (select @foo=foo, @bar=bar from Foobar where id=123);

insert into ...
select @foo, 3, @bar

Upvotes: 1

Views: 51

Answers (2)

Michael Fredrickson
Michael Fredrickson

Reputation: 37398

You can assign the variables by using SELECT:

select @foo=foo, @bar=bar from Foobar where id=123;

Or, just skip the variables and combine the SELECT and INSERT:

insert into ...
select foo, bar
from Foobar
where id = 123;

Upvotes: 1

Manish Gupta
Manish Gupta

Reputation: 149

Use this -

declare @foo int
declare @bar int
select @foo=foo, @bar=bar from Foobar where id=123;

insert into ...
select @foo, 3, @bar

Upvotes: 1

Related Questions