Friday, February 19, 2016

SQL: cursor example

以下例子中,#missing是一個tmp table


DECLARE this_cursor CURSOR FOR 
select XX from #missing

OPEN this_cursor
DECLARE @cde integer
FETCH NEXT FROM this_cursor INTO @cde

WHILE @@FETCH_STATUS = 0
BEGIN
  -- do something here
  print @cde

  -- get next value
  FETCH NEXT FROM this_cursor INTO @cde
END

CLOSE this_cursor   
DEALLOCATE this_cursor

No comments: