Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iHvAY-0001Vz-Ef for pgsql-admin@arkaria.postgresql.org; Tue, 08 Oct 2019 19:29:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iHvAX-0004FW-BU for pgsql-admin@arkaria.postgresql.org; Tue, 08 Oct 2019 19:29:13 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iHvAX-0004FL-0w for pgsql-admin@lists.postgresql.org; Tue, 08 Oct 2019 19:29:13 +0000 Received: from jakobs.com ([85.214.83.89] helo=rs.plausibolo.de) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iHvAT-0000rR-Ls for pgsql-admin@lists.postgresql.org; Tue, 08 Oct 2019 19:29:12 +0000 Received: from localhost (localhost [127.0.0.1]) by rs.plausibolo.de (Postfix) with ESMTP id 64778BE08DE for ; Tue, 8 Oct 2019 21:29:08 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at rs.plausibolo.de Received: from rs.plausibolo.de ([IPv6:::1]) by localhost (h2367442.stratoserver.net [IPv6:::1]) (amavisd-new, port 10024) with ESMTP id RyH7755NuZuy; Tue, 8 Oct 2019 21:29:04 +0200 (CEST) Received: from [192.168.179.151] (dslb-094-219-025-209.094.219.pools.vodafone-ip.de [94.219.25.209]) by rs.plausibolo.de (Postfix) with ESMTPSA id 0FF54BE078C; Tue, 8 Oct 2019 21:29:03 +0200 (CEST) Subject: Re: how to call a stored function on conflict To: pgsql-admin@lists.postgresql.org References: <1496660547.3525362.1570560989743.ref@mail.yahoo.com> <1496660547.3525362.1570560989743@mail.yahoo.com> From: Holger Jakobs Openpgp: preference=signencrypt Autocrypt: addr=holger@jakobs.com; prefer-encrypt=mutual; keydata= mQGiBEG8IA8RBAC6WqUzEPTjest7MvFca5WlI47EgtKzOy1D1X9gSEmPj0sQrilNGEitY/+Q kPnCvI3odz1XXf/MZQtXwlxJA4lPmx8/K7MqBj9vh0J5jRznpE7l+SBDmmDobIqBAgl6BZvH 1C7e2y72h5T1/plrXonLUGzthqBtsvWl0ogoyTJqiwCg6AOhYX+Xbcl8/AXy4F4qbUq4LXsD /j6yCKRkyleN688y9YpuS5J6/ZZ/OBCM74pa2iNge3GmXn1JeUowF693QFCGHkpswmDIoGhL 5iM6GHj9HFB6rTXW5H3sR1y6ta7vXwoNBGkgiM89B1a/KsLT5L6MZgZ5AV0tj6vxwAsuLixS SDiNVNAd8vp2FfqdLlOgThsgVIwKBACsCzZa1VxpdoNSYnkTOSZhEjZkbWApdneQ2bTxXJgW +fWgVeLiojCp6I1AmQqzwHz8UN1nrsQjH8Rnxt2J/C5H/Ek9jlOFakFQXlPMZw3tNeTA1P3B 3zPyq6hKFPZHFklUquE3gHVjnX9qxSW0xvFirf6xAMCMYWRnCQHRgiXoAbQhSG9sZ2VyIEph a29icyA8aG9sZ2VyQGpha29icy5jb20+iGQEExECACQCGwMGCwkIBwMCAxUCAwMWAgECHgEC F4AFAlZ7KnEFCSeLDWIACgkQileck7MZYbpbxgCgxM54+4qx/sfGwq01x93pWr2QghwAoIfD Of6Uoc43y98+xR5/Iu9Z1RH8uQENBEG8IBAQBACiGkoX8HKCocdCwRdMzc9siDs1w0YNayhT PnK9Ii3wzzS2R9NSyWsPQVyx6hkVfSmK6CE5RzfuDUzrYEgyaV/2vT3JD9Gko+RuFnjeQvVo 8hc19bmBSn95ATki+hFy/68MqpMlsG7YyCluRSpXh2qxlhbh2FzTIQmiEQAR4kFCEwADBQP/ Z1Y6nwoXw/5JmLJLFcvZZlFfn4g9TjyMSwH50/AXqOApccy9JSzoTFY87ItmGkhD0qj18PJB St2uqASGrk2/55XK596RWnoeyL9mzj5r6woDqPNRXIbK85foGVIMRrb/EufgSOP5H6VHybvB PD4CppEw4Q0fRsMCaOcvibkjHoqITwQYEQIADwIbDAUCVnsqcQUJJ4sNYQAKCRCKV5yTsxlh urunAJ9avyijdRls7l6XPc2cBMys3xu/sgCeOVJE6DzwBBRUd6S6mtf2eG4FPXg= Message-ID: <725f0501-c6db-f343-aff6-30d70040ed07@jakobs.com> Date: Tue, 8 Oct 2019 21:29:03 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <1496660547.3525362.1570560989743@mail.yahoo.com> Content-Type: multipart/alternative; boundary="------------1EC70F37B84C9D6C28E14E4C" Content-Language: de-DE List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------1EC70F37B84C9D6C28E14E4C Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Hi Pepe, the SQLSTATE is not an integer or bigint, so it cannot be stored in a variable declared as bigint. See https://www.postgresql.org/docs/current/errcodes-appendix.html Regards, Holger Am 08.10.19 um 20:56 schrieb Pepe TD Vo: > Hello experts, > > I have a store function on conflict to update and insert features > information from one table to another table   > > My code is: > > create or replace FUNCTION ecisdrdm.pr_mig_stg_application_cdim (v_Ret > OUT bigint )  > as $$ > declare  >         v_ErrorCode             bigint; >         v_ErrorMsg              varchar(512); >         v_Module                varchar(32) = > 'pr_mig_stg_application_cdim'; > > begin > > ---- > -- MERGING: STG_APPLICATION_CDIM into APPLICATION_CDIM > ---- > > INSERT INTO application_cdim AS prod (prod.application_id, > prod.receipt_number,prod.init_frm_id, prod.frm_typ_id,   > prod.sbmtd_dt_id, prod.crtd_user_id, prod.init_src_sys_id, > prod.init_svc_ctr_id, prod.mig_filename)  > SELECT stg.application_id, stg.receipt_number, stg.init_frm_id, > stg.frm_typ_id, stg.sbmtd_dt_id, stg.crtd_user_id, > stg.init_src_sys_id, stg.init_svc_ctr_id, stg.mig_filename  > FROM ecisdrdm.stg_application stg > ON CONFLICT (application_id)  DO UPDATE  > SET ( prod.init_frm_id, prod.frm_typ_id,prod. sbmtd_dt_id, > prod.crtd_user_id,  > prod.init_src_sys_id, prod.init_svc_ctr_id, prod.mig_filename, > prod.mig_modified_dt > ) > =  > (SELECT stg.init_frm_id, stg.frm_typ_id, stg.sbmtd_dt_id, > stg.crtd_user_id, >  stg.init_src_sys_id, stg.init_svc_ctr_id, stg.mig_filename, > current_timestamp > FROM ecisdrdm.stg_application_cdim stg > WHERE prod.receipt_number = stg.receipt_number > );            > RETURN; > >    ---- >    -- Set the return code to 0 >    ---- > >    v_Ret := SQLSTATE; > > ---- > -- Exception error handler > ---- > exception >    when others then > v_ErrorCode := SQLSTATE; > v_ErrorMsg  := SQLERRM; > v_Ret       := v_ErrorCode; > > ---- > -- Commit the record into the ErrorLog > ---- > PERFORM pr_write_error_log( v_os_user, v_host, v_module, v_ErrorCode, > v_ErrorMsg ); > > ---- > -- Intentionally leaving the "commit" to application > ---- > end; > $$ language plpgsql; > > and my other function for pr_write_error_log is  > > CREATE OR REPLACE FUNCTION ecisdrdm.pr_write_error_log ( > v_os_user character varying, > v_host character varying, > v_module character varying, > v_errorcode integer, > v_errormsg character varying) >     RETURNS void >     LANGUAGE 'plpgsql' > >     COST 100 >     VOLATILE  > AS $BODY$ > > BEGIN > START TRANSACTION; > INSERT INTO ecisdrdm.errorlog(tstamp, os_user, host, module, > errorcode, errormsg) > VALUES >    (CURRENT_TIMESTAMP, CURRENT_USER, inet_server_addr(), v_module, > v_ErrorCode, v_ErrorMsg ); > >         /* commit; */ > end; > > $BODY$; > > ALTER FUNCTION ecisdrdm.pr_write_error_log (character varying, > character varying, character varying, integer, character varying) >     OWNER TO "PSmasteruser"; > > > they all created successfully. > > when I execute the function of pr_mig_stg_application_cdim as "select > ecisdrdm.pr_mig_stg_application_cdim(); " > > ERROR: invalid input syntax for integer: "42P01" CONTEXT: PL/pgSQL > function ecisdrdm.pr_mig_stg_application_cdim() line 41 at assignment > SQL state: 22P02 > > may I know how to execute the stored function?  What did wrong on my > queries? > > thank you for your help. > > ** > *Bach-Nga > > *No one in this world is pure and perfect.  If you avoid people for > their mistakes you will be alone. So judge less, love and forgive more. > To call him a dog hardly seems to do him justice though in as much as > he had four legs, a tail, and barked, I admit he was, to all outward > appearances. But to those who knew him well, he was a perfect > gentleman (Hermione Gingold) > > **Live simply **Love generously **Care deeply **Speak kindly. > *** Genuinely rich *** Faithful talent *** Sharing success -- Holger Jakobs, Bergisch Gladbach instant messaging: xmpp:holger@jakobs.com +49 178 9759012 oder +49 2202 817157 --------------1EC70F37B84C9D6C28E14E4C Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit

