2013-02-19 88 views
1

如何在插入语句之前设置多个值?以下不起作用。选择并设置多个变量sql2008

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

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

回答

1

使用此 -

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

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

您可以通过使用SELECT分配变量:

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

或者,只跳过变量,并结合SELECT and INSERT

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