读书人

insert. select 时怎么对非自增长的

发布时间: 2012-01-06 22:55:18 作者: rapoo

insert.. select 时,如何对非自增长的列添加顺序值?
tb1
id int
name char(10)

tb2
name char(10)

SQL:
insert into tb1 (id,name) select IDENTITY(int, 1,1) id, name from tb2

以上的SQL无法执行,因为IDENTITY函数只能用在有into子句的Select中,有什么好的办法吗?

[解决办法]
可以做

Select IDENTITY(int, 1,1) As id, name Into #T From tb2
insert into tb1 (id,name) select id,name From #T

读书人网 >SQL Server

热点推荐