SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [PRDEX_GET_INPUT_CONTENT]
-- =================================================================
-- Sucht alle noch nicht exportierten Dokumente bzw. Daten zusammen
-- 
-- Returns: INTEGER; 0 = ok; 0 <> nicht 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_GET_INPUT_CONTENT](
	@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),
	@pEXPORT_NUMBER_SCHEMA				NVARCHAR(25),
	@pDEFAULT_DATA_SEPARATOR			NVARCHAR(10),
	@pTBDEX_TMP_TABLE_SCHEMA			SYSNAME,
	@pTBDEX_TMP_TABLE_NAME				SYSNAME,
	@pTBDEX_TMP_TABLE_SCHEMA_AND_NAME	NVARCHAR(150),
	@pTBDEX_TMP_TABLE_COLUMNS			NVARCHAR(MAX),
	@pTBDEX_TMP_TABLE_DROP_QUERY		NVARCHAR(MAX),
	@pTBDEX_TMP_TABLE_CREATE_QUERY		NVARCHAR(MAX),
	@pTBDEX_TMP_TABLE_INSERT_QUERY		NVARCHAR(MAX),
	@pREFERENCE1						NVARCHAR(150),
	@pREFERENCE2						NVARCHAR(150),
	@pREFERENCE3						NVARCHAR(150),
	@pREFERENCE4						NVARCHAR(150),
	@pREFERENCE5						NVARCHAR(150),
	@pEXTERNAL_REFERENCE1				NVARCHAR(250),
	@pEXTERNAL_REFERENCE2				NVARCHAR(250),
	@pEXTERNAL_REFERENCE3				NVARCHAR(250),
	@pEXTERNAL_REFERENCE4				NVARCHAR(250),
	@pEXTERNAL_REFERENCE5				NVARCHAR(250)
)
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(500)	= @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(500)	= @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(500)	= @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(50)	= @pEXPORT_TYPE,
			@EXPORT_NUMBER_SCHEMA				NVARCHAR(25)	= @pEXPORT_NUMBER_SCHEMA,
			@DEFAULT_DATA_SEPARATOR				NVARCHAR(10)	= @pDEFAULT_DATA_SEPARATOR,
			@TBDEX_TMP_TABLE_SCHEMA				SYSNAME			= @pTBDEX_TMP_TABLE_SCHEMA,
			@TBDEX_TMP_TABLE_NAME				SYSNAME			= @pTBDEX_TMP_TABLE_NAME,
			@TBDEX_TMP_TABLE_SCHEMA_AND_NAME	NVARCHAR(150)	= @pTBDEX_TMP_TABLE_SCHEMA_AND_NAME,
			@TBDEX_TMP_TABLE_COLUMNS			NVARCHAR(MAX)	= @pTBDEX_TMP_TABLE_COLUMNS,
			@TBDEX_TMP_TABLE_DROP_QUERY			NVARCHAR(MAX)	= @pTBDEX_TMP_TABLE_DROP_QUERY,
			@TBDEX_TMP_TABLE_CREATE_QUERY		NVARCHAR(MAX)	= @pTBDEX_TMP_TABLE_CREATE_QUERY,
			@TBDEX_TMP_TABLE_INSERT_QUERY		NVARCHAR(MAX)	= @pTBDEX_TMP_TABLE_INSERT_QUERY,
			@REFERENCE1							NVARCHAR(150)	= @pREFERENCE1,
			@REFERENCE2							NVARCHAR(150)	= @pREFERENCE2,
			@REFERENCE3							NVARCHAR(150)	= @pREFERENCE3,
			@REFERENCE4							NVARCHAR(150)	= @pREFERENCE4,
			@REFERENCE5							NVARCHAR(150)	= @pREFERENCE5,
			@EXTERNAL_REFERENCE1				NVARCHAR(250)	= @pEXTERNAL_REFERENCE1,
			@EXTERNAL_REFERENCE2				NVARCHAR(250)	= @pEXTERNAL_REFERENCE2,
			@EXTERNAL_REFERENCE3				NVARCHAR(250)	= @pEXTERNAL_REFERENCE3,
			@EXTERNAL_REFERENCE4				NVARCHAR(250)	= @pEXTERNAL_REFERENCE4,
			@EXTERNAL_REFERENCE5				NVARCHAR(250)	= @pEXTERNAL_REFERENCE5;

	-- declare runtime vars
    DECLARE @MY_PROCEDURE_NAME					NVARCHAR(128)	= OBJECT_NAME(@@PROCID);
	DECLARE	@ACTIVE_FLAG						BIT				= 1,
			@INACTIVE_FLAG						BIT				= 0,
			@ROW_COUNT0							SMALLINT		= 0,
			@ROW_COUNT1							SMALLINT		= 0,
			@ROW_COUNT2							SMALLINT		= 0,
			@ROW_COUNTER1						SMALLINT		= 0,
			@ROW_COUNTER2						SMALLINT		= 0,
			@ROW_COUNT_SUM1						SMALLINT		= 0,
			@PLACEHOLDER_NAME					NVARCHAR(50)	= NULL,
			@PLACEHOLDER						NVARCHAR(MAX)	= NULL,
			@PLACEHOLDER_TYPE					NVARCHAR(10)	= 'CONST',
			@TBDEX_IN_GUID						BIGINT			= 0,
			@TBDEX_IN_PROFILE_ID				SMALLINT		= 0,
			@TBDEX_IN_BATCH_ID					NVARCHAR(25)	= NULL,
			@TBDEX_IN_REFERENCE1				NVARCHAR(150)	= NULL,
			@TBDEX_IN_REFERENCE2				NVARCHAR(150)	= NULL,
			@TBDEX_IN_REFERENCE3				NVARCHAR(150)	= NULL,
			@TBDEX_IN_REFERENCE4				NVARCHAR(150)	= NULL,
			@TBDEX_IN_REFERENCE5				NVARCHAR(150)	= NULL,
			@TBDEX_IN_EXTERNAL_REFERENCE1		NVARCHAR(250)	= NULL,
			@TBDEX_IN_EXTERNAL_REFERENCE2		NVARCHAR(250)	= NULL,
			@TBDEX_IN_EXTERNAL_REFERENCE3		NVARCHAR(250)	= NULL,
			@TBDEX_IN_EXTERNAL_REFERENCE4		NVARCHAR(250)	= NULL,
			@TBDEX_IN_EXTERNAL_REFERENCE5		NVARCHAR(250)	= NULL,
			@TBDEX_IN_TEST_MODE					BIT  			= 0,
			@TBDEX_IN_ASYNC_MODE				BIT  			= 0,
			@TBDEX_IN_COMMENT					NVARCHAR(150)	= NULL,
			@TBDEX_IN_ADDED_WHO					NVARCHAR(50)	= NULL,
			@TBDEX_IN_ADDED_WHEN				DATETIME		= NULL,
			@TBDEX_TMP_QUERY_GUID				BIGINT			= 0,
			@TBDEX_TMP_QUERY_PROFILE_ID			SMALLINT		= 0,
			@TBDEX_TMP_QUERY_BATCH_ID			NVARCHAR(25)	= NULL,
			@TBDEX_TMP_QUERY_QUERY				NVARCHAR(MAX)	= NULL,
			@TBDEX_TMP_QUERY_COMMENT			NVARCHAR(150)	= NULL,
			@TBDEX_TMP_QUERY_ADDED_WHO			NVARCHAR(50)	= NULL,
			@TBDEX_TMP_QUERY_ADDED_WHEN			DATETIME		= NULL,
			@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 - @EXPORT_NUMBER_SCHEMA:   ' + CONVERT(NVARCHAR(50),ISNULL(@EXPORT_NUMBER_SCHEMA,'<NO-VALUE>'));
	PRINT 'PARAMETER31 - @DEFAULT_DATA_SEPARATOR: ' + CONVERT(NVARCHAR(50),ISNULL(@DEFAULT_DATA_SEPARATOR,'<NO-VALUE>'));
	PRINT 'PARAMETER32 - @REFERENCE1:             ' + CONVERT(NVARCHAR(50),ISNULL(@REFERENCE1,'<NO-VALUE>'));
	PRINT 'PARAMETER33 - @REFERENCE2:             ' + CONVERT(NVARCHAR(50),ISNULL(@REFERENCE2,'<NO-VALUE>'));
	PRINT 'PARAMETER34 - @REFERENCE3:             ' + CONVERT(NVARCHAR(50),ISNULL(@REFERENCE3,'<NO-VALUE>'));
	PRINT 'PARAMETER35 - @REFERENCE4:             ' + CONVERT(NVARCHAR(50),ISNULL(@REFERENCE4,'<NO-VALUE>'));
	PRINT 'PARAMETER36 - @REFERENCE5:             ' + CONVERT(NVARCHAR(50),ISNULL(@REFERENCE5,'<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;
	-----------------------------------------------------------------------------------------------------------------------------------


	--==================================================-- Count main sql querys --==================================================--
	SET @ROW_COUNT1 = ISNULL((SELECT COUNT(*) FROM [TBDEX_TMP_PROCESS_REFERENCES] WITH (SNAPSHOT) 
							  WHERE [PROFILE_ID]=@PROFILE_ID AND [BATCH_ID]=@BATCH_ID AND [ADDED_WHO]=@BATCH_CREATOR),0);	
	SET @ROW_COUNT2 = ISNULL((SELECT COUNT(*) FROM [TBDEX_TMP_QUERY] WITH (SNAPSHOT) 
							  WHERE [GUID] > 0 AND [PROFILE_ID]=@PROFILE_ID AND [BATCH_ID]=@BATCH_ID AND [QUERY] IS NOT NULL AND [ADDED_WHO]=@BATCH_CREATOR),0);
	SET @ROW_COUNT_SUM1 = (@ROW_COUNT1 + @ROW_COUNT2);

	IF (@ROW_COUNT_SUM1 < 2) BEGIN

		--====================================================-- exception / error --====================================================--
		SET @RETURN_STATUS		= 50007;
		SET @RETURN_STATUS_TEXT = concat('','');
		SET @RETURN_ERROR_TEXT	= concat('Insufficient rows in [TBDEX_TMP_PROCESS_REFERENCES] (COUNT: ',CONVERT(NVARCHAR(50),@ROW_COUNT1),') and/or [TBDEX_TMP_QUERY] (COUNT: ',CONVERT(NVARCHAR(50),@ROW_COUNT2),') found!');

        THROW @RETURN_STATUS,@RETURN_ERROR_TEXT,1;
		-----------------------------------------------------------------------------------------------------------------------------------

	END; ELSE BEGIN

		--======================================================-- Output result --======================================================--
		SET @RETURN_STATUS		= 0;
		SET @RETURN_STATUS_TEXT = concat('Found the sum of: ',CONVERT(NVARCHAR(50),@ROW_COUNT_SUM1),' in the tables [TBDEX_TMP_PROCESS_REFERENCES] and [TBDEX_TMP_QUERY].');
		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;
		-----------------------------------------------------------------------------------------------------------------------------------

	END;

	--==================================================-- Handle placeholders  --==================================================--
	DECLARE CURSOR_TBDEX_TMP_PROCESS_REFERENCES CURSOR
	LOCAL FAST_FORWARD FOR 
		SELECT	[GUID]					as 'TBDEX_IN_GUID',					[PROFILE_ID]			as 'TBDEX_IN_PROFILE_ID',			
				[BATCH_ID]				as 'TBDEX_IN_BATCH_ID',
				[REFERENCE1]			as 'TBDEX_IN_REFERENCE1',			[REFERENCE2]			as 'TBDEX_IN_REFERENCE2',			
				[REFERENCE3]			as 'TBDEX_IN_REFERENCE3',			[REFERENCE4]			as 'TBDEX_IN_REFERENCE4',			
				[REFERENCE5]			as 'TBDEX_IN_REFERENCE5',
				[EXTERNAL_REFERENCE1]	as 'TBDEX_IN_EXTERNAL_REFERENCE1',	[EXTERNAL_REFERENCE2]	as 'TBDEX_IN_EXTERNAL_REFERENCE2',	
				[EXTERNAL_REFERENCE3]	as 'TBDEX_IN_EXTERNAL_REFERENCE3',	[EXTERNAL_REFERENCE4]	as 'TBDEX_IN_EXTERNAL_REFERENCE4',	
				[EXTERNAL_REFERENCE5]	as 'TBDEX_IN_EXTERNAL_REFERENCE5',
				--[TEST_MODE]				as 'TBDEX_IN_TEST_MODE',			[ASYNC_MODE]			as 'TBDEX_IN_ASYNC_MODE',			
				[COMMENT]				as 'TBDEX_IN_COMMENT',
				[ADDED_WHO]				as 'TBDEX_IN_ADDED_WHO',			[ADDED_WHEN]			as 'TBDEX_IN_ADDED_WHEN'
		FROM	[TBDEX_TMP_PROCESS_REFERENCES] WITH (SNAPSHOT)
		WHERE	[PROFILE_ID]=@PROFILE_ID AND [BATCH_ID]=@BATCH_ID AND [ADDED_WHO]=@BATCH_CREATOR
		ORDER BY [GUID] ASC;

	OPEN CURSOR_TBDEX_TMP_PROCESS_REFERENCES 
	FETCH NEXT FROM CURSOR_TBDEX_TMP_PROCESS_REFERENCES INTO @TBDEX_IN_GUID,@TBDEX_IN_PROFILE_ID,@TBDEX_IN_BATCH_ID,@TBDEX_IN_REFERENCE1,@TBDEX_IN_REFERENCE2,@TBDEX_IN_REFERENCE3,@TBDEX_IN_REFERENCE4,@TBDEX_IN_REFERENCE5,@TBDEX_IN_EXTERNAL_REFERENCE1,@TBDEX_IN_EXTERNAL_REFERENCE2,@TBDEX_IN_EXTERNAL_REFERENCE3,@TBDEX_IN_EXTERNAL_REFERENCE4,@TBDEX_IN_EXTERNAL_REFERENCE5,@TBDEX_IN_COMMENT,@TBDEX_IN_ADDED_WHO,@TBDEX_IN_ADDED_WHEN;
	WHILE @@FETCH_STATUS = 0
		BEGIN

			SET @ROW_COUNTER1 += 1; -- Increase loop counter
			IF ((@ROW_COUNTER1 = 1) OR (@ROW_COUNTER1 = @ROW_COUNT1) OR (@ROW_COUNTER1 % 100 = 0)) BEGIN
				PRINT concat('This is cursor (CURSOR_TBDEX_TMP_PROCESS_REFERENCES), loop nr: ',@ROW_COUNTER1);
			END;

			--======================================================-- Output result --======================================================--
			SET @RETURN_STATUS		= 0;
			SET @RETURN_STATUS_TEXT = concat('Proccessing table: [TBDEX_TMP_PROCESS_REFERENCES] - Row ',CONVERT(NVARCHAR(50),@ROW_COUNTER1),' of ',CONVERT(NVARCHAR(50),@ROW_COUNT1));
			SET @RETURN_ERROR_TEXT	= concat('','');

			IF ((@ROW_COUNTER1 = 1) OR (@ROW_COUNTER1 = @ROW_COUNT1) OR (@ROW_COUNTER1 % 100 = 0)) BEGIN
				PRINT 'INFO: ' + @RETURN_STATUS_TEXT;
			END;
			-----------------------------------------------------------------------------------------------------------------------------------

			--===================================================-- 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;
			-----------------------------------------------------------------------------------------------------------------------------------

			--===================================================-- loop vars from row --====================================================--
			PRINT '[GUID]:       ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_GUID,'<NO-VALUE>'));
			PRINT '[PROFILE_ID]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_PROFILE_ID,'<NO-VALUE>'));
			PRINT '[BATCH_ID]:   ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_BATCH_ID,'<NO-VALUE>'));
			PRINT '[REFERENCE1]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_REFERENCE1,'<NO-VALUE>'));
			PRINT '[REFERENCE2]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_REFERENCE2,'<NO-VALUE>'));
			PRINT '[REFERENCE3]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_REFERENCE3,'<NO-VALUE>'));
			PRINT '[REFERENCE4]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_REFERENCE4,'<NO-VALUE>'));
			PRINT '[REFERENCE5]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_REFERENCE5,'<NO-VALUE>'));
			PRINT '[TEST_MODE]:  ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_TEST_MODE,'<NO-VALUE>'));
			PRINT '[ASYNC_MODE]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_ASYNC_MODE,'<NO-VALUE>'));
			PRINT '[COMMENT]:    ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_COMMENT,'<NO-VALUE>'));
			PRINT '[ADDED_WHO]:  ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_ADDED_WHO,'<NO-VALUE>'));
			PRINT '[ADDED_WHEN]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_IN_ADDED_WHEN,'<NO-VALUE>'));
			-----------------------------------------------------------------------------------------------------------------------------------

			SET @ROW_COUNTER2 = 0;

			DECLARE CURSOR_TBDEX_TMP_QUERY CURSOR
			LOCAL FAST_FORWARD FOR
				SELECT [GUID]	    as 'TBDEX_TMP_QUERY_GUID',	[PROFILE_ID] as 'TBDEX_TMP_QUERY_PROFILE_ID', [BATCH_ID]   as 'TBDEX_TMP_QUERY_BATCH_ID',  
					   [QUERY]      as 'TBDEX_TMP_QUERY_QUERY', [COMMENT]    as 'TBDEX_TMP_QUERY_COMMENT',    [ADDED_WHO]  as 'TBDEX_TMP_QUERY_ADDED_WHO',
					   [ADDED_WHEN] as 'TBDEX_TMP_QUERY_ADDED_WHEN'
				FROM   [TBDEX_TMP_QUERY] WITH (SNAPSHOT)
				WHERE  [PROFILE_ID]=@PROFILE_ID AND [BATCH_ID]=@BATCH_ID
				ORDER BY [GUID] ASC;

			OPEN CURSOR_TBDEX_TMP_QUERY 
			FETCH NEXT FROM CURSOR_TBDEX_TMP_QUERY INTO @TBDEX_TMP_QUERY_GUID,@TBDEX_TMP_QUERY_PROFILE_ID,@TBDEX_TMP_QUERY_BATCH_ID,@TBDEX_TMP_QUERY_QUERY,@TBDEX_TMP_QUERY_COMMENT,@TBDEX_TMP_QUERY_ADDED_WHO,@TBDEX_TMP_QUERY_ADDED_WHEN;
			WHILE @@FETCH_STATUS = 0
				BEGIN

					SET @ROW_COUNTER2 += 1; -- Increase loop counter
					IF ((@ROW_COUNTER2 = 1) OR (@ROW_COUNTER2 = @ROW_COUNT2) OR (@ROW_COUNTER2 % 100 = 0)) BEGIN
						PRINT concat('This is cursor (CURSOR_TBDEX_TMP_QUERY), loop nr: ',@ROW_COUNTER2);
					END;

					--======================================================-- Output result --======================================================--
					SET @RETURN_STATUS		= 0;
					SET @RETURN_STATUS_TEXT = concat('Proccessing table: [TBDEX_TMP_QUERY] - Row ',CONVERT(NVARCHAR(50),@ROW_COUNTER2),' of ',CONVERT(NVARCHAR(50),@ROW_COUNT2));
					SET @RETURN_ERROR_TEXT	= concat('','');

					IF ((@ROW_COUNTER2 = 1) OR (@ROW_COUNTER2 = @ROW_COUNT2) OR (@ROW_COUNTER2 % 100 = 0)) BEGIN
						PRINT 'INFO: ' + @RETURN_STATUS_TEXT;
					END;
					-----------------------------------------------------------------------------------------------------------------------------------

					--===================================================-- 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;
					-----------------------------------------------------------------------------------------------------------------------------------

					--===================================================-- loop vars from row --====================================================--
					PRINT '[GUID]:       ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_QUERY_GUID,'<NO-VALUE>'));
					PRINT '[PROFILE_ID]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_QUERY_PROFILE_ID,'<NO-VALUE>'));
					PRINT '[BATCH_ID]:   ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_QUERY_BATCH_ID,'<NO-VALUE>'));
					PRINT '[QUERY]:      ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_QUERY_QUERY,'<NO-VALUE>'));
					PRINT '[COMMENT]:    ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_QUERY_COMMENT,'<NO-VALUE>'));
					PRINT '[ADDED_WHO]:  ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_QUERY_ADDED_WHO,'<NO-VALUE>'));
					PRINT '[ADDED_WHEN]: ' + CONVERT(NVARCHAR(50),ISNULL(@TBDEX_TMP_QUERY_ADDED_WHEN,'<NO-VALUE>'));
					-----------------------------------------------------------------------------------------------------------------------------------


					--==================================================-- Handle placeholders  --==================================================--
					PRINT '';
					PRINT 'Replacing placeholders in query...';
		
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%PROFILE_ID%',ISNULL(@PROFILE_ID,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%TEST_MODE%',ISNULL(@TEST_MODE,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%ASYNC_MODE%',ISNULL(@ASYNC_MODE,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%BATCH_ID%',ISNULL(@BATCH_ID,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%BATCH_CREATOR%',ISNULL(@BATCH_CREATOR,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%LANGUAGE%',ISNULL(@LANGUAGE,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%LOG_LEVEL%',ISNULL(@LOG_LEVEL,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%START_QUERY%',ISNULL(@START_QUERY,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%START_QUERY_VAR1%',ISNULL(@START_QUERY_VAR1,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%START_QUERY_VAR2%',ISNULL(@START_QUERY_VAR2,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%START_QUERY_VAR3%',ISNULL(@START_QUERY_VAR3,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%START_QUERY_VAR4%',ISNULL(@START_QUERY_VAR4,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%START_QUERY_VAR5%',ISNULL(@START_QUERY_VAR5,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%FORCE%',ISNULL(@FORCE,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%MY_PROCEDURE_NAME%',ISNULL(@MY_PROCEDURE_NAME,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%PROFILE_NAME%',ISNULL(@PROFILE_NAME,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%MANDANTOR%',ISNULL(@MANDANTOR,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%EXPORT_TYPE%',ISNULL(@EXPORT_TYPE,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%REFERENCE1%',ISNULL(@TBDEX_IN_REFERENCE1,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%REFERENCE2%',ISNULL(@TBDEX_IN_REFERENCE2,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%REFERENCE3%',ISNULL(@TBDEX_IN_REFERENCE3,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%REFERENCE4%',ISNULL(@TBDEX_IN_REFERENCE4,''));
					SET @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,'%REFERENCE5%',ISNULL(@TBDEX_IN_REFERENCE5,''));

					DECLARE CURSOR_PLACEHOLDER CURSOR
					LOCAL FAST_FORWARD FOR 
						SELECT [PLACEHOLDER_NAME] as 'Placeholder_Name', [PLACEHOLDER] as 'Placeholder'
						FROM   [TBDEX_CFG_RESOURCES] (NOLOCK) 
						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 @TBDEX_TMP_QUERY_QUERY = REPLACE(@TBDEX_TMP_QUERY_QUERY,concat('%',@PLACEHOLDER_NAME,'%'),ISNULL(@PLACEHOLDER,''));

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

					--=================================================-- Check first sql query --===================================================--
					IF (LEN(@TBDEX_TMP_QUERY_QUERY) < 15) BEGIN

						--====================================================-- exception / error --====================================================--
						SET @RETURN_STATUS		= 50001;
						SET @RETURN_STATUS_TEXT = concat('Wrong call or configuraton!','');
						SET @RETURN_ERROR_TEXT	= concat('Found no valid querys (query needs to be 15 chars or more)!','');

						THROW @RETURN_STATUS,@RETURN_ERROR_TEXT,1;
						-----------------------------------------------------------------------------------------------------------------------------------

					END; ELSE BEGIN

						--======================================================-- Output result --======================================================--
						SET @RETURN_STATUS		= 0;
						SET @RETURN_STATUS_TEXT = concat('Found valid "@TBDEX_TMP_QUERY_QUERY" query: ',char(13),@TBDEX_TMP_QUERY_QUERY);
						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;
						-----------------------------------------------------------------------------------------------------------------------------------

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

					--=================================================-- build insert statement --==================================================--
					SET @TBDEX_TMP_QUERY_QUERY =  CONCAT(char(13),'INSERT INTO ',@TBDEX_TMP_TABLE_SCHEMA_AND_NAME,
														 '(',@pTBDEX_TMP_TABLE_COLUMNS,')',char(13),
														 @TBDEX_TMP_QUERY_QUERY);
					-----------------------------------------------------------------------------------------------------------------------------------

					--======================================================-- Output result --======================================================--
					SET @RETURN_STATUS		= 0;
					SET @RETURN_STATUS_TEXT = concat('The complete "@TBDEX_TMP_QUERY_QUERY" query: ',char(13),@TBDEX_TMP_QUERY_QUERY);
					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			= @TBDEX_TMP_QUERY_QUERY,
														@pRETURN_STATUS = 50052,
														@pQUERY_NAME	= N'@TBDEX_TMP_QUERY_QUERY';
					-----------------------------------------------------------------------------------------------------------------------------------

					--==================================================-- run insert statement --===================================================--
					EXEC @RETURN_STATUS = sp_executesql @TBDEX_TMP_QUERY_QUERY;

					IF (@RETURN_STATUS <> 0) BEGIN

						--====================================================-- exception / error --====================================================--
						SET @RETURN_STATUS		= @RETURN_STATUS;
						SET @RETURN_STATUS_TEXT = concat('Check previous error text!','');
						SET @RETURN_ERROR_TEXT	= concat('Error occurred in [PRDEX_GET_INPUT_CONTENT] / @TBDEX_TMP_QUERY_QUERY!','');
		
						THROW @RETURN_STATUS,@RETURN_ERROR_TEXT,1;
						-----------------------------------------------------------------------------------------------------------------------------------

					END; ELSE BEGIN

						--======================================================-- Output result --======================================================--
						SET @RETURN_STATUS		= 0;
						SET @RETURN_STATUS_TEXT = concat('Procedure [PRDEX_GET_INPUT_CONTENT] / @TBDEX_TMP_QUERY_QUERY worked just fine!','');
						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;
						-----------------------------------------------------------------------------------------------------------------------------------

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

					FETCH NEXT FROM CURSOR_TBDEX_TMP_QUERY INTO @TBDEX_TMP_QUERY_GUID,@TBDEX_TMP_QUERY_PROFILE_ID,@TBDEX_TMP_QUERY_BATCH_ID,@TBDEX_TMP_QUERY_QUERY,@TBDEX_TMP_QUERY_COMMENT,@TBDEX_TMP_QUERY_ADDED_WHO,@TBDEX_TMP_QUERY_ADDED_WHEN;
				END
			CLOSE CURSOR_TBDEX_TMP_QUERY;
			DEALLOCATE CURSOR_TBDEX_TMP_QUERY;
			-----------------------------------------------------------------------------------------------------------------------------------

			FETCH NEXT FROM CURSOR_TBDEX_TMP_PROCESS_REFERENCES INTO @TBDEX_IN_GUID,@TBDEX_IN_PROFILE_ID,@TBDEX_IN_BATCH_ID,@TBDEX_IN_REFERENCE1,@TBDEX_IN_REFERENCE2,@TBDEX_IN_REFERENCE3,@TBDEX_IN_REFERENCE4,@TBDEX_IN_REFERENCE5,@TBDEX_IN_EXTERNAL_REFERENCE1,@TBDEX_IN_EXTERNAL_REFERENCE2,@TBDEX_IN_EXTERNAL_REFERENCE3,@TBDEX_IN_EXTERNAL_REFERENCE4,@TBDEX_IN_EXTERNAL_REFERENCE5,@TBDEX_IN_COMMENT,@TBDEX_IN_ADDED_WHO,@TBDEX_IN_ADDED_WHEN;
		END
	CLOSE CURSOR_TBDEX_TMP_PROCESS_REFERENCES;
	DEALLOCATE CURSOR_TBDEX_TMP_PROCESS_REFERENCES;
	-----------------------------------------------------------------------------------------------------------------------------------

	--======================================================-- 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