SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

-- [PRREC_UPDATE_OBJECT]
-- =================================================================
-- Updates one record in a given Entity with validation of allowed values
-- Setting a parameter with NULL = keeps the current value
-- 
-- Returns: INT Value - 0 = Everything worked well
-- =================================================================
-- 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:    22.12.2025 / MK
-- Version Date / Editor:     22.12.2025 / MK
-- Version Number:            1.0.0.0
-- =================================================================
-- History:
-- 22.12.2025 / MK - First Version

CREATE OR ALTER PROCEDURE [dbo].[PRREC_UPDATE_OBJECT] (
	@pENTITY									NVARCHAR(25)	= 'ACTION',		-- Target entity: ACTION, ENDPOINT, ENDPOINT_AUTH, ENDPOINT_PARAMS, PROFILE, RESULT
	@pGUID										BIGINT			= NULL,			-- Target GUID to update (required)
	@pCHANGED_WHO								NVARCHAR(50)	= NULL,			-- User or function of the Update
	@pCHANGED_WHEN								DATETIME		= NULL,			-- Datetime of the Update
	@pACTION_PROFILE_ID							BIGINT			= NULL,			-- ACTION: New profile GUID
	@pACTION_ACTIVE								BIT				= NULL,			-- ACTION: New active flag
	@pACTION_SEQUENCE							TINYINT			= NULL,			-- ACTION: New sequence/order
	@pACTION_ENDPOINT_ID						BIGINT			= NULL,			-- ACTION: New endpoint GUID
	@pACTION_ENDPOINT_AUTH_ID					BIGINT			= NULL,			-- ACTION: New endpoint auth GUID
	@pACTION_ENDPOINT_PARAMS_ID					SMALLINT		= NULL,			-- ACTION: New endpoint params group GUID
	@pACTION_SQL_CONNECTION_ID					SMALLINT		= NULL,			-- ACTION: New SQL connection GUID
	@pACTION_TYPE_ID							TINYINT			= NULL,			-- ACTION: New HTTP method id (0..9)
	@pACTION_PRE_SQL							NVARCHAR(MAX)	= NULL,			-- ACTION: New SQL before request
	@pACTION_HEADER_SQL							NVARCHAR(MAX)	= NULL,			-- ACTION: New SQL for headers
	@pACTION_BODY_SQL							NVARCHAR(MAX)	= NULL,			-- ACTION: New SQL for body
	@pACTION_POST_SQL							NVARCHAR(MAX)	= NULL,			-- ACTION: New SQL after request
	@pACTION_ERROR_ACTION_ID					TINYINT			= NULL,			-- ACTION: New error behavior (0=STOP,1=CONTINUE)
	@pENDPOINT_ACTIVE							BIT				= NULL,			-- ENDPOINT: New active flag
	@pENDPOINT_DESCRIPTION						NVARCHAR(250)	= NULL,			-- ENDPOINT: New description
	@pENDPOINT_URI								NVARCHAR(2000)	= NULL,			-- ENDPOINT: New target URI
	@pENDPOINT_AUTH_ACTIVE						BIT				= NULL,			-- ENDPOINT_AUTH: New active flag
	@pENDPOINT_AUTH_DESCRIPTION					NVARCHAR(250)	= NULL,			-- ENDPOINT_AUTH: New description
	@pENDPOINT_AUTH_TYPE_ID						TINYINT			= NULL,			-- ENDPOINT_AUTH: New auth type id (0..9)
	@pENDPOINT_AUTH_API_KEY						NVARCHAR(150)	= NULL,			-- ENDPOINT_AUTH: New API key name/header
	@pENDPOINT_AUTH_API_VALUE					NVARCHAR(150)	= NULL,			-- ENDPOINT_AUTH: New API key value
	@pENDPOINT_AUTH_API_KEY_ADD_TO_ID			BIT				= NULL,			-- ENDPOINT_AUTH: New API key placement (0=HEADER,1=QUERY)
	@pENDPOINT_AUTH_TOKEN						NVARCHAR(150)	= NULL,			-- ENDPOINT_AUTH: New bearer/JWT token
	@pENDPOINT_AUTH_USERNAME					NVARCHAR(100)	= NULL,			-- ENDPOINT_AUTH: New username
	@pENDPOINT_AUTH_PASSWORD					NVARCHAR(100)	= NULL,			-- ENDPOINT_AUTH: New password
	@pENDPOINT_AUTH_DOMAIN						NVARCHAR(50)	= NULL,			-- ENDPOINT_AUTH: New domain (NTLM)
	@pENDPOINT_AUTH_WORKSTATION					NVARCHAR(50)	= NULL,			-- ENDPOINT_AUTH: New workstation (NTLM)
	@pENDPOINT_PARAMS_ACTIVE					BIT				= NULL,			-- ENDPOINT_PARAMS: New active flag
	@pENDPOINT_PARAMS_DESCRIPTION				NVARCHAR(250)	= NULL,			-- ENDPOINT_PARAMS: New description
	@pENDPOINT_PARAMS_GROUP_ID					SMALLINT		= NULL,			-- ENDPOINT_PARAMS: New parameter group id
	@pENDPOINT_PARAMS_SEQUENCE					TINYINT			= NULL,			-- ENDPOINT_PARAMS: New sequence/order
	@pENDPOINT_PARAMS_KEY						NVARCHAR(150)	= NULL,			-- ENDPOINT_PARAMS: New parameter key/name
	@pENDPOINT_PARAMS_VALUE						NVARCHAR(150)	= NULL,			-- ENDPOINT_PARAMS: New parameter value
	@pPROFILE_ACTIVE							BIT				= NULL,			-- PROFILE: New active flag
	@pPROFILE_TYPE_ID							TINYINT			= NULL,			-- PROFILE: New protocol type id (1=HTTP,2=HTTPS)
	@pPROFILE_MANDANTOR							NVARCHAR(50)	= NULL,			-- PROFILE: New tenant/mandator
	@pPROFILE_NAME								NVARCHAR(50)	= NULL,			-- PROFILE: New profile name
	@pPROFILE_DESCRIPTION						NVARCHAR(250)	= NULL,			-- PROFILE: New description
	@pPROFILE_LOG_LEVEL_ID						TINYINT			= NULL,			-- PROFILE: New log level id (0..6)
	@pPROFILE_LANGUAGE_ID						SMALLINT		= NULL,			-- PROFILE: New language id (1031/1033)
	@pPROFILE_FIRST_RUN							DATETIME		= NULL,			-- PROFILE: New first run timestamp (RUN_PROFILE)
	@pPROFILE_LAST_RUN							DATETIME		= NULL,			-- PROFILE: New last run timestamp (RUN_PROFILE)
	@pPROFILE_LAST_RESULT						NVARCHAR(250)	= NULL,			-- PROFILE: New last result text (RUN_PROFILE)
	@pRESULT_ACTION_ID							BIGINT			= NULL,			-- RESULT: New action GUID
	@pRESULT_STATUS_ID							SMALLINT		= NULL,			-- RESULT: New HTTP status code
	@pRESULT_TYPE_ID							TINYINT			= NULL,     	-- RESULT: 1 = Pre; 2 = Main; 3 = Post
	@pRESULT_HEADER								NVARCHAR(MAX)	= NULL,			-- RESULT: New response header
	@pRESULT_BODY								NVARCHAR(MAX)	= NULL,			-- RESULT: New response body
	@pRESULT_INFO								NVARCHAR(MAX)	= NULL,     	-- RESULT: Optional Response info text
	@pRESULT_ERROR								NVARCHAR(MAX)	= NULL      	-- RESULT: Optional Response error text
)
AS
BEGIN

	SET NOCOUNT ON;
	SET XACT_ABORT ON;

	-- declare new vars because of parameter sniffing
	DECLARE @ENTITY								NVARCHAR(25)	= UPPER(LTRIM(RTRIM(ISNULL(@pENTITY, 'ACTION')))),
			@GUID								BIGINT			= @pGUID,
			@CHANGED_WHO						NVARCHAR(50)	= ISNULL(@pCHANGED_WHO, OBJECT_NAME(@@PROCID)),
			@CHANGED_WHEN						DATETIME		= ISNULL(@pCHANGED_WHEN,GetDate()),
			@ACTION_PROFILE_ID					BIGINT			= @pACTION_PROFILE_ID,
			@ACTION_ACTIVE						BIT				= @pACTION_ACTIVE,
			@ACTION_SEQUENCE					TINYINT			= @pACTION_SEQUENCE,
			@ACTION_ENDPOINT_ID					BIGINT			= @pACTION_ENDPOINT_ID,
			@ACTION_ENDPOINT_AUTH_ID			BIGINT			= @pACTION_ENDPOINT_AUTH_ID,
			@ACTION_ENDPOINT_PARAMS_ID			SMALLINT		= @pACTION_ENDPOINT_PARAMS_ID,
			@ACTION_SQL_CONNECTION_ID			SMALLINT		= @pACTION_SQL_CONNECTION_ID,
			@ACTION_TYPE_ID						TINYINT			= @pACTION_TYPE_ID,
			@ACTION_PRE_SQL						NVARCHAR(MAX)	= @pACTION_PRE_SQL,
			@ACTION_HEADER_SQL					NVARCHAR(MAX)	= @pACTION_HEADER_SQL,
			@ACTION_BODY_SQL					NVARCHAR(MAX)	= @pACTION_BODY_SQL,
			@ACTION_POST_SQL					NVARCHAR(MAX)	= @pACTION_POST_SQL,
			@ACTION_ERROR_ACTION_ID				TINYINT			= @pACTION_ERROR_ACTION_ID,
			@ENDPOINT_ACTIVE					BIT				= @pENDPOINT_ACTIVE,
			@ENDPOINT_DESCRIPTION				NVARCHAR(250)	= @pENDPOINT_DESCRIPTION,
			@ENDPOINT_URI						NVARCHAR(2000)	= @pENDPOINT_URI,
			@ENDPOINT_AUTH_ACTIVE				BIT				= @pENDPOINT_AUTH_ACTIVE,
			@ENDPOINT_AUTH_DESCRIPTION			NVARCHAR(250)	= @pENDPOINT_AUTH_DESCRIPTION,
			@ENDPOINT_AUTH_TYPE_ID				TINYINT			= @pENDPOINT_AUTH_TYPE_ID,
			@ENDPOINT_AUTH_API_KEY				NVARCHAR(150)	= @pENDPOINT_AUTH_API_KEY,
			@ENDPOINT_AUTH_API_VALUE			NVARCHAR(150)	= @pENDPOINT_AUTH_API_VALUE,
			@ENDPOINT_AUTH_API_KEY_ADD_TO_ID	BIT				= @pENDPOINT_AUTH_API_KEY_ADD_TO_ID,
			@ENDPOINT_AUTH_TOKEN				NVARCHAR(150)	= @pENDPOINT_AUTH_TOKEN,
			@ENDPOINT_AUTH_USERNAME				NVARCHAR(100)	= @pENDPOINT_AUTH_USERNAME,
			@ENDPOINT_AUTH_PASSWORD				NVARCHAR(100)	= @pENDPOINT_AUTH_PASSWORD,
			@ENDPOINT_AUTH_DOMAIN				NVARCHAR(50)	= @pENDPOINT_AUTH_DOMAIN,
			@ENDPOINT_AUTH_WORKSTATION			NVARCHAR(50)	= @pENDPOINT_AUTH_WORKSTATION,
			@ENDPOINT_PARAMS_ACTIVE				BIT				= @pENDPOINT_PARAMS_ACTIVE,
			@ENDPOINT_PARAMS_DESCRIPTION		NVARCHAR(250)	= @pENDPOINT_PARAMS_DESCRIPTION,
			@ENDPOINT_PARAMS_GROUP_ID			SMALLINT		= @pENDPOINT_PARAMS_GROUP_ID,
			@ENDPOINT_PARAMS_SEQUENCE			TINYINT			= @pENDPOINT_PARAMS_SEQUENCE,
			@ENDPOINT_PARAMS_KEY				NVARCHAR(150)	= @pENDPOINT_PARAMS_KEY,
			@ENDPOINT_PARAMS_VALUE				NVARCHAR(150)	= @pENDPOINT_PARAMS_VALUE,
			@PROFILE_ACTIVE						BIT				= @pPROFILE_ACTIVE,
			@PROFILE_TYPE_ID					TINYINT			= @pPROFILE_TYPE_ID,
			@PROFILE_MANDANTOR					NVARCHAR(50)	= @pPROFILE_MANDANTOR,
			@PROFILE_NAME						NVARCHAR(50)	= @pPROFILE_NAME,
			@PROFILE_DESCRIPTION				NVARCHAR(250)	= @pPROFILE_DESCRIPTION,
			@PROFILE_LOG_LEVEL_ID				TINYINT			= @pPROFILE_LOG_LEVEL_ID,
			@PROFILE_LANGUAGE_ID				SMALLINT		= @pPROFILE_LANGUAGE_ID,
			@PROFILE_FIRST_RUN					DATETIME		= @pPROFILE_FIRST_RUN,
			@PROFILE_LAST_RUN					DATETIME		= @pPROFILE_LAST_RUN,
			@PROFILE_LAST_RESULT				NVARCHAR(250)	= @pPROFILE_LAST_RESULT,
			@RESULT_ACTION_ID					BIGINT			= @pRESULT_ACTION_ID,
			@RESULT_STATUS_ID					SMALLINT		= @pRESULT_STATUS_ID,
			@RESULT_TYPE_ID						TINYINT			= @pRESULT_TYPE_ID,
			@RESULT_HEADER						NVARCHAR(MAX)	= @pRESULT_HEADER,
			@RESULT_BODY						NVARCHAR(MAX)	= @pRESULT_BODY,
			@RESULT_INFO						NVARCHAR(MAX)	= @pRESULT_INFO,
			@RESULT_ERROR						NVARCHAR(MAX)	= @pRESULT_ERROR;

	-- declare runtime vars
	DECLARE @MY_PROCEDURE_NAME					NVARCHAR(128)	= OBJECT_NAME(@@PROCID);
	DECLARE @RETURN_STATUS						INT				= 0,
			@RETURN_STATUS_TEXT					NVARCHAR(MAX)	= 'START PROCEDURE [' + @MY_PROCEDURE_NAME + '] @ ' + CONVERT(VARCHAR(50), GETDATE(), 120);
	----------------------------------------------------------------------------------------------------------------------------

	--=================================================-- Output parameters --================================================--
	PRINT '====================================================================================================';
	PRINT @RETURN_STATUS_TEXT;
	PRINT '';
	PRINT 'PARAMETER01 - @ENTITY:                          ' + ISNULL(CONVERT(NVARCHAR(25),   @ENTITY), 							'NULL');
	PRINT 'PARAMETER02 - @GUID:                            ' + ISNULL(CONVERT(NVARCHAR(30),   @GUID), 								'NULL');
	PRINT 'PARAMETER03 - @CHANGED_WHO:                     ' + ISNULL(CONVERT(NVARCHAR(50),   @CHANGED_WHO), 						'NULL');
	PRINT 'PARAMETER04 - @CHANGED_WHEN:                    ' + ISNULL(CONVERT(NVARCHAR(50),   @CHANGED_WHEN), 						'NULL');
	PRINT 'PARAMETER05 - @ACTION_PROFILE_ID:               ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_PROFILE_ID), 					'NULL');
	PRINT 'PARAMETER06 - @ACTION_ACTIVE:                   ' + ISNULL(CONVERT(NVARCHAR(1),    @ACTION_ACTIVE), 						'NULL');
	PRINT 'PARAMETER07 - @ACTION_SEQUENCE:                 ' + ISNULL(CONVERT(NVARCHAR(3),    @ACTION_SEQUENCE), 					'NULL');
	PRINT 'PARAMETER08 - @ACTION_ENDPOINT_ID:              ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_ENDPOINT_ID), 				'NULL');
	PRINT 'PARAMETER09 - @ACTION_ENDPOINT_AUTH_ID:         ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_ENDPOINT_AUTH_ID), 			'NULL');
	PRINT 'PARAMETER10 - @ACTION_ENDPOINT_PARAMS_ID:       ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_ENDPOINT_PARAMS_ID), 			'NULL');
	PRINT 'PARAMETER11 - @ACTION_SQL_CONNECTION_ID:        ' + ISNULL(CONVERT(NVARCHAR(30),   @ACTION_SQL_CONNECTION_ID), 			'NULL');
	PRINT 'PARAMETER12 - @ACTION_TYPE_ID:                  ' + ISNULL(CONVERT(NVARCHAR(5),    @ACTION_TYPE_ID), 					'NULL');
	PRINT 'PARAMETER13 - @ACTION_PRE_SQL:                  ' + ISNULL(CONVERT(NVARCHAR(4000), @ACTION_PRE_SQL), 					'NULL');
	PRINT 'PARAMETER14 - @ACTION_HEADER_SQL:               ' + ISNULL(CONVERT(NVARCHAR(4000), @ACTION_HEADER_SQL), 					'NULL');
	PRINT 'PARAMETER15 - @ACTION_BODY_SQL:                 ' + ISNULL(CONVERT(NVARCHAR(4000), @ACTION_BODY_SQL), 					'NULL');
	PRINT 'PARAMETER16 - @ACTION_POST_SQL:                 ' + ISNULL(CONVERT(NVARCHAR(4000), @ACTION_POST_SQL), 					'NULL');
	PRINT 'PARAMETER17 - @ACTION_ERROR_ACTION_ID:          ' + ISNULL(CONVERT(NVARCHAR(5),    @ACTION_ERROR_ACTION_ID), 			'NULL');
	PRINT 'PARAMETER18 - @ENDPOINT_ACTIVE:                 ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_ACTIVE), 					'NULL');
	PRINT 'PARAMETER19 - @ENDPOINT_DESCRIPTION:            ' + ISNULL(CONVERT(NVARCHAR(250),  @ENDPOINT_DESCRIPTION), 				'NULL');
	PRINT 'PARAMETER20 - @ENDPOINT_URI:                    ' + ISNULL(CONVERT(NVARCHAR(2000), @ENDPOINT_URI), 						'NULL');
	PRINT 'PARAMETER21 - @ENDPOINT_AUTH_ACTIVE:            ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_AUTH_ACTIVE), 				'NULL');
	PRINT 'PARAMETER22 - @ENDPOINT_AUTH_DESCRIPTION:       ' + ISNULL(CONVERT(NVARCHAR(250),  @ENDPOINT_AUTH_DESCRIPTION), 			'NULL');
	PRINT 'PARAMETER23 - @ENDPOINT_AUTH_TYPE_ID:           ' + ISNULL(CONVERT(NVARCHAR(5),    @ENDPOINT_AUTH_TYPE_ID), 				'NULL');
	PRINT 'PARAMETER24 - @ENDPOINT_AUTH_API_KEY:           ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_AUTH_API_KEY), 				'NULL');
	PRINT 'PARAMETER25 - @ENDPOINT_AUTH_API_VALUE:         ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_AUTH_API_VALUE), 			'NULL');
	PRINT 'PARAMETER26 - @ENDPOINT_AUTH_API_KEY_ADD_TO_ID: ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_AUTH_API_KEY_ADD_TO_ID),	'NULL');
	PRINT 'PARAMETER27 - @ENDPOINT_AUTH_TOKEN:             ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_AUTH_TOKEN), 				'NULL');
	PRINT 'PARAMETER28 - @ENDPOINT_AUTH_USERNAME:          ' + ISNULL(CONVERT(NVARCHAR(100),  @ENDPOINT_AUTH_USERNAME), 			'NULL');
	PRINT 'PARAMETER29 - @ENDPOINT_AUTH_PASSWORD:          ' + ISNULL(CONVERT(NVARCHAR(100),  @ENDPOINT_AUTH_PASSWORD), 			'NULL');
	PRINT 'PARAMETER30 - @ENDPOINT_AUTH_DOMAIN:            ' + ISNULL(CONVERT(NVARCHAR(50),   @ENDPOINT_AUTH_DOMAIN), 				'NULL');
	PRINT 'PARAMETER31 - @ENDPOINT_AUTH_WORKSTATION:       ' + ISNULL(CONVERT(NVARCHAR(50),   @ENDPOINT_AUTH_WORKSTATION), 			'NULL');
	PRINT 'PARAMETER32 - @ENDPOINT_PARAMS_ACTIVE:          ' + ISNULL(CONVERT(NVARCHAR(1),    @ENDPOINT_PARAMS_ACTIVE), 			'NULL');
	PRINT 'PARAMETER33 - @ENDPOINT_PARAMS_DESCRIPTION:     ' + ISNULL(CONVERT(NVARCHAR(250),  @ENDPOINT_PARAMS_DESCRIPTION), 		'NULL');
	PRINT 'PARAMETER34 - @ENDPOINT_PARAMS_GROUP_ID:        ' + ISNULL(CONVERT(NVARCHAR(10),   @ENDPOINT_PARAMS_GROUP_ID), 			'NULL');
	PRINT 'PARAMETER35 - @ENDPOINT_PARAMS_SEQUENCE:        ' + ISNULL(CONVERT(NVARCHAR(5),    @ENDPOINT_PARAMS_SEQUENCE), 			'NULL');
	PRINT 'PARAMETER36 - @ENDPOINT_PARAMS_KEY:             ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_PARAMS_KEY), 				'NULL');
	PRINT 'PARAMETER37 - @ENDPOINT_PARAMS_VALUE:           ' + ISNULL(CONVERT(NVARCHAR(150),  @ENDPOINT_PARAMS_VALUE), 				'NULL');	
	PRINT 'PARAMETER38 - @PROFILE_ACTIVE:                  ' + ISNULL(CONVERT(NVARCHAR(1),    @PROFILE_ACTIVE), 					'NULL');
	PRINT 'PARAMETER39 - @PROFILE_TYPE_ID:                 ' + ISNULL(CONVERT(NVARCHAR(5),    @PROFILE_TYPE_ID), 					'NULL');
	PRINT 'PARAMETER40 - @PROFILE_MANDANTOR:               ' + ISNULL(CONVERT(NVARCHAR(50),   @PROFILE_MANDANTOR), 					'NULL');
	PRINT 'PARAMETER41 - @PROFILE_NAME:                    ' + ISNULL(CONVERT(NVARCHAR(50),   @PROFILE_NAME), 						'NULL');
	PRINT 'PARAMETER42 - @PROFILE_DESCRIPTION:             ' + ISNULL(CONVERT(NVARCHAR(250),  @PROFILE_DESCRIPTION), 				'NULL');
	PRINT 'PARAMETER43 - @PROFILE_LOG_LEVEL_ID:            ' + ISNULL(CONVERT(NVARCHAR(5),    @PROFILE_LOG_LEVEL_ID), 				'NULL');
	PRINT 'PARAMETER44 - @PROFILE_LANGUAGE_ID:             ' + ISNULL(CONVERT(NVARCHAR(10),   @PROFILE_LANGUAGE_ID), 				'NULL');
	PRINT 'PARAMETER45 - @PROFILE_FIRST_RUN:               ' + ISNULL(CONVERT(NVARCHAR(50),   @PROFILE_FIRST_RUN), 					'NULL');
	PRINT 'PARAMETER46 - @PROFILE_LAST_RUN:                ' + ISNULL(CONVERT(NVARCHAR(50),   @PROFILE_LAST_RUN), 					'NULL');
	PRINT 'PARAMETER47 - @PROFILE_LAST_RESULT:             ' + ISNULL(CONVERT(NVARCHAR(250),  @PROFILE_LAST_RESULT), 				'NULL');
	PRINT 'PARAMETER48 - @RESULT_ACTION_ID:                ' + ISNULL(CONVERT(NVARCHAR(30),   @RESULT_ACTION_ID), 					'NULL');
	PRINT 'PARAMETER49 - @RESULT_STATUS_ID:                ' + ISNULL(CONVERT(NVARCHAR(10),   @RESULT_STATUS_ID), 					'NULL');
	PRINT 'PARAMETER50 - @RESULT_TYPE_ID:                  ' + ISNULL(CONVERT(NVARCHAR(1),    @RESULT_TYPE_ID), 					'NULL');
	PRINT 'PARAMETER51 - @RESULT_HEADER:                   ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_HEADER), 						'NULL');
	PRINT 'PARAMETER52 - @RESULT_BODY:                     ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_BODY), 						'NULL');
	PRINT 'PARAMETER53 - @RESULT_INFO:                     ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_INFO), 						'NULL');
	PRINT 'PARAMETER54 - @RESULT_ERROR:                    ' + ISNULL(CONVERT(NVARCHAR(100),  @RESULT_ERROR), 						'NULL');
	PRINT '';
	-----------------------------------------------------------------------------------------------------------------------------

	--================================================-- Validation --=======================================================--
	IF (@ENTITY NOT IN ('ACTION', 'ENDPOINT', 'ENDPOINT_AUTH', 'ENDPOINT_PARAMS', 'PROFILE', 'RESULT')) BEGIN
		SET @RETURN_STATUS = 50201;
		SET @RETURN_STATUS_TEXT = 'ERROR: Unknown entity "' + @ENTITY + '". Allowed: ACTION, ENDPOINT, ENDPOINT_AUTH, ENDPOINT_PARAMS, PROFILE, RESULT.';
		RAISERROR(@RETURN_STATUS_TEXT,16,1);
		RETURN @RETURN_STATUS;
	END;

	IF (@GUID IS NULL) BEGIN
		SET @RETURN_STATUS = 50202;
		SET @RETURN_STATUS_TEXT = 'ERROR: @pGUID is required.';
		RAISERROR(@RETURN_STATUS_TEXT,16,1);
		RETURN @RETURN_STATUS;
	END;

	-- value validations from views
	IF (@ENTITY = 'ACTION') BEGIN
		IF (@ACTION_TYPE_ID IS NOT NULL AND @ACTION_TYPE_ID NOT IN (0,1,2,3,4,5,6,7,8,9)) BEGIN
			SET @RETURN_STATUS = 50203;
			SET @RETURN_STATUS_TEXT = 'ERROR: ACTION.TYPE_ID must be one of (0..9).';
		END; ELSE IF (@ACTION_ERROR_ACTION_ID IS NOT NULL AND @ACTION_ERROR_ACTION_ID NOT IN (0,1)) BEGIN
			SET @RETURN_STATUS = 50204;
			SET @RETURN_STATUS_TEXT = 'ERROR: ACTION.ERROR_ACTION_ID must be 0 or 1.';
		END;
	END; ELSE IF (@ENTITY = 'ENDPOINT_AUTH') BEGIN
		IF (@ENDPOINT_AUTH_TYPE_ID IS NOT NULL AND @ENDPOINT_AUTH_TYPE_ID NOT IN (0,1,2,3,4,5,6,7,8,9)) BEGIN
			SET @RETURN_STATUS = 50205;
			SET @RETURN_STATUS_TEXT = 'ERROR: ENDPOINT_AUTH.TYPE_ID must be one of (0..9).';
		END; ELSE IF (@ENDPOINT_AUTH_API_KEY_ADD_TO_ID IS NOT NULL AND @ENDPOINT_AUTH_API_KEY_ADD_TO_ID NOT IN (0,1)) BEGIN
			SET @RETURN_STATUS = 50206;
			SET @RETURN_STATUS_TEXT = 'ERROR: ENDPOINT_AUTH.API_KEY_ADD_TO must be 0 (HEADER) or 1 (QUERY).';
		END;
	END; ELSE IF (@ENTITY = 'PROFILE') BEGIN
		IF (@PROFILE_TYPE_ID IS NOT NULL AND @PROFILE_TYPE_ID NOT IN (1,2)) BEGIN
			SET @RETURN_STATUS = 50207;
			SET @RETURN_STATUS_TEXT = 'ERROR: PROFILE.TYPE_ID must be 1 or 2.';
		END; ELSE IF (@PROFILE_LOG_LEVEL_ID IS NOT NULL AND @PROFILE_LOG_LEVEL_ID NOT IN (0,1,2,3,4,5,6)) BEGIN
			SET @RETURN_STATUS = 50208;
			SET @RETURN_STATUS_TEXT = 'ERROR: PROFILE.LOG_LEVEL_ID must be between 0 and 6.';
		END; ELSE IF (@PROFILE_LANGUAGE_ID IS NOT NULL AND @PROFILE_LANGUAGE_ID NOT IN (1031,1033)) BEGIN
			SET @RETURN_STATUS = 50209;
			SET @RETURN_STATUS_TEXT = 'ERROR: PROFILE.LANGUAGE_ID must be 1031 or 1033.';
		END;
	END; ELSE IF (@ENTITY = 'RESULT') BEGIN
		IF (@RESULT_STATUS_ID IS NOT NULL AND @RESULT_STATUS_ID NOT IN (100,101,103,200,201,202,204,206,301,302,304,307,308,400,401,403,404,405,408,429,451,500,501,502,503,504,511)) BEGIN
			SET @RETURN_STATUS = 50210;
			SET @RETURN_STATUS_TEXT = 'ERROR: RESULT.STATUS_ID not in allowed HTTP status list.';
		END;
	END;

	IF (@RETURN_STATUS > 0) BEGIN
		RAISERROR(@RETURN_STATUS_TEXT,16,1);
		RETURN @RETURN_STATUS;
	END;
	-----------------------------------------------------------------------------------------------------------------------------

	--===============================================-- Main Processing --===================================================--
	BEGIN TRY

		BEGIN TRANSACTION;

		IF (@ENTITY = 'ACTION') BEGIN
			IF NOT EXISTS (SELECT 1 FROM [dbo].[TBREC_CFG_ACTION] WHERE [GUID] = @GUID) BEGIN
				SET @RETURN_STATUS = 50211;
				SET @RETURN_STATUS_TEXT = 'ERROR: ACTION with GUID=' + CONVERT(NVARCHAR(30), @GUID) + ' not found.';
			END; ELSE BEGIN
				UPDATE	[CFG_ACTION] SET
						[PROFILE_ID]        	  = COALESCE(@ACTION_PROFILE_ID,        [CFG_ACTION].[PROFILE_ID]),
						[ACTIVE]            	  = COALESCE(@ACTION_ACTIVE,            [CFG_ACTION].[ACTIVE]),
						[SEQUENCE]          	  = COALESCE(@ACTION_SEQUENCE,          [CFG_ACTION].[SEQUENCE]),
						[ENDPOINT_ID]       	  = COALESCE(@ACTION_ENDPOINT_ID,       [CFG_ACTION].[ENDPOINT_ID]),
						[ENDPOINT_AUTH_ID]  	  = COALESCE(@ACTION_ENDPOINT_AUTH_ID,  [CFG_ACTION].[ENDPOINT_AUTH_ID]),
						[ENDPOINT_PARAMS_ID]	  = COALESCE(@ACTION_ENDPOINT_PARAMS_ID,[CFG_ACTION].[ENDPOINT_PARAMS_ID]),
						[SQL_CONNECTION_ID] 	  = COALESCE(@ACTION_SQL_CONNECTION_ID, [CFG_ACTION].[SQL_CONNECTION_ID]),
						[TYPE_ID]           	  = COALESCE(@ACTION_TYPE_ID,           [CFG_ACTION].[TYPE_ID]),
						[PREPROCESSING_QUERY]     = COALESCE(@ACTION_PRE_SQL,       	[CFG_ACTION].[PREPROCESSING_QUERY]),
						[HEADER_QUERY]      	  = COALESCE(@ACTION_HEADER_SQL,        [CFG_ACTION].[HEADER_QUERY]),
						[BODY_QUERY]        	  = COALESCE(@ACTION_BODY_SQL,          [CFG_ACTION].[BODY_QUERY]),
						[POSTPROCESSING_QUERY] 	  = COALESCE(@ACTION_POST_SQL,         	[CFG_ACTION].[POSTPROCESSING_QUERY]),
						[ERROR_ACTION_ID]      	  = COALESCE(@ACTION_ERROR_ACTION_ID,  	[CFG_ACTION].[ERROR_ACTION_ID]),
						[CHANGED_WHO]      	 	  = COALESCE(@CHANGED_WHO,  			[CFG_ACTION].[CHANGED_WHO]),
						[CHANGED_WHEN]      	  = COALESCE(@CHANGED_WHEN,  			[CFG_ACTION].[CHANGED_WHEN])
				FROM 	[dbo].[TBREC_CFG_ACTION] as [CFG_ACTION]
				WHERE	[CFG_ACTION].[GUID] = @GUID;

				SET @RETURN_STATUS_TEXT = 'INFO: Updated ACTION GUID=' + CONVERT(NVARCHAR(30), @GUID);
			END;

		END; ELSE IF (@ENTITY = 'ENDPOINT') BEGIN
			IF NOT EXISTS (SELECT 1 FROM [dbo].[TBREC_CFG_ENDPOINT] WHERE [GUID] = @GUID) BEGIN
				SET @RETURN_STATUS = 50212;
				SET @RETURN_STATUS_TEXT = 'ERROR: ENDPOINT with GUID=' + CONVERT(NVARCHAR(30), @GUID) + ' not found.';
			END; ELSE BEGIN
				UPDATE	[CFG_ENDPOINT] SET
						[ACTIVE]      	= COALESCE(@ENDPOINT_ACTIVE,      [CFG_ENDPOINT].[ACTIVE]),
						[DESCRIPTION] 	= COALESCE(@ENDPOINT_DESCRIPTION, [CFG_ENDPOINT].[DESCRIPTION]),
						[URI]         	= COALESCE(@ENDPOINT_URI,         [CFG_ENDPOINT].[URI]),
						[CHANGED_WHO]   = COALESCE(@CHANGED_WHO,  		  [CFG_ENDPOINT].[CHANGED_WHO]),
						[CHANGED_WHEN]  = COALESCE(@CHANGED_WHEN,  		  [CFG_ENDPOINT].[CHANGED_WHEN])
				FROM  	[dbo].[TBREC_CFG_ENDPOINT] as [CFG_ENDPOINT]
				WHERE 	[CFG_ENDPOINT].[GUID] = @GUID;

				SET @RETURN_STATUS_TEXT = 'INFO: Updated ENDPOINT GUID=' + CONVERT(NVARCHAR(30), @GUID);
			END;

		END; ELSE IF (@ENTITY = 'ENDPOINT_AUTH') BEGIN
			IF NOT EXISTS (SELECT 1 FROM [dbo].[TBREC_CFG_ENDPOINT_AUTH] WHERE [GUID] = @GUID) BEGIN
				SET @RETURN_STATUS = 50213;
				SET @RETURN_STATUS_TEXT = 'ERROR: ENDPOINT_AUTH with GUID=' + CONVERT(NVARCHAR(30), @GUID) + ' not found.';
			END; ELSE BEGIN
				UPDATE 	[CFG_ENDPOINT_AUTH] SET
						[ACTIVE]       		= COALESCE(@ENDPOINT_AUTH_ACTIVE,        	[CFG_ENDPOINT_AUTH].[ACTIVE]),
						[DESCRIPTION]  		= COALESCE(@ENDPOINT_AUTH_DESCRIPTION,   	[CFG_ENDPOINT_AUTH].[DESCRIPTION]),
						[TYPE_ID]         	= COALESCE(@ENDPOINT_AUTH_TYPE_ID,          [CFG_ENDPOINT_AUTH].[TYPE_ID]),
						[API_KEY]      		= COALESCE(@ENDPOINT_AUTH_API_KEY,       	[CFG_ENDPOINT_AUTH].[API_KEY]),
						[API_VALUE]    		= COALESCE(@ENDPOINT_AUTH_API_VALUE,     	[CFG_ENDPOINT_AUTH].[API_VALUE]),
						[API_KEY_ADD_TO_ID]	= COALESCE(@ENDPOINT_AUTH_API_KEY_ADD_TO_ID,[CFG_ENDPOINT_AUTH].[API_KEY_ADD_TO_ID]),
						[TOKEN]        		= COALESCE(@ENDPOINT_AUTH_TOKEN,         	[CFG_ENDPOINT_AUTH].[TOKEN]),
						[USERNAME]     		= COALESCE(@ENDPOINT_AUTH_USERNAME,      	[CFG_ENDPOINT_AUTH].[USERNAME]),
						[PASSWORD]     		= COALESCE(@ENDPOINT_AUTH_PASSWORD,      	[CFG_ENDPOINT_AUTH].[PASSWORD]),
						[DOMAIN]       		= COALESCE(@ENDPOINT_AUTH_DOMAIN,        	[CFG_ENDPOINT_AUTH].[DOMAIN]),
						[WORKSTATION]  		= COALESCE(@ENDPOINT_AUTH_WORKSTATION,   	[CFG_ENDPOINT_AUTH].[WORKSTATION]),
						[CHANGED_WHO]   	= COALESCE(@CHANGED_WHO,  		  			[CFG_ENDPOINT_AUTH].[CHANGED_WHO]),
						[CHANGED_WHEN]  	= COALESCE(@CHANGED_WHEN,  		  			[CFG_ENDPOINT_AUTH].[CHANGED_WHEN])
				FROM 	[dbo].[TBREC_CFG_ENDPOINT_AUTH] as [CFG_ENDPOINT_AUTH]
				WHERE 	[CFG_ENDPOINT_AUTH].[GUID] = @GUID;

				SET @RETURN_STATUS_TEXT = 'INFO: Updated ENDPOINT_AUTH GUID=' + CONVERT(NVARCHAR(30), @GUID);
			END;

		END; ELSE IF (@ENTITY = 'PROFILE') BEGIN
			IF NOT EXISTS (SELECT 1 FROM [dbo].[TBREC_CFG_PROFILE] WHERE [GUID] = @GUID) BEGIN
				SET @RETURN_STATUS = 50214;
				SET @RETURN_STATUS_TEXT = 'ERROR: PROFILE with GUID=' + CONVERT(NVARCHAR(30), @GUID) + ' not found.';
			END; ELSE BEGIN
				UPDATE	[CFG_PROFILE] SET
						[ACTIVE]       		= COALESCE(@PROFILE_ACTIVE,      	[CFG_PROFILE].[ACTIVE]),
						[TYPE_ID]      		= COALESCE(@PROFILE_TYPE_ID,     	[CFG_PROFILE].[TYPE_ID]),
						[MANDANTOR]    		= COALESCE(@PROFILE_MANDANTOR,   	[CFG_PROFILE].[MANDANTOR]),
						[PROFILE_NAME] 		= COALESCE(@PROFILE_NAME,        	[CFG_PROFILE].[PROFILE_NAME]),
						[DESCRIPTION]  		= COALESCE(@PROFILE_DESCRIPTION, 	[CFG_PROFILE].[DESCRIPTION]),
						[LOG_LEVEL_ID] 		= COALESCE(@PROFILE_LOG_LEVEL_ID,	[CFG_PROFILE].[LOG_LEVEL_ID]),
						[LANGUAGE_ID]  		= COALESCE(@PROFILE_LANGUAGE_ID, 	[CFG_PROFILE].[LANGUAGE_ID]),
						[CHANGED_WHO]   	= COALESCE(@CHANGED_WHO,  		  	[CFG_PROFILE].[CHANGED_WHO]),
						[CHANGED_WHEN]  	= COALESCE(@CHANGED_WHEN,  		  	[CFG_PROFILE].[CHANGED_WHEN])
				FROM 	[dbo].[TBREC_CFG_PROFILE] as [CFG_PROFILE]
				WHERE 	[CFG_PROFILE].[GUID] = @GUID;

				IF EXISTS (SELECT 1 FROM [dbo].[TBREC_RUN_PROFILE] WHERE [PROFILE_ID] = @GUID) BEGIN
					UPDATE	[RUN_PROFILE] SET
							[FIRST_RUN]  	= COALESCE(@PROFILE_FIRST_RUN,  	[RUN_PROFILE].[FIRST_RUN]),
						 	[LAST_RUN]   	= COALESCE(@PROFILE_LAST_RUN,   	[RUN_PROFILE].[LAST_RUN]),
						 	[LAST_RESULT]	= COALESCE(@PROFILE_LAST_RESULT,	[RUN_PROFILE].[LAST_RESULT])
					FROM 	[dbo].[TBREC_RUN_PROFILE] as [RUN_PROFILE]
					WHERE 	[RUN_PROFILE].[PROFILE_ID] = @GUID;
				END; ELSE BEGIN
					INSERT INTO [dbo].[TBREC_RUN_PROFILE] ([PROFILE_ID],[FIRST_RUN],[LAST_RUN],[LAST_RESULT])
					VALUES                                (@GUID, @PROFILE_FIRST_RUN, @PROFILE_LAST_RUN, @PROFILE_LAST_RESULT);
				END;

				SET @RETURN_STATUS_TEXT = 'INFO: Updated PROFILE and RUN_PROFILE for GUID=' + CONVERT(NVARCHAR(30), @GUID);
			END;

		END; ELSE IF (@ENTITY = 'ENDPOINT_PARAMS') BEGIN
			IF NOT EXISTS (SELECT 1 FROM [dbo].[TBREC_CFG_ENDPOINT_PARAMS] WHERE [GUID] = @GUID) BEGIN
				SET @RETURN_STATUS = 50215;
				SET @RETURN_STATUS_TEXT = 'ERROR: ENDPOINT_PARAMS with GUID=' + CONVERT(NVARCHAR(30), @GUID) + ' not found.';
			END; ELSE BEGIN
				UPDATE 	[CFG_ENDPOINT_PARAMS] SET
						[ACTIVE]      		= COALESCE(@ENDPOINT_PARAMS_ACTIVE,      	[CFG_ENDPOINT_PARAMS].[ACTIVE]),
						[DESCRIPTION] 		= COALESCE(@ENDPOINT_PARAMS_DESCRIPTION, 	[CFG_ENDPOINT_PARAMS].[DESCRIPTION]),
						[GROUP_ID]    		= COALESCE(@ENDPOINT_PARAMS_GROUP_ID,    	[CFG_ENDPOINT_PARAMS].[GROUP_ID]),
						[SEQUENCE]    		= COALESCE(@ENDPOINT_PARAMS_SEQUENCE,    	[CFG_ENDPOINT_PARAMS].[SEQUENCE]),
						[KEY]         		= COALESCE(@ENDPOINT_PARAMS_KEY,         	[CFG_ENDPOINT_PARAMS].[KEY]),
						[VALUE]       		= COALESCE(@ENDPOINT_PARAMS_VALUE,       	[CFG_ENDPOINT_PARAMS].[VALUE]),
						[CHANGED_WHO]   	= COALESCE(@CHANGED_WHO,  		  			[CFG_ENDPOINT_PARAMS].[CHANGED_WHO]),
						[CHANGED_WHEN]  	= COALESCE(@CHANGED_WHEN,  		  			[CFG_ENDPOINT_PARAMS].[CHANGED_WHEN])
				FROM 	[dbo].[TBREC_CFG_ENDPOINT_PARAMS] as [CFG_ENDPOINT_PARAMS]
				WHERE	[CFG_ENDPOINT_PARAMS].[GUID] = @GUID;

				SET @RETURN_STATUS_TEXT = 'INFO: Updated ENDPOINT_PARAMS GUID=' + CONVERT(NVARCHAR(30), @GUID);
			END;

		END; ELSE IF (@ENTITY = 'RESULT') BEGIN
			IF NOT EXISTS (SELECT 1 FROM [dbo].[TBREC_OUT_RESULT] WHERE [GUID] = @GUID) BEGIN
				SET @RETURN_STATUS = 50216;
				SET @RETURN_STATUS_TEXT = 'ERROR: RESULT with GUID=' + CONVERT(NVARCHAR(30), @GUID) + ' not found.';
			END; ELSE BEGIN
				UPDATE 	[OUT_RESULT] SET
						[ACTION_ID]    		= COALESCE(@RESULT_ACTION_ID, 		[OUT_RESULT].[ACTION_ID]),
					 	[STATUS_ID]    		= COALESCE(@RESULT_STATUS_ID, 		[OUT_RESULT].[STATUS_ID]),
						[RESULT_TYPE_ID]	= COALESCE(@RESULT_STATUS_ID, 		[OUT_RESULT].[RESULT_TYPE_ID]),
					 	[RESULT_HEADER]		= COALESCE(@RESULT_HEADER,    		[OUT_RESULT].[RESULT_HEADER]),
					 	[RESULT_BODY]  		= COALESCE(@RESULT_BODY,      		[OUT_RESULT].[RESULT_BODY]),
						[RESULT_INFO]  		= COALESCE(@RESULT_INFO,      		[OUT_RESULT].[RESULT_INFO]),
						[RESULT_ERROR]  	= COALESCE(@RESULT_ERROR,      		[OUT_RESULT].[RESULT_ERROR]),
						[CHANGED_WHO]   	= COALESCE(@CHANGED_WHO,  		  	[OUT_RESULT].[CHANGED_WHO]),
						[CHANGED_WHEN]  	= COALESCE(@CHANGED_WHEN,  		  	[OUT_RESULT].[CHANGED_WHEN])
				FROM 	[dbo].[TBREC_OUT_RESULT] as [OUT_RESULT]
				WHERE 	[OUT_RESULT].[GUID] = @GUID;

				SET @RETURN_STATUS_TEXT = 'INFO: Updated RESULT GUID=' + CONVERT(NVARCHAR(30), @GUID);
			END;

		END;

		IF (@RETURN_STATUS > 0) BEGIN
			IF (XACT_STATE() <> 0) ROLLBACK TRANSACTION;
		END; ELSE BEGIN
			COMMIT TRANSACTION;
		END;

		--======================================================-- Output result --======================================================--
		SET @RETURN_STATUS_TEXT = 'END PROCEDURE [' + @MY_PROCEDURE_NAME + '] @ ' + CONVERT(VARCHAR(50), GETDATE(), 120);

		PRINT '';
		PRINT @RETURN_STATUS_TEXT;
		PRINT '====================================================================================================';
		-----------------------------------------------------------------------------------------------------------------------------------
		
		RETURN @RETURN_STATUS;

	END TRY BEGIN CATCH

		IF (XACT_STATE() <> 0) ROLLBACK TRANSACTION;

		--======================================================-- Output result --======================================================--
		SET @RETURN_STATUS_TEXT = 'ERROR: ' + ERROR_MESSAGE();
		RAISERROR(@RETURN_STATUS_TEXT,16,1);

		SET @RETURN_STATUS_TEXT = 'END PROCEDURE [' + @MY_PROCEDURE_NAME + '] @ ' + CONVERT(VARCHAR(50), GETDATE(), 120);

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

		RETURN @RETURN_STATUS;

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

END;
GO