﻿DECLARE hibasOrarendiOrak CURSOR LOCAL FOR
  SELECT
     C_INTEZMENYID
    ,C_TANEVID
    ,ID
  FROM T_ORARENDIORA
  WHERE TOROLT = 'F'
    AND (C_ORAKEZDETE IS NULL OR C_ORAVEGE IS NULL)

DECLARE
   @intezmenyId int
  ,@tanevId int
  ,@orarendiOraId int

OPEN hibasOrarendiOrak
FETCH NEXT FROM hibasOrarendiOrak INTO @intezmenyId, @tanevId, @orarendiOraId
WHILE @@FETCH_STATUS = 0
BEGIN

  UPDATE T_ORARENDIORA
  SET
     TOROLT = 'T'
    ,MODIFIER = 0
    ,SERIAL = SERIAL + 1
    ,LASTCHANGED = GETDATE()
  WHERE C_INTEZMENYID = @intezmenyId
    AND C_TANEVID = @tanevId
    AND ID = @orarendiOraId

  EXEC uspGenerateTeljesOrarend
     @pIntezmenyId = @intezmenyId
    ,@pTanevId = @tanevId
    ,@pOrarendiOraId = @orarendiOraId

  FETCH NEXT FROM hibasOrarendiOrak INTO @intezmenyId, @tanevId, @orarendiOraId
END

CLOSE hibasOrarendiOrak
DEALLOCATE hibasOrarendiOrak
