如何在PostgreSQL函数中使用“ RAISE INFO,RAISE LOG,RAISE DEBUG,PERFORM”?

时间:2018-11-07 15:13:09

标签: postgresql

我在Oracle过程中有脚本,如Pragma automation_transaction:

CREATE OR REPLACE EDITIONABLE PROCEDURE "CIDR_STAGING"."PR_WRITE_ERROR_LOG" is
        PRAGMA AUTONOMOUS_TRANSACTION;
begin
   insert into cidrmgmt.errorlog(
                         tstamp, os_user,host,module,errorcode,errormsg)
        values
                (sysdate, v_os_user, v_host, v_module, v_ErrorCode, v_ErrorMsg );
        commit;
end;
/

和另一个调用交易过程的过程:

CREATE OR REPLACE EDITIONABLE PROCEDURE "CIDR_STAGING"."PR_MIG_STG_FORMS" ( v_Ret OUT number )
as
        v_ErrorCode             number;
        v_ErrorMsg              varchar2(512);
        v_Module                        varchar2(32) := 'PR_MIG_STG_FORMS';
begin

        ----
        -- Simply delete the data from production table
        ----
        delete from cidrdba.ref_forms where 1=1;

        ----
        -- Simply copy the data from staging into production
        ----
        insert into cidrdba.ref_forms(
           form_number, form_title, mig_filename
                )
                select form_number, form_title, mig_filename
                from cidr_staging.stg_ref_forms;
        ----
        -- Set the return code to 0
        ----
        v_Ret := SQLCODE;

----
-- Exception error handler
----
exception
        when others then
                v_ErrorCode := SQLCODE;
                v_ErrorMsg  := SQLERRM;
                v_Ret       := v_ErrorCode;

                ----
                -- Commit the record into the ErrorLog
                ----
                pr_write_error_log( sys_context('userenv','session_user'),
                    sys_context('userenv','host'), v_Module,
                         v_ErrorCode, v_ErrorMsg );
        ----
        -- Intentionally leaving the "commit" to application
        ----
end;
/

我将其转换为Postgres Pragma automation_transaction,但我不确定它是否已纠正,但没有给出任何错误。

create or replace FUNCTION "PR_WRITE_ERROR_LOG" ( v_os_user IN varchar(4000), v_host IN
 varchar(4000), v_module IN varchar(4000), v_errorcode IN int, v_errormsg IN varchar(4000) ) 
 RETURNS VOID 
as $$

BEGIN
    START TRANSACTION;
   insert into cidrmgmt.errorlog(
                         tstamp, os_user,host,module,errorcode,errormsg)
        values
                (current_timestamp, v_os_user, v_host, v_module, v_ErrorCode, v_ErrorMsg );

        /* commit; */

end;
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION "CIDR_STAGING"."PR_MIG_STG_FORMS" ( v_Ret OUT int ) RETURNS integer
as $$
declare
        v_ErrorCode             int;
        v_ErrorMsg              varchar(512);
        v_Module                varchar(32) = 'PR_MIG_STG_FORMS';

begin
        ----
        -- Simply delete the data from production table
        ----
        delete from cidrdba.ref_forms where 1=1;

        ----
        -- Simply copy the data from staging into production
        ----
        insert into cidrdba.ref_forms(
           form_number, form_title, mig_filename
                )
                select form_number, form_title, mig_filename
                from cidr_staging.stg_ref_forms;
        ----
        -- Set the return code to 0
        ----
        v_Ret := SQLCODE;

----
-- Exception error handler
----
exception
        when others then
                v_ErrorCode := SQLCODE;
                v_ErrorMsg  := SQLERRM;
                v_Ret       := v_ErrorCode;

                ----
                -- Commit the record into the ErrorLog
                ----
        **RAISE NOTICE 'Calling "CIDR_STAGING"."PR_WRITE_ERROR_LOG"(%)', ( sys_context('userenv','session_user'),
        sys_context('userenv','host'), v_Module,
        v_ErrorCode, v_ErrorMsg );**

or
**PERFORM pr_write_error_log( sys_context('userenv','session_user'),
                    sys_context('userenv','host'), v_Module,
                         v_ErrorCode, v_ErrorMsg );
        -- Intentionally leaving the "commit" to application**
        ----
end;
$$ LANGUAGE plpgsql;

我应该如何在Oracle之类的函数中执行另一个函数?我已经搜索并发现参数提高日志,提高信息,提高之前和执行。我想了解如何在Oracle之类的函数中调用该函数,请您解释一下?

0 个答案:

没有答案