2017-04-05 51 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 
相關問題