2017-04-05 99 views
1

我有一个临时表,其中有一列名为Id。如何使用最小的查询将100行插入此表中?将行插入到一个只有一列的临时表中

CREATE TABLE #Sample (
    id INT IDENTITY(1,1) 

+0

标签你用你的数据库问题使用。 –

+0

我已经标记了它。它的SQL服务器。 –

+0

另一个表格中有100行吗? – VDK

回答

1

这是棘手。强力方法是关闭标识插入:

set identity_insert #Sample on; 

with n as (
     select 1 as n union all 
     select n + 1 
     from n 
     where n + 1 <= 100 
    ) 
insert into #Sample(id) 
    select n.n 
    from n; 

set identity_insert #Sample off; 
0
declare @i int = 1 

while(@i<=100) 
begin 
insert into #Sample default values 
set @[email protected]+1 
end 

select * from #Sample 
0

你可以使用默认值,并使用批处理分隔符去执行INSERT 100倍

insert into #sample 
default values 
go 100 
相关问题