﻿DROP PROCEDURE IF EXISTS uspDeleteTanevRendjeNewBontottCsoport
GO

CREATE PROCEDURE uspDeleteTanevRendjeNewBontottCsoport
   @osztalyId int
  ,@bonottCsoportId int
AS BEGIN
  SET NOCOUNT ON
  
  
	CREATE TABLE #tmp (Id int, TanevId int, IntezmenyId int, OsztalyCsoportId int)

	INSERT INTO #tmp (Id, TanevId, IntezmenyId, OsztalyCsoportId)
		SELECT tr.ID, tr.C_TANEVID, tr.C_INTEZMENYID, @bonottCsoportId FROM T_TANEVRENDJE_OSSZES tr
			JOIN T_OSZTALYCSOPORT_TANEVRENDJE  ocstr ON ocstr.C_TANEVRENDJEID = tr.ID and ocstr.C_OSZTALYCSOPORTID = @osztalyId
		WHERE
			tr.TOROLT = 'F' and tr.C_AKTIV = 'T' and tr.C_EGYEDINAP = 'T'

	DELETE FROM T_OSZTALYCSOPORT_TANEVRENDJE WHERE C_TANEVRENDJEID in (select Id from #tmp) AND C_OSZTALYCSOPORTID = @bonottCsoportId
	
	DECLARE @Id varchar(50)
	DECLARE @TanevId varchar(50)
	DECLARE @IntezmenyId varchar(50)
	DECLARE @OsztalycsoportId varchar(50)

	DECLARE tr_cursor CURSOR FOR  
	SELECT Id, TanevId, IntezmenyId, OsztalyCsoportId FROM #tmp  
	OPEN tr_cursor;  
	  
	FETCH NEXT FROM tr_cursor INTO @Id, @TanevId, @IntezmenyId, @OsztalycsoportId;
	WHILE @@FETCH_STATUS = 0  
	BEGIN  
		
		exec uspDeleteEgyediNapOsztalycsoport @pTanevRendjeId=@Id,@pOsztalycsoportId=@OsztalycsoportId,@pIsKapcsolodoCsoportokIs=N'F'

		FETCH NEXT FROM tr_cursor INTO  @Id, @TanevId, @IntezmenyId, @OsztalycsoportId;
	END  
	  
	CLOSE tr_cursor;  
	DEALLOCATE tr_cursor;  

	DROP TABLE #tmp

END