CREATE OR REPLACE PROCEDURE internal.pdataimportloginsertcount(IN par_dataimportid BIGINT, IN par_description VARCHAR, 
                IN par_rowcount INTEGER, IN par_loglevel CHAR DEFAULT 'I', IN par_data VARCHAR DEFAULT null, 
                IN par_dataid INTEGER DEFAULT null, IN par_code INTEGER DEFAULT 0)
AS 
$BODY$
DECLARE
    var_Msg VARCHAR(280) DEFAULT '';
    var_Severity INTEGER;
    var_LogLevelID SMALLINT;
BEGIN

RAISE NOTICE 'procedure internal.pdataimportloginsertcount';
    /*
    [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;
    */
    /*
    [7811 - Severity CRITICAL - PostgreSQL doesn't support the FORMATMESSAGE(NVARCHAR,<VARIABLE_ARGUMENTS>) function. Create a user-defined function.]
    SET @Description = FORMATMESSAGE(@Description, @RowCount);
    */
    IF LOWER(par_LogLevel) = LOWER('I') THEN
        var_Msg := 'INFO';
        var_LogLevelID := 0;
        var_Severity := 0;
    ELSE
        IF LOWER(par_LogLevel) = LOWER('W') THEN
            var_Msg := 'WARNING';
            var_LogLevelID := 1;
            var_Severity := 1;
        ELSE
            IF LOWER(par_LogLevel) = LOWER('E') THEN
                var_Msg := 'ERROR';
                var_LogLevelID := 2;
                var_Severity := 2;
            ELSE
                IF LOWER(par_LogLevel) = LOWER('X') THEN
                    var_Msg := 'ERROR';
                    var_LogLevelID := 2;
                    var_Severity := 16;
                END IF;
            END IF;
        END IF;
    END IF;

    IF par_Code IS NOT NULL AND par_Code <> 0 THEN
        var_Msg := var_Msg || ' [' || CAST (par_Code AS VARCHAR(30)) || ']';
    END IF;
    var_Msg := var_Msg || ': ' || COALESCE(par_Description, '');

    IF par_Data IS NOT NULL AND LENGTH(par_Data) > 0 THEN
        var_Msg := var_Msg || ': ' || par_Data;
    END IF;
    INSERT INTO internal.dataimportlog (dataimportid, loglevelid, dataid, code, description, data)
    SELECT
        par_DataImportID, var_LogLevelID, par_DataID, par_Code, par_Description, par_Data;
    -- RAISE 'Error %, severity %, state % was raised. Message: %.', '50000', var_Severity, 0, var_Msg USING ERRCODE = '50000';
END;
$BODY$
LANGUAGE plpgsql;