SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('[dbo].[sp_FizikaiTorlesOrarendek]') IS NOT NULL
BEGIN
  DROP PROCEDURE [dbo].[sp_FizikaiTorlesOrarendek]
END
GO

-- =============================================
-- Description:	<Fizikailag töröljük az intézmény adott tanévére az órarendeket>
-- =============================================
CREATE PROCEDURE [dbo].[sp_FizikaiTorlesOrarendek]
	 @pIntezmenyId	INT
	,@pTanevId		INT
	,@pUserId		INT

AS
BEGIN

	-- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements.
	SET NOCOUNT ON;

	UPDATE T_TANITASIORA_OSSZES SET
		 C_ORARENDIORAID = NULL
		,SERIAL += 1
		,LASTCHANGED = GETDATE()
		,MODIFIER = @pUserId
	FROM
		T_TANITASIORA_OSSZES
	WHERE
			C_INTEZMENYID = @pIntezmenyId
		AND C_TANEVID = @pTanevId
		AND C_ORARENDIORAID IS NOT NULL

	DELETE FROM
		T_HELYETTESITESIIDOSZAK_OSSZES
	WHERE
			C_HELYETTESITETTORARENDID IN (
				SELECT ID
				FROM
					T_ORARENDIORA_OSSZES
				WHERE
						C_INTEZMENYID = @pIntezmenyId
					AND C_TANEVID = @pTanevId
			)
		AND C_INTEZMENYID = @pIntezmenyId
		AND C_TANEVID = @pTanevId

	DELETE FROM
		T_ORARENDIORA_OSSZES
	WHERE
			C_INTEZMENYID = @pIntezmenyId
		AND C_TANEVID = @pTanevId

END


GO

