cursor and insert with select in mssql

from:
http://www.thescripts.com/forum/thread78561.html

You can do this, but in general you should avoid cursors unless there
is no other choice. It may be that your cursor can be written as a
single insert statement, in which case it will be a lot faster and
easier to code. See below for a rough example.

Simon

/* With a cursor */

declare @var1 int, @var2 int

declare cur cursor fast_forward for
select col1, col2
from dbo.table1

open cur

fetch cur into @var1, @var2

while @@fetch_status = 0
begin
if @var1 > @var2 -- check conditions
begin
insert into dbo.table2 (col1, col2)
select @var1, @var2
end

fetch cur into @var1, @var2
end

close cur
deallocate cur

/* With an insert */

insert into dbo.table2 (col1, col2)
select col1, col2
from dbo.table1
where col1 > col2

posted @ 2008-01-12 02:26  N/A2011  阅读(236)  评论(0编辑  收藏  举报