CREATE OR REPLACE PROCEDURE internal.pdataimportlogtext(IN par_dataimportid BIGINT, INOUT par_logtext TEXT DEFAULT null, 
    IN par_logresult INTEGER DEFAULT 0, IN par_select INTEGER DEFAULT 1, INOUT p_refcur refcursor DEFAULT NULL, 
    INOUT p_refcur_2 refcursor DEFAULT NULL)
AS 
$BODY$
DECLARE
    var_Text TEXT;
BEGIN
    /*
    [7811 - Severity CRITICAL - PostgreSQL doesn't support the FORMAT(VARCHAR,VARCHAR) function. Create a user-defined function., 7811 - Severity CRITICAL - PostgreSQL doesn't support the FORMAT(VARCHAR,VARCHAR) function. Create a user-defined function.]
    SELECT @Text = STRING_AGG(CAST(L.Row AS varchar(max)),CHAR(10)) WITHIN GROUP(ORDER BY DataImportLogID)
    	FROM
    	(
    		SELECT
    			L.DataImportLogID,
    			(
    				FORMAT(LogTimestamp,'yyyy-MM-dd HH\:mm\:ss\.fff') + CHAR(9) +
    				L.LogLevel + CHAR(9) +
    				L.LogCode + CHAR(9) +
    				L.LogDescription + CHAR(9) +
    				CASE WHEN LEN(L.LogData) > 100 THEN LEFT(L.LogData,96) + ' ...' ELSE L.LogData END
    			) AS 'Row'
    		FROM
    		(
    			SELECT
    				L.DataImportLogID,
    				L.LogTimestamp,
    				LL.Code LogLevel,
    				CASE WHEN L.Code = 0 THEN '     ' ELSE FORMAT(L.Code,'00000') END LogCode,
    				L.Description LogDescription,
    				ISNULL(L.Data,'') LogData
    			FROM internal.DataImportLog L
    			JOIN internal.LogLevel LL ON L.LogLevelID = LL.LogLevelID
    			WHERE L.DataImportID=@DataImportID
    		) L
    	) L;
    */
    IF par_LogText = 1 THEN
        par_LogText := var_Text;
    END IF;

    IF par_Select = 1 THEN
        OPEN p_refcur FOR
        SELECT
            CAST (var_Text AS VARCHAR(8000)) AS logtext;
    ELSE
        IF par_Select = 2 THEN
            OPEN p_refcur_2 FOR
            SELECT
                var_Text AS logtext;
        END IF;
    END IF;
END;
$BODY$
LANGUAGE plpgsql;