/************************************************************************************/
/*** Dieses Query gehört zu WS#66636: Kooperation - Kooperation auflösen          ***/
/************************************************************************************/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sp_DeleteKOP]') AND type in (N'P', N'PC'))

BEGIN
	DROP PROCEDURE [dbo].[sp_DeleteKOP]
END 
GO

CREATE PROCEDURE [dbo].[sp_DeleteKOP] @lMandantID INT AS
BEGIN
	SET NOCOUNT ON

	IF NOT EXISTS (SELECT lMandantID FROM KODef WHERE lMandantID = @lMandantID) BEGIN
		RAISERROR ('sp_DeleteKOP(): Invalid Parameter lMandant!', 11, 1)
		RETURN
	END

	BEGIN TRANSACTION

	-- Lösche alle relevanten Daten eines Kooperationspartners
	DELETE FROM KOINContainer WHERE (lMandantID = @lMandantID)
	DELETE FROM KOOUTContainer WHERE (lMandantID = @lMandantID)
	DELETE FROM KOLog WHERE (lMandantID = @lMandantID)
	DELETE FROM KOLeistung WHERE (lMandantID = @lMandantID)
	DELETE FROM KOHistory WHERE (lMandantID = @lMandantID)
	DELETE FROM KOBlockedService WHERE (lMandantID = @lMandantID)
	DELETE FROM KOBezirkStruktur WHERE (lMandantID = @lMandantID)

	UPDATE KODef SET dtLastInConnection = 0, dtLastOutConnection = 0, dtLastInData = 0, dtLastOutData = 0,
					 lINLastContainerSequenz = 0, lOUTLastContainerSequenz = 0, dtChanged = GETDATE()
	WHERE (lMandantID = @lMandantID)

	COMMIT TRANSACTION

END
GO

GRANT EXECUTE ON [dbo].[sp_DeleteKOP] TO [cx_Exec_SP_UDP]
GO
/************************************************************************************/
