CREATE OR REPLACE function internal.pupdatedataimport(IN par_dataimportid BIGINT, IN par_dataimportstatus VARCHAR, 
    IN par_message VARCHAR DEFAULT null, INOUT return_code int default null)
AS 
$BODY$
DECLARE
    var_TranCountOnEntry INTEGER
    /*
    [7811 - Severity CRITICAL - PostgreSQL doesn't support the @@TRANCOUNT function. Create a user-defined function.]
    = @@TRANCOUNT
    */;
BEGIN
    /*
    [7810 - Severity CRITICAL - PostgreSQL doesn't support the SET NOCOUNT. If need try another way to send message back to the client application.]
    SET NOCOUNT ON;
    */
    BEGIN
        IF var_TranCountOnEntry = 0 THEN
            /*
            [7807 - Severity CRITICAL - PostgreSQL does not support explicit transaction management in functions. Perform a manual conversion.]
            BEGIN TRAN;
            */
            BEGIN
            END;
        END IF;
        UPDATE internal.dataimport AS di
        SET completetime = CURRENT_TIMESTAMP(3), dataimportstatus = par_DataImportStatus, message =
        CASE
            WHEN par_Message IS NULL THEN di.message
            WHEN di.message IS NULL OR LOWER(par_Message) = LOWER('') THEN par_Message
            ELSE di.message || aws_sqlserver_ext.CHAR(10) || REPLACE(par_Message, aws_sqlserver_ext.CHAR(13), '')
        END
            WHERE di.dataimportid = par_DataImportID;

        IF var_TranCountOnEntry = 0 THEN
            COMMIT;
        END IF;
        EXCEPTION
            WHEN OTHERS THEN
                IF var_TranCountOnEntry = 0 THEN
                    ROLLBACK;
                END IF;
                RAISE;
    END;
    return_code := 0;
    RETURN;
END;
$BODY$
LANGUAGE plpgsql;