2013-11-23 106 views
1

我有一個要求將多個記錄插入表中存儲過程將返回插入的值。請考慮我的邏輯。如何在SQL Server 2008中編寫此查詢

查詢邏輯

foreach(var a in (select id from table1)) 
{ 
    insert into table2 values(a,DateTime.Now) 
} 

我需要與上述相同邏輯需要在SQL服務器來完成。對此解決方案的任何幫助將不勝感激。

感謝,

+1

'insert into table2(idColumn,dateColumn)select id,getdate()from table1' –

回答

1

我嘗試這樣做,運作良好。感謝@NikolaMarkovinović

insert into table2(idColumn, dateColumn) select id, getdate() from table1 
0

希望這有助於..

DECLARE @intFlag INT 
SET @intFlag = 1 
WHILE (@intFlag <=5) 
BEGIN 
PRINT @intFlag 
SET @intFlag = @intFlag + 1 
IF @intFlag = 4 
BREAK; 
END 
GO 
1
declare @a int=0 
while(@a<10) 
begin 
if(@a in (select id from table1)) 
begin 
    insert into table2 values(a,DateTime.Now) 
    set @[email protected]+1 
end 
end 
0
declare @a int = 0, @n int, @i int = 1 

select @n=COUNT(*) from table1 

while @i < @n 
begin 
insert into table2 values 
select x.a,Getdate() from 
(select ROW_NUMBER() over (order by [key]) as slno,* from table1) as x where x.slno = @i 
set @[email protected]+1; 
end