Sqlserver 커서 복습

2741 단어 sqlserver
저장 프로세스를 자주 쓰지만 오늘은 커서 사용 과정에서 일부 일을 소홀히 해서 실행 과정에서 계속 실행하지 못했습니다. 나중에 직접 sql 서버를 끊었습니다. 교훈입니다!
코드는 간단하지만 명심하세요.
Create  PROCEDURE [dbo].[temphxb]
AS
BEGIN
    declare @uid int
    declare mycursortemp Cursor
        for select uid from temptable1 where Indate>'2015-05-21' and type='1' group by uid  having COUNT(*)>1
    open mycursortemp
    fetch next from mycursortemp into @uid
        
    while @@FETCH_STATUS=0
    begin
        delete from temptable1 where id 
        in (select top 1 id from temptable1 where uid=@uid and Indate>'2015-05-21' and fdtype='1')
        
        update temptable2 set num=num+1 where uid=@uid
        fetch next from mycursortemp into @uid
    end

    close mycursortemp
    DEALLOCATE mycursortemp    
end

커서의 사용 과정에서 일반적으로 다음과 같은 다섯 가지 절차로 나뉜다. 1. 커서를 성명한다.
2. 커서 열기
3. 커서 사용
4. 커서 닫기
5. 커서 삭제
오늘 저는 세 번째 커서가 데이터를 훑어보는 동안fetch next from mycursortemp into @uid를 실행하는 것을 잊어버려서 사순환이 발생하고 적지 않은 손실을 초래했습니다.또 많은 사람들이 커서를 닫는 것을 잊어버려서 다음 실행에 오류가 발생할 수도 있다.
데이터베이스 조작은 신중하고 신중해야 하며 명심해야 한다.

좋은 웹페이지 즐겨찾기