读书人

select into 不能给一个变量赋值吗?该

发布时间: 2012-01-15 22:57:48 作者: rapoo

select into 不能给一个变量赋值吗?
select into 只能把结果给一个临时表,不能给一个变量吗?
如果有这样需求,如何处理?

[解决办法]
是不是意思?

create table test
(
pk_id varchar(20)
)
insert into test select '290 '
Union All select '291 '

Declare @pk_id varchar(20)
Select @pk_id = pk_id From test Where pk_id = '291 '
Select @pk_id
GO
Drop Table test
--Result
/*
291
*/
[解决办法]
不能用表变量实现select into
用insert...select
declare @tb table
(
ID int identity,
name varchar(20)
)
insert @tb(name) select 'AAA '

select * from @tb
[解决办法]
不能(游标才用fetch ...into)
如果是单个值可以 select @a=cola from tablea
如果是数据集可以表变量
create table test(pk_id varchar(20))
insert into test select '290 '
insert into test select '290 '
insert into test select '290 '
insert into test select '290 '
insert into test select '290 '
insert into test select '290 '
declare @a table(a int)
insert @a select * from test
select * from @a

读书人网 >SQL Server

热点推荐