Hi Pepe,

the SQLSTATE is not an integer or bigint, so it cannot be stored in a variable declared as bigint.

See https://www.postgresql.org/docs/current/errcodes-appendix.html

Regards,

Holger

Am 08.10.19 um 20:56 schrieb Pepe TD Vo:
Hello experts,

I have a store function on conflict to update and insert features information from one table to another table  

My code is:

create or replace FUNCTION ecisdrdm.pr_mig_stg_application_cdim (v_Ret OUT bigint ) 
as $$
declare 
        v_ErrorCode             bigint;
        v_ErrorMsg              varchar(512);
        v_Module                varchar(32) = 'pr_mig_stg_application_cdim';

begin

----
-- MERGING: STG_APPLICATION_CDIM into APPLICATION_CDIM
----

INSERT INTO application_cdim AS prod (prod.application_id, prod.receipt_number,prod.init_frm_id, prod.frm_typ_id,  
prod.sbmtd_dt_id, prod.crtd_user_id, prod.init_src_sys_id, prod.init_svc_ctr_id, prod.mig_filename) 
SELECT stg.application_id, stg.receipt_number, stg.init_frm_id, stg.frm_typ_id, stg.sbmtd_dt_id, stg.crtd_user_id,
stg.init_src_sys_id, stg.init_svc_ctr_id, stg.mig_filename 
FROM ecisdrdm.stg_application stg
ON CONFLICT (application_id)  DO UPDATE 
SET ( prod.init_frm_id, prod.frm_typ_id,prod. sbmtd_dt_id, prod.crtd_user_id, 
prod.init_src_sys_id, prod.init_svc_ctr_id, prod.mig_filename, prod.mig_modified_dt
)
= 
(SELECT stg.init_frm_id, stg.frm_typ_id, stg.sbmtd_dt_id, stg.crtd_user_id,
  stg.init_src_sys_id, stg.init_svc_ctr_id, stg.mig_filename, current_timestamp
FROM ecisdrdm.stg_application_cdim stg
WHERE prod.receipt_number = stg.receipt_number
);           
RETURN;

   ----
   -- Set the return code to 0
   ----

   v_Ret := SQLSTATE;

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

