SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [PRDEX_RUN_POSTPROCESSING]
-- =================================================================
-- Sub procedure to run POSTPROCESSING steps
-- 
-- Returns: INTEGER; 0 = ok; 0 <> not ok
-- =================================================================
-- Copyright (c) 2025 by Digital Data GmbH
--
-- Digital Data GmbH • Ludwig-Rinn-Strasse 16 • D-35452 Heuchelheim
-- Tel.: 0641/202360 • E-Mail: info-flow@digitaldata.works
-- =================================================================
-- Creation Date / Author:    01.08.2025 / MK
-- Version Date / Editor:     01.08.2025 / MK
-- Version Number:            1.0.0.0
-- =================================================================
-- History:
-- 01.08.2025 / MK - First Version

CREATE OR ALTER PROCEDURE [dbo].[PRDEX_RUN_POSTPROCESSING](
	@pPROFILE_ID				BIGINT,
	@pTEST_MODE					BIT,
	@pASYNC_MODE				BIT,
	@pBATCH_ID					NVARCHAR(25),
	@pBATCH_CREATOR				NVARCHAR(50),
	@pLANGUAGE					NVARCHAR(10),
	@pLOG_LEVEL					NVARCHAR(25),
	@pSTART_QUERY				NVARCHAR(MAX),
	@pSTART_QUERY_VAR1			NVARCHAR(50),
	@pSTART_QUERY_VAR2			NVARCHAR(50),
	@pSTART_QUERY_VAR3			NVARCHAR(50),
	@pSTART_QUERY_VAR4			NVARCHAR(50),
	@pSTART_QUERY_VAR5			NVARCHAR(50),
	@pMAIN_QUERY				NVARCHAR(MAX),
	@pMAIN_QUERY_VAR1			NVARCHAR(50),
	@pMAIN_QUERY_VAR2			NVARCHAR(50),
	@pMAIN_QUERY_VAR3			NVARCHAR(50),
	@pMAIN_QUERY_VAR4			NVARCHAR(50),
	@pMAIN_QUERY_VAR5			NVARCHAR(50),
	@pEND_QUERY					NVARCHAR(MAX),
	@pEND_QUERY_VAR1			NVARCHAR(50),
	@pEND_QUERY_VAR2			NVARCHAR(50),
	@pEND_QUERY_VAR3			NVARCHAR(50),
	@pEND_QUERY_VAR4			NVARCHAR(50),
	@pEND_QUERY_VAR5			NVARCHAR(50),
	@pFORCE						BIT,
	@pPROFILE_NAME				NVARCHAR(50),
	@pMANDANTOR					NVARCHAR(50),
	@pEXPORT_TYPE				NVARCHAR(50),
	@pDEFAULT_DATA_SEPARATOR	NVARCHAR(10),
	@pTBDEX_TMP_TABLE_SCHEMA	NVARCHAR(150),
	@pTBDEX_TMP_TABLE_NAME		NVARCHAR(150),
	@pTBDEX_TMP_TABLE_COLUMNS	NVARCHAR(MAX)
)
AS
BEGIN TRY

	SET NOCOUNT ON;

	-- declare new vars because of parameter sniffing
	DECLARE @PROFILE_ID							BIGINT			= @pPROFILE_ID,
			@TEST_MODE							BIT				= @pTEST_MODE,
			@ASYNC_MODE							BIT				= @pASYNC_MODE,
			@BATCH_ID							NVARCHAR(25)	= @pBATCH_ID,
			@BATCH_CREATOR						NVARCHAR(128)	= ISNULL(@pBATCH_CREATOR,OBJECT_NAME(@@PROCID)),
			@LANGUAGE							NVARCHAR(10)	= @pLANGUAGE,
			@LOG_LEVEL							NVARCHAR(25)	= UPPER(ISNULL(@pLOG_LEVEL,'INFO')),
			@START_QUERY						NVARCHAR(MAX)	= @pSTART_QUERY,
			@START_QUERY_VAR1					NVARCHAR(50)	= @pSTART_QUERY_VAR1,
			@START_QUERY_VAR2					NVARCHAR(50)	= @pSTART_QUERY_VAR2,
			@START_QUERY_VAR3					NVARCHAR(50)	= @pSTART_QUERY_VAR3,
			@START_QUERY_VAR4					NVARCHAR(50)	= @pSTART_QUERY_VAR4,
			@START_QUERY_VAR5					NVARCHAR(50)	= @pSTART_QUERY_VAR5,
			@MAIN_QUERY							NVARCHAR(MAX)	= @pMAIN_QUERY,
			@MAIN_QUERY_VAR1					NVARCHAR(50)	= @pMAIN_QUERY_VAR1,
			@MAIN_QUERY_VAR2					NVARCHAR(50)	= @pMAIN_QUERY_VAR2,
			@MAIN_QUERY_VAR3					NVARCHAR(50)	= @pMAIN_QUERY_VAR3,
			@MAIN_QUERY_VAR4					NVARCHAR(50)	= @pMAIN_QUERY_VAR4,
			@MAIN_QUERY_VAR5					NVARCHAR(50)	= @pMAIN_QUERY_VAR5,
			@END_QUERY							NVARCHAR(MAX)	= @pEND_QUERY,
			@END_QUERY_VAR1						NVARCHAR(50)	= @pEND_QUERY_VAR1,
			@END_QUERY_VAR2						NVARCHAR(50)	= @pEND_QUERY_VAR2,
			@END_QUERY_VAR3						NVARCHAR(50)	= @pEND_QUERY_VAR3,
			@END_QUERY_VAR4						NVARCHAR(50)	= @pEND_QUERY_VAR4,
			@END_QUERY_VAR5						NVARCHAR(50)	= @pEND_QUERY_VAR5,
			@FORCE								BIT				= @pFORCE,
			@PROFILE_NAME						NVARCHAR(50)	= @pPROFILE_NAME,
			@MANDANTOR							NVARCHAR(50)	= @pMANDANTOR,
			@EXPORT_TYPE						NVARCHAR(10)	= @pEXPORT_TYPE,
			@DEFAULT_DATA_SEPARATOR				NVARCHAR(50)	= @pDEFAULT_DATA_SEPARATOR,
			@TBDEX_TMP_TABLE_SCHEMA				NVARCHAR(150)	= @pTBDEX_TMP_TABLE_SCHEMA,
			@TBDEX_TMP_TABLE_NAME				NVARCHAR(150)	= @pTBDEX_TMP_TABLE_NAME,
			@TBDEX_TMP_TABLE_COLUMNS			NVARCHAR(MAX)	= @pTBDEX_TMP_TABLE_COLUMNS;

	-- declare virtual table for POSTPROCESSING steps
	DECLARE	@vTBDEX_CFG_RESOURCES_POSTPROCESSING as TABLE (
			[GUID]								BIGINT 				NOT NULL,
			[PROFILE_ID]						BIGINT				NOT NULL,
			[SEQUENCE]							TINYINT				NOT NULL,
			[POSTPROCESSING_NAME]				NVARCHAR(50)		NOT NULL,
			[POSTPROCESSING_TYPE]				NVARCHAR(10)		NOT NULL,
			[POSTPROCESSING_QUERY]				NVARCHAR(max)		NOT NULL,
			[ERROR_ACTION]						NVARCHAR(25)		NOT NULL);

	-- declare runtime vars
    DECLARE @MY_PROCEDURE_NAME					NVARCHAR(128)	= OBJECT_NAME(@@PROCID);
	DECLARE	@ACTIVE_FLAG						BIT				= 1,
			@INACTIVE_FLAG						BIT				= 0,
			@STEP_COUNTER						INT				= 0,
			@ROW_COUNT							SMALLINT		= 0,
			@GUID								BIGINT			= 0,
			@SEQUENCE							TINYINT			= 0,
			@POSTPROCESSING_NAME				NVARCHAR(50)	= NULL,
			@POSTPROCESSING_TYPE				NVARCHAR(10)	= NULL,
			@POSTPROCESSING_QUERY				NVARCHAR(MAX)	= NULL,
			@ERROR_ACTION						NVARCHAR(25)	= NULL,
			@REFERENCE1							NVARCHAR(150)	= NULL,
			@REFERENCE2							NVARCHAR(150)	= NULL,
			@REFERENCE3							NVARCHAR(150)	= NULL,
			@REFERENCE4							NVARCHAR(150)	= NULL,
			@REFERENCE5							NVARCHAR(150)	= NULL,
			@PLACEHOLDER_NAME					NVARCHAR(50)	= 'QUERY_POSTPROCESSING',
			@PLACEHOLDER						NVARCHAR(MAX)	= NULL,
			@PLACEHOLDER_TYPE					NVARCHAR(10)	= 'CONST',
			@HAS_UNRESOLVED_PLACEHOLDER		BIT				= 0,
			@HAS_RESTRICTED_SQL				BIT				= 0,
			@RETURN_STATUS						INT				= 0,
			@RETURN_STATUS_TEXT					NVARCHAR(MAX)	= concat('START PROCEDURE [',@MY_PROCEDURE_NAME,'] @ ',CONVERT(varchar(50),GETDATE(),120)),
			@RETURN_ERROR_TEXT					NVARCHAR(MAX)	= '';

	PRINT '====================================================================================================';
	PRINT @RETURN_STATUS_TEXT;
	PRINT '';
	PRINT 'PARAMETER01 - @PROFILE_ID:              ' + CONVERT(NVARCHAR(50),ISNULL(@PROFILE_ID,'<NO-VALUE>'));
	PRINT 'PARAMETER02 - @TEST_MODE:               ' + CONVERT(NVARCHAR(50),ISNULL(@TEST_MODE,'<NO-VALUE>'));
	PRINT 'PARAMETER03 - @ASYNC_MODE:              ' + CONVERT(NVARCHAR(50),ISNULL(@ASYNC_MODE,'<NO-VALUE>'));
	PRINT 'PARAMETER04 - @BATCH_ID:                ' + CONVERT(NVARCHAR(50),ISNULL(@BATCH_ID,'<NO-VALUE>'));
	PRINT 'PARAMETER05 - @BATCH_CREATOR:           ' + CONVERT(NVARCHAR(50),ISNULL(@BATCH_CREATOR,'<NO-VALUE>'));
	PRINT 'PARAMETER06 - @LANGUAGE:                ' + CONVERT(NVARCHAR(50),ISNULL(@LANGUAGE,'<NO-VALUE>'));
	PRINT 'PARAMETER07 - @LOG_LEVEL:               ' + CONVERT(NVARCHAR(50),ISNULL(@LOG_LEVEL,'<NO-VALUE>'));
	PRINT 'PARAMETER08 - @START_QUERY:             ' + CONVERT(NVARCHAR(50),ISNULL(@START_QUERY,'<NO-VALUE>'));
	PRINT 'PARAMETER09 - @START_QUERY_VAR1:        ' + CONVERT(NVARCHAR(50),ISNULL(@START_QUERY_VAR1,'<NO-VALUE>'));
	PRINT 'PARAMETER10 - @START_QUERY_VAR2:        ' + CONVERT(NVARCHAR(50),ISNULL(@START_QUERY_VAR2,'<NO-VALUE>'));
	PRINT 'PARAMETER11 - @START_QUERY_VAR3:        ' + CONVERT(NVARCHAR(50),ISNULL(@START_QUERY_VAR3,'<NO-VALUE>'));
	PRINT 'PARAMETER12 - @START_QUERY_VAR4:        ' + CONVERT(NVARCHAR(50),ISNULL(@START_QUERY_VAR4,'<NO-VALUE>'));
	PRINT 'PARAMETER13 - @START_QUERY_VAR5:        ' + CONVERT(NVARCHAR(50),ISNULL(@START_QUERY_VAR5,'<NO-VALUE>'));
	PRINT 'PARAMETER14 - @MAIN_QUERY:              ' + CONVERT(NVARCHAR(50),ISNULL(@MAIN_QUERY,'<NO-VALUE>'));
	PRINT 'PARAMETER15 - @MAIN_QUERY_VAR1:         ' + CONVERT(NVARCHAR(50),ISNULL(@MAIN_QUERY_VAR1,'<NO-VALUE>'));
	PRINT 'PARAMETER16 - @MAIN_QUERY_VAR2:         ' + CONVERT(NVARCHAR(50),ISNULL(@MAIN_QUERY_VAR2,'<NO-VALUE>'));
	PRINT 'PARAMETER17 - @MAIN_QUERY_VAR3:         ' + CONVERT(NVARCHAR(50),ISNULL(@MAIN_QUERY_VAR3,'<NO-VALUE>'));
	PRINT 'PARAMETER18 - @MAIN_QUERY_VAR4:         ' + CONVERT(NVARCHAR(50),ISNULL(@MAIN_QUERY_VAR4,'<NO-VALUE>'));
	PRINT 'PARAMETER19 - @MAIN_QUERY_VAR5:         ' + CONVERT(NVARCHAR(50),ISNULL(@MAIN_QUERY_VAR5,'<NO-VALUE>'));
	PRINT 'PARAMETER20 - @END_QUERY:               ' + CONVERT(NVARCHAR(50),ISNULL(@END_QUERY,'<NO-VALUE>'));
	PRINT 'PARAMETER21 - @END_QUERY_VAR1:          ' + CONVERT(NVARCHAR(50),ISNULL(@END_QUERY_VAR1,'<NO-VALUE>'));
	PRINT 'PARAMETER22 - @END_QUERY_VAR2:          ' + CONVERT(NVARCHAR(50),ISNULL(@END_QUERY_VAR2,'<NO-VALUE>'));
	PRINT 'PARAMETER23 - @END_QUERY_VAR3:          ' + CONVERT(NVARCHAR(50),ISNULL(@END_QUERY_VAR3,'<NO-VALUE>'));
	PRINT 'PARAMETER24 - @END_QUERY_VAR4:          ' + CONVERT(NVARCHAR(50),ISNULL(@END_QUERY_VAR4,'<NO-VALUE>'));
	PRINT 'PARAMETER25 - @END_QUERY_VAR5:          ' + CONVERT(NVARCHAR(50),ISNULL(@END_QUERY_VAR5,'<NO-VALUE>'));
	PRINT 'PARAMETER26 - @FORCE:                   ' + CONVERT(NVARCHAR(50),ISNULL(@FORCE,'<NO-VALUE>'));
	PRINT 'PARAMETER27 - @PROFILE_NAME:            ' + CONVERT(NVARCHAR(50),ISNULL(@PROFILE_NAME,'<NO-VALUE>'));
	PRINT 'PARAMETER28 - @MANDANTOR:               ' + CONVERT(NVARCHAR(50),ISNULL(@MANDANTOR,'<NO-VALUE>'));
	PRINT 'PARAMETER29 - @EXPORT_TYPE:             ' + CONVERT(NVARCHAR(50),ISNULL(@EXPORT_TYPE,'<NO-VALUE>'));
	PRINT 'PARAMETER30 - @DEFAULT_DATA_SEPARATOR:  ' + CONVERT(NVARCHAR(50),ISNULL(@DEFAULT_DATA_SEPARATOR,'<NO-VALUE>'));
	PRINT 'PARAMETER31 - @TBDEX_TMP_TABLE_SCHEMA:  ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_TABLE_SCHEMA,'<NO-VALUE>'));
	PRINT 'PARAMETER32 - @TBDEX_TMP_TABLE_NAME:    ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_TABLE_NAME,'<NO-VALUE>'));
	PRINT 'PARAMETER33 - @TBDEX_TMP_TABLE_COLUMNS: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_TABLE_COLUMNS,'<NO-VALUE>'));
	PRINT '';

	--===================================================-- Log result to table --===================================================--
	IF (@LOG_LEVEL in ('INFO')) BEGIN
		EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
								@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
								@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
								@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
	END;
	-----------------------------------------------------------------------------------------------------------------------------------


	--===================================================-- POSTPROCESSING steps --===================================================--
	IF (@TEST_MODE = 1) OR (@ASYNC_MODE = 1) BEGIN

		--======================================================-- Output result --======================================================--
		SET @RETURN_STATUS		= 0;
		SET @RETURN_STATUS_TEXT = concat('TEST_MODE and/or ASYNC_MODE are enabled!','Exiting...');
		SET @RETURN_ERROR_TEXT	= concat('','');
		
		PRINT 'INFO: ' + @RETURN_STATUS_TEXT;
		-----------------------------------------------------------------------------------------------------------------------------------

		--===================================================-- Log result to table --===================================================--
		IF (@LOG_LEVEL in ('INFO')) BEGIN
			EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
									@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
									@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
									@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
		END;
		-----------------------------------------------------------------------------------------------------------------------------------

		--======================================================-- Output result --======================================================--
		SET @RETURN_STATUS		= 0;
		SET @RETURN_STATUS_TEXT = concat('END PROCEDURE [',@MY_PROCEDURE_NAME,'] @ ',CONVERT(NVARCHAR(50),GETDATE(),120));
		SET @RETURN_ERROR_TEXT	= concat('','');

		PRINT @RETURN_STATUS_TEXT;
		PRINT '====================================================================================================';
		-----------------------------------------------------------------------------------------------------------------------------------

		--===================================================-- Log result to table --===================================================--
		IF (@LOG_LEVEL in ('INFO')) BEGIN
			EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
									@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
									@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
									@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
		END;
		-----------------------------------------------------------------------------------------------------------------------------------

		RETURN @RETURN_STATUS;

	END; ELSE BEGIN

		INSERT INTO @vTBDEX_CFG_RESOURCES_POSTPROCESSING([GUID],[PROFILE_ID],[SEQUENCE],[POSTPROCESSING_NAME],[POSTPROCESSING_TYPE],[POSTPROCESSING_QUERY],[ERROR_ACTION])
		SELECT TOP 100 PERCENT                           [GUID],[PROFILE_ID],[SEQUENCE],[PLACEHOLDER_NAME],   [PLACEHOLDER_TYPE],LTRIM(RTRIM([PLACEHOLDER])),CASE WHEN [ERROR_ACTION] = 1 THEN 'CONTINUE' ELSE 'STOP' END
		FROM     [TBDEX_CFG_RESOURCES]
		WHERE    [PROFILE_ID] IN (@PROFILE_ID,0) AND [ACTIVE] = @ACTIVE_FLAG AND LTRIM(RTRIM([PLACEHOLDER_NAME])) = @PLACEHOLDER_NAME
		ORDER BY [PROFILE_ID] ASC,[GUID] DESC;

		SET @ROW_COUNT = (ISNULL((SELECT COUNT(*) FROM @vTBDEX_CFG_RESOURCES_POSTPROCESSING),0));
		IF (@ROW_COUNT = 0) BEGIN

			--======================================================-- Output result --======================================================--
			SET @RETURN_STATUS		= 0;
			SET @RETURN_STATUS_TEXT = concat('Found no assigned POSTPROCESSING steps in [TBDEX_CFG_RESOURCES] table! ',', Exiting POSTPROCESSING...');
			SET @RETURN_ERROR_TEXT	= concat('','');
		
			PRINT 'WARN: ' + @RETURN_STATUS_TEXT;
			-----------------------------------------------------------------------------------------------------------------------------------

			--===================================================-- Log result to table --===================================================--
			IF (@LOG_LEVEL in ('INFO')) BEGIN
				EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
										@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
										@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
										@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
			END;
			-----------------------------------------------------------------------------------------------------------------------------------

		END; ELSE BEGIN

			--======================================================-- Output result --======================================================--
			SET @RETURN_STATUS		= 0;
			SET @RETURN_STATUS_TEXT = concat('Found ',@ROW_COUNT,' assigned POSTPROCESSING steps in [TBDEX_CFG_RESOURCES] table!',', Engaging POSTPROCESSING...');
			SET @RETURN_ERROR_TEXT	= concat('','');

			PRINT '';
			PRINT 'INFO: ' + @RETURN_STATUS_TEXT;
			-----------------------------------------------------------------------------------------------------------------------------------

			--===================================================-- Log result to table --===================================================--
			IF (@LOG_LEVEL in ('INFO')) BEGIN
				EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
										@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
										@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
										@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
			END;
			-----------------------------------------------------------------------------------------------------------------------------------


			--================================================-- Run loop for POSTPROCESSING --===============================================--
			DECLARE CURSOR_POSTPROCESSING CURSOR
			LOCAL FAST_FORWARD FOR 
				SELECT	 [GUID]					as 'GUID', 
						 [PROFILE_ID]			as 'PROFILE_ID', 
						 [SEQUENCE]				as 'SEQUENCE',
						 [POSTPROCESSING_NAME]	as 'POSTPROCESSING_NAME',
						 [POSTPROCESSING_TYPE]	as 'POSTPROCESSING_TYPE', 
						 [POSTPROCESSING_QUERY]	as 'POSTPROCESSING_QUERY',
						 [ERROR_ACTION]			as 'ERROR_ACTION'
				FROM     @vTBDEX_CFG_RESOURCES_POSTPROCESSING
				ORDER BY [PROFILE_ID] ASC,[SEQUENCE] ASC, [GUID] DESC;

			OPEN CURSOR_POSTPROCESSING 
			FETCH NEXT FROM CURSOR_POSTPROCESSING INTO @GUID, @PROFILE_ID, @SEQUENCE, @POSTPROCESSING_NAME, @POSTPROCESSING_TYPE, @POSTPROCESSING_QUERY, @ERROR_ACTION;
			WHILE @@FETCH_STATUS = 0
				BEGIN
					SET @STEP_COUNTER = @STEP_COUNTER + 1;

					--======================================================-- Output result --======================================================--
					SET @RETURN_STATUS		= 0;
					SET @RETURN_STATUS_TEXT = concat('Running POSTPROCESSING step with GUID: ',@GUID,'. The set "ERROR_ACTION" behavior is: ',@ERROR_ACTION);
					SET @RETURN_ERROR_TEXT	= concat('','');

					PRINT '';
					PRINT 'INFO: ' + @RETURN_STATUS_TEXT;
					-----------------------------------------------------------------------------------------------------------------------------------

					--===================================================-- Log result to table --===================================================--
					IF (@LOG_LEVEL in ('INFO')) BEGIN
						EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
												@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
												@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
												@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
					END;
					-----------------------------------------------------------------------------------------------------------------------------------
					
					
					BEGIN TRY

						--==================================================-- Handle placeholders  --==================================================--
						PRINT '';
						PRINT 'Replacing placeholders...';
		
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%PROFILE_ID%',ISNULL(@PROFILE_ID,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%TEST_MODE%',ISNULL(@TEST_MODE,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%ASYNC_MODE%',ISNULL(@ASYNC_MODE,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%BATCH_ID%',ISNULL(@BATCH_ID,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%BATCH_CREATOR%',ISNULL(@BATCH_CREATOR,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%LANGUAGE%',ISNULL(@LANGUAGE,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%LOG_LEVEL%',ISNULL(@LOG_LEVEL,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%START_QUERY%',ISNULL(@START_QUERY,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%START_QUERY_VAR1%',ISNULL(@START_QUERY_VAR1,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%START_QUERY_VAR2%',ISNULL(@START_QUERY_VAR2,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%START_QUERY_VAR3%',ISNULL(@START_QUERY_VAR3,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%START_QUERY_VAR4%',ISNULL(@START_QUERY_VAR4,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%START_QUERY_VAR5%',ISNULL(@START_QUERY_VAR5,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MAIN_QUERY%',ISNULL(@MAIN_QUERY,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MAIN_QUERY_VAR1%',ISNULL(@MAIN_QUERY_VAR1,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MAIN_QUERY_VAR2%',ISNULL(@MAIN_QUERY_VAR2,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MAIN_QUERY_VAR3%',ISNULL(@MAIN_QUERY_VAR3,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MAIN_QUERY_VAR4%',ISNULL(@MAIN_QUERY_VAR4,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MAIN_QUERY_VAR5%',ISNULL(@MAIN_QUERY_VAR5,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%END_QUERY%',ISNULL(@END_QUERY,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%END_QUERY_VAR1%',ISNULL(@END_QUERY_VAR1,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%END_QUERY_VAR2%',ISNULL(@END_QUERY_VAR2,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%END_QUERY_VAR3%',ISNULL(@END_QUERY_VAR3,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%END_QUERY_VAR4%',ISNULL(@END_QUERY_VAR4,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%END_QUERY_VAR5%',ISNULL(@END_QUERY_VAR5,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%FORCE%',ISNULL(@FORCE,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MY_PROCEDURE_NAME%',ISNULL(@MY_PROCEDURE_NAME,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%PROFILE_NAME%',ISNULL(@PROFILE_NAME,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%MANDANTOR%',ISNULL(@MANDANTOR,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%EXPORT_TYPE%',ISNULL(@EXPORT_TYPE,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%REFERENCE1%',ISNULL(@REFERENCE1,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%REFERENCE2%',ISNULL(@REFERENCE2,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%REFERENCE3%',ISNULL(@REFERENCE3,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%REFERENCE4%',ISNULL(@REFERENCE4,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%REFERENCE5%',ISNULL(@REFERENCE5,''));

						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%TBDEX_TMP_TABLE_SCHEMA%',ISNULL(@TBDEX_TMP_TABLE_SCHEMA,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%TBDEX_TMP_TABLE_NAME%',ISNULL(@TBDEX_TMP_TABLE_NAME,''));
						SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,'%TBDEX_TMP_TABLE_COLUMNS%',ISNULL(@TBDEX_TMP_TABLE_COLUMNS,''));

						DECLARE CURSOR_PLACEHOLDER CURSOR
						LOCAL FAST_FORWARD FOR 
							SELECT [PLACEHOLDER_NAME] as 'Placeholder_Name', [PLACEHOLDER] as 'Placeholder'
							FROM   [TBDEX_CFG_RESOURCES]
							WHERE  [PROFILE_ID] IN (0,@PROFILE_ID) AND [ACTIVE] = @ACTIVE_FLAG AND LTRIM(RTRIM([PLACEHOLDER_TYPE])) = @PLACEHOLDER_TYPE
							ORDER BY [SEQUENCE] ASC;

						OPEN CURSOR_PLACEHOLDER 
						FETCH NEXT FROM CURSOR_PLACEHOLDER INTO @PLACEHOLDER_NAME,@PLACEHOLDER;
						WHILE @@FETCH_STATUS = 0
							BEGIN

								SET @POSTPROCESSING_QUERY = REPLACE(@POSTPROCESSING_QUERY,concat('%',@PLACEHOLDER_NAME,'%'),ISNULL(@PLACEHOLDER,''));

								FETCH NEXT FROM CURSOR_PLACEHOLDER INTO @PLACEHOLDER_NAME,@PLACEHOLDER;
							END
						CLOSE CURSOR_PLACEHOLDER;
						DEALLOCATE CURSOR_PLACEHOLDER;
						-----------------------------------------------------------------------------------------------------------------------------------

						--======================================================-- Output result --======================================================--
						SET @RETURN_STATUS		= 0;
						SET @RETURN_STATUS_TEXT = concat('Prepared "@POSTPROCESSING_QUERY" (length: ',LEN(@POSTPROCESSING_QUERY),', preview: ',LEFT(@POSTPROCESSING_QUERY,500),')');
						SET @RETURN_ERROR_TEXT	= concat('','');

						PRINT 'INFO: ' + @RETURN_STATUS_TEXT;
						-----------------------------------------------------------------------------------------------------------------------------------
	
						--===================================================-- Log result to table --===================================================--
						IF (@LOG_LEVEL in ('INFO')) BEGIN
							EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
													@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
													@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
													@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
						END;
						-----------------------------------------------------------------------------------------------------------------------------------

						--======================================================-- Safety checks --======================================================--
						EXEC [PRDEX_TEST_DYNAMIC_SQL]	@pQUERY			= @POSTPROCESSING_QUERY,
														@pRETURN_STATUS = 50056,
														@pQUERY_NAME	= N'@POSTPROCESSING_QUERY';
						-----------------------------------------------------------------------------------------------------------------------------------

						--=========================================-- Finally execute the POSTPROCESSING_QUERY --=========================================--
						EXEC sp_executesql @POSTPROCESSING_QUERY;
						-----------------------------------------------------------------------------------------------------------------------------------

					END TRY BEGIN CATCH 

						--======================================================-- Output result --======================================================--
						SET @RETURN_STATUS		= 50018;
						SET @RETURN_STATUS_TEXT = concat('','');
						SET @RETURN_ERROR_TEXT	= CONVERT(NVARCHAR(500),ERROR_MESSAGE());

						PRINT 'ERROR: ' + @RETURN_ERROR_TEXT;
						-----------------------------------------------------------------------------------------------------------------------------------

						--===================================================-- Log result to table --===================================================--
						IF (@LOG_LEVEL in ('INFO','WARN','ERROR')) BEGIN
							EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'ERROR',			@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
													@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
													@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
													@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
						END;
						-----------------------------------------------------------------------------------------------------------------------------------

					END CATCH;
					-----------------------------------------------------------------------------------------------------------------------------------

					IF (@RETURN_STATUS <> 0) BEGIN

						IF (@ERROR_ACTION <> 'CONTINUE') BEGIN

							--====================================================-- exception / error --====================================================--
							SET @RETURN_STATUS		= @RETURN_STATUS;
							SET @RETURN_STATUS_TEXT = concat('Error occurred in [PRDEX_RUN_POSTPROCESSING]!',' Exiting...');
							SET @RETURN_ERROR_TEXT	= ISNULL(@RETURN_ERROR_TEXT,'Unknown postprocessing error.');
		
							THROW @RETURN_STATUS,@RETURN_ERROR_TEXT,1;
							-----------------------------------------------------------------------------------------------------------------------------------

						END; ELSE BEGIN

							--====================================================-- exception / error --====================================================--
							SET @RETURN_STATUS		= @RETURN_STATUS;
							SET @RETURN_STATUS_TEXT = concat('Error occurred in [PRDEX_RUN_POSTPROCESSING]!',' Continue...');
							SET @RETURN_ERROR_TEXT	= ISNULL(@RETURN_ERROR_TEXT,'Unknown postprocessing error.');

							PRINT 'ERROR: ' + @RETURN_STATUS_TEXT;
							-----------------------------------------------------------------------------------------------------------------------------------

							--===================================================-- Log result to table --===================================================--
							IF (@LOG_LEVEL in ('INFO','WARN','ERROR')) BEGIN
								EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'ERROR',			@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
														@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
														@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
														@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
							END;
							-----------------------------------------------------------------------------------------------------------------------------------

						END;

					END; ELSE BEGIN

						--======================================================-- Output result --======================================================--
						SET @RETURN_STATUS		= 0;
						SET @RETURN_STATUS_TEXT = concat('Procedure [PRDEX_RUN_POSTPROCESSING] worked just fine!','');
						SET @RETURN_ERROR_TEXT	= concat('','');
		
						PRINT '';
						PRINT 'INFO: ' + @RETURN_STATUS_TEXT;
						-----------------------------------------------------------------------------------------------------------------------------------

						--===================================================-- Log result to table --===================================================--
						IF (@LOG_LEVEL in ('INFO')) BEGIN
							EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
													@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
													@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
													@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
						END;
						-----------------------------------------------------------------------------------------------------------------------------------

					END;
					-----------------------------------------------------------------------------------------------------------------------------------

					FETCH NEXT FROM CURSOR_POSTPROCESSING INTO @GUID, @PROFILE_ID, @SEQUENCE, @POSTPROCESSING_NAME, @POSTPROCESSING_TYPE, @POSTPROCESSING_QUERY, @ERROR_ACTION;
				END
			CLOSE CURSOR_POSTPROCESSING;
			DEALLOCATE CURSOR_POSTPROCESSING;
			-----------------------------------------------------------------------------------------------------------------------------------

		END;
		-----------------------------------------------------------------------------------------------------------------------------------

	END;
	-----------------------------------------------------------------------------------------------------------------------------------

	--======================================================-- Output result --======================================================--
	SET @RETURN_STATUS		= 0;
	SET @RETURN_STATUS_TEXT = concat('END PROCEDURE [',@MY_PROCEDURE_NAME,'] @ ',CONVERT(NVARCHAR(50),GETDATE(),120));
	SET @RETURN_ERROR_TEXT	= concat('','');

	PRINT '';
	PRINT @RETURN_STATUS_TEXT;
	PRINT '====================================================================================================';
	-----------------------------------------------------------------------------------------------------------------------------------

	--===================================================-- Log result to table --===================================================--
	IF (@LOG_LEVEL in ('INFO')) BEGIN
		EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'INFO',				@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
								@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
								@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
								@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
	END;
	-----------------------------------------------------------------------------------------------------------------------------------

	RETURN @RETURN_STATUS;

END TRY BEGIN CATCH

	--====================================================-- exception / error --====================================================--
	IF (@@TRANCOUNT > 0) BEGIN
		ROLLBACK TRANSACTION;
	END;

	PRINT 'ERROR: ' + @RETURN_ERROR_TEXT;
	-----------------------------------------------------------------------------------------------------------------------------------

	--===================================================-- Log result to table --===================================================--
	IF (@LOG_LEVEL in ('INFO','WARN','ERROR')) BEGIN
		EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'ERROR',			@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
								@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
								@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
								@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
	END;
	-----------------------------------------------------------------------------------------------------------------------------------

	--======================================================-- Output result --======================================================--
	SET @RETURN_STATUS		= CASE WHEN @RETURN_STATUS > 50000 THEN @RETURN_STATUS ELSE 50000 END;
	SET @RETURN_STATUS_TEXT = concat('END PROCEDURE [',@MY_PROCEDURE_NAME,'] @ ',CONVERT(NVARCHAR(50),GETDATE(),120));
	SET @RETURN_ERROR_TEXT	= concat('ERROR MESSAGE: ',CONVERT(NVARCHAR(500),ERROR_MESSAGE()));

	PRINT 'ERROR IN PROCEDURE: [' + @MY_PROCEDURE_NAME + ']' + char(13) + @RETURN_ERROR_TEXT;
	PRINT @RETURN_STATUS_TEXT;
	PRINT '====================================================================================================';
	-----------------------------------------------------------------------------------------------------------------------------------

	--===================================================-- Log result to table --===================================================--
	IF (@LOG_LEVEL in ('INFO','WARN','ERROR')) BEGIN
		EXEC [PRDEX_WRITE_LOG]	@pLOG_LEVEL	 = N'ERROR',			@pPROFILE_ID	= @PROFILE_ID,			@pBATCH_ID		= @BATCH_ID,
								@pREFERENCE1 = @REFERENCE1,			@pREFERENCE2	= @REFERENCE2,			@pREFERENCE3	= @REFERENCE3,
								@pREFERENCE4 = @REFERENCE4,			@pREFERENCE5	= @REFERENCE5,			@pRETURN_CODE	= @RETURN_STATUS,
								@pMESSAGE1	 = @RETURN_STATUS_TEXT, @pMESSAGE2		= @RETURN_ERROR_TEXT,	@pADDED_WHO		= @MY_PROCEDURE_NAME;
	END;
	-----------------------------------------------------------------------------------------------------------------------------------

	RETURN @RETURN_STATUS;

END CATCH;
GO