MSSQL

declare 여러개로 선언하여 사용하기

littlemk 2021. 2. 26. 09:13
declare @cursor1_status   --hold the first fetch status
declare @cursor1_holder
declare @cursor2_status   --hold the second fetch status
declare @cursor2_holder

--declare cursors
declare first_cursor cursor for....
declare second_cursor cursor for....

--open cursors
open first_cursor
open second_cursor

--fetch cursors and use another variable to hold the fetch -- status
fetch next from first_cursor into @cursor1_holder
select @cursor1_status = @@fetch_status

fetch next from second_cursor into @cursor2_holder
select @cursor2_status = @@fetch_status

while @cursor1_status = 0     --while cursor still has data
begin
 .......
  while @cursor2_status = 0
  begin
  ..........
    fetch next from second_cursor into @cursor2_holder
    select @cursor2_status = @@fetch_status
  end

  fetch next from first_cursor into @cursor1_holder
  select @cursor1_status = @@fetch_status

  --go back to first record of second_cursor to start
  --loop again
  fetch first from second_cursor into @cursor2_holder
  select @cursor2_status = @@fetch_status

end

close and deallocate cursors

'MSSQL' 카테고리의 다른 글

MSSQL Trigger 트리거 조회하기  (0) 2021.02.03
MSSQL 테이블 정보 조회  (0) 2021.01.12