프로그램/Mssql

MS-SQL 커서 활용법 Cursor

워누별 2024. 1. 23. 11:52
반응형

DECLARE @scode char(6)
DECLARE @i INT
SET @i=0
DECLARE Ncursor CURSOR FOR
select s_code from tb_student where s_code=학생코드

OPEN Ncursor
    FETCH NEXT FROM Ncursor INTO @scode

    WHILE @@fetch_status = 0
        BEGIN

delete from tb_nowtable1 where s_code=@scode
delete from tb_nowtable2 where s_code=@scode 
delete from tb_nowtable3 where mb_id=@scode 
delete from tb_nowtable4 where s_code=@scode 



            FETCH NEXT FROM Ncursor INTO @scode
SET @i=@i+1
        END
        CLOSE Ncursor
        DEALLOCATE Ncursor


select @i
select s_code from tb_student where s_code=학생코드




@@@@@@@@@@#  --두개의 조건값을 가지고 비교해야할 경우 @@@@@@@@@@@@############


DECLARE @scode char(6)
DECLARE @brcode char(4)
DECLARE @i INT
SET @i=0
DECLARE Ncursor CURSOR FOR
select s_code,br_code from 테이블

OPEN Ncursor
    FETCH NEXT FROM Ncursor INTO @scode, @brcode

    WHILE @@fetch_status = 0
        BEGIN

-- select @scode, @brcode
-- select * from 테이블 where s_code=@scode and br_code=@brcode

            FETCH NEXT FROM Ncursor INTO @scode, @brcode
SET @i=@i+1
        END
        CLOSE Ncursor
        DEALLOCATE Ncursor


select @i

반응형