----
-- Commit the record into the ErrorLog
----
PERFORM pr_write_error_log( v_os_user, v_host, v_module, v_ErrorCode, v_ErrorMsg );

----
-- Intentionally leaving the "commit" to application
----
end;
$$ language plpgsql;

and my other function for pr_write_error_log is 

CREATE OR REPLACE FUNCTION ecisdrdm.pr_write_error_log (
v_os_user character varying,
v_host character varying,
v_module character varying,
v_errorcode integer,
v_errormsg character varying)
    RETURNS void
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
AS $BODY$

BEGIN
START TRANSACTION;
INSERT INTO ecisdrdm.errorlog(tstamp, os_user, host, module, errorcode, errormsg)
VALUES
   (CURRENT_TIMESTAMP, CURRENT_USER, inet_server_addr(), v_module, v_ErrorCode, v_ErrorMsg );

        /* commit; */
end;

$BODY$;

ALTER FUNCTION ecisdrdm.pr_write_error_log (character varying, character varying, character varying, integer, character varying)
    OWNER TO "PSmasteruser";


they all created successfully.

when I execute the function of pr_mig_stg_application_cdim as "select ecisdrdm.pr_mig_stg_application_cdim(); "

ERROR: invalid input syntax for integer: "42P01" CONTEXT: PL/pgSQL function ecisdrdm.pr_mig_stg_application_cdim() line 41 at assignment SQL state: 22P02

may I know how to execute the stored function?  What did wrong on my queries?

thank you for your help.

Bach-Nga

No one in this world is pure and perfect.  If you avoid people for their mistakes you will be alone. So judge less, love and forgive more.
To call him a dog hardly seems to do him justice though in as much as he had four legs, a tail, and barked, I admit he was, to all outward appearances. But to those who knew him well, he was a perfect gentleman (Hermione Gingold)

**Live simply **Love generously **Care deeply **Speak kindly.
*** Genuinely rich *** Faithful talent *** Sharing success
--

Holger Jakobs, Bergisch Gladbach
instant messaging: xmpp:holger@jakobs.com
+49 178 9759012 oder +49 2202 817157

--------------1EC70F37B84C9D6C28E14E4C--