比如
insert into table1 (id,name1)
select top 1000 ?,name2 from table2
要求结果是:
talbe1:
id name1
1 x
2 x
3 x
. x
. x
. x
1000 x
?号部分的SQL如何写?或有什么别的SQL语句可以完成上述功能?(id不是标识种子,也没有标识递增量)
多谢各位高人
5 个解决方案
#1
select ID=identity(int,1,1),name2 into #t from table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
#2
上面的再改一改
select top 1000 ID=identity(int,1,1),name2 into #t from table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
select top 1000 ID=identity(int,1,1),name2 into #t from table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
#3
如果table2有唯一字段a,并已它排序:
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
否则就只能用临时表
select top 1000 IDENTITY(int,1,1) as id,name2
into #t
from Table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
否则就只能用临时表
select top 1000 IDENTITY(int,1,1) as id,name2
into #t
from Table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
#4
還有一種方法插入後處理:
declare @num int
set @num=0
update table
set @num=id=@num+1 --從1遞增
declare @num int
set @num=0
update table
set @num=id=@num+1 --從1遞增
#5
请问 8992026(8992026)
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
中x.a的x表是哪个表?
谢谢各位回答
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
中x.a的x表是哪个表?
谢谢各位回答
#1
select ID=identity(int,1,1),name2 into #t from table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
#2
上面的再改一改
select top 1000 ID=identity(int,1,1),name2 into #t from table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
select top 1000 ID=identity(int,1,1),name2 into #t from table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
#3
如果table2有唯一字段a,并已它排序:
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
否则就只能用临时表
select top 1000 IDENTITY(int,1,1) as id,name2
into #t
from Table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
否则就只能用临时表
select top 1000 IDENTITY(int,1,1) as id,name2
into #t
from Table2
insert into table1 (id,name1)
select id,name2 from #t
drop table #t
#4
還有一種方法插入後處理:
declare @num int
set @num=0
update table
set @num=id=@num+1 --從1遞增
declare @num int
set @num=0
update table
set @num=id=@num+1 --從1遞增
#5
请问 8992026(8992026)
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
中x.a的x表是哪个表?
谢谢各位回答
insert into table1 (id,name1)
select top 1000 (select count(*) from table2 where a<=x.a) as id,name2 from table2 order by a
中x.a的x表是哪个表?
谢谢各位回答