Re: Automatically updating a new information column in PostgreSQL

Поиск
Список
Период
Сортировка
От Pepe TD Vo
Тема Re: Automatically updating a new information column in PostgreSQL
Дата
Msg-id 396604943.2395306.1570204412036@mail.yahoo.com
обсуждение исходный текст
Ответ на RE: Automatically updating a new information column in PostgreSQL  ("Voillequin, Jean-Marc" <Jean-Marc.Voillequin@moodys.com>)
Список pgsql-sql
1st I had both "return new" in the exception and main part. When I run insert and it is still not working and when I remove it from main part, the result is the same.  

an expert from reddit test the script out and he informed me that the script worked fine.  I have test in the linux box and it worked fine too but do not understand why it's not working in AWS unclassified environment.

I found out that in AWS is case sensitive and recreated table with lower case and voila, all worked. 

thank you so much for looking into script and corrected my work.

have a good weekend.

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


On Friday, October 4, 2019, 09:49:33 AM EDT, Voillequin, Jean-Marc <Jean-Marc.Voillequin@moodys.com> wrote:


#yiv0934717431 #yiv0934717431 --_filtered #yiv0934717431 {font-family:Wingdings;panose-1:5 0 0 0 0 0 0 0 0 0;}_filtered #yiv0934717431 {panose-1:2 4 5 3 5 4 6 3 2 4;}_filtered #yiv0934717431 {font-family:Calibri;panose-1:2 15 5 2 2 2 4 3 2 4;}_filtered #yiv0934717431 {font-family:Verdana;panose-1:2 11 6 4 3 5 4 4 2 4;}_filtered #yiv0934717431 {font-family:inherit;panose-1:0 0 0 0 0 0 0 0 0 0;} #yiv0934717431 #yiv0934717431 p.yiv0934717431MsoNormal, #yiv0934717431 li.yiv0934717431MsoNormal, #yiv0934717431 div.yiv0934717431MsoNormal{margin:0in;margin-bottom:.0001pt;font-size:11.0pt;font-family:sans-serif;} #yiv0934717431 a:link, #yiv0934717431 span.yiv0934717431MsoHyperlink{color:#0563C1;text-decoration:underline;} #yiv0934717431 a:visited, #yiv0934717431 span.yiv0934717431MsoHyperlinkFollowed{color:#954F72;text-decoration:underline;} #yiv0934717431 p.yiv0934717431msonormal0, #yiv0934717431 li.yiv0934717431msonormal0, #yiv0934717431 div.yiv0934717431msonormal0{margin-right:0in;margin-left:0in;font-size:11.0pt;font-family:sans-serif;} #yiv0934717431 p.yiv0934717431ydpea5899c81qeiagb0cpwnlhdf9xsijm, #yiv0934717431 li.yiv0934717431ydpea5899c81qeiagb0cpwnlhdf9xsijm, #yiv0934717431 div.yiv0934717431ydpea5899c81qeiagb0cpwnlhdf9xsijm{margin-right:0in;margin-left:0in;font-size:11.0pt;font-family:sans-serif;} #yiv0934717431 span.yiv0934717431ydp3b190d25yiv3811552299sg{} #yiv0934717431 span.yiv0934717431EmailStyle22{font-family:sans-serif;color:windowtext;} #yiv0934717431 .yiv0934717431MsoChpDefault{font-size:10.0pt;}_filtered #yiv0934717431 {margin:70.85pt 70.85pt 70.85pt 70.85pt;} #yiv0934717431 div.yiv0934717431WordSection1{} #yiv0934717431 _filtered #yiv0934717431 {}_filtered #yiv0934717431 {font-family:Wingdings;}_filtered #yiv0934717431 {}_filtered #yiv0934717431 {font-family:Wingdings;}_filtered #yiv0934717431 {font-family:Symbol;}_filtered #yiv0934717431 {}_filtered #yiv0934717431 {font-family:Wingdings;}_filtered #yiv0934717431 {font-family:Symbol;}_filtered #yiv0934717431 {}_filtered #yiv0934717431 {font-family:Wingdings;} #yiv0934717431 ol{margin-bottom:0in;} #yiv0934717431 ul{margin-bottom:0in;} #yiv0934717431

Hello,

 

The function has a “return new;” in the exception part, not in the main part.

A return new is probably missing after the end if;

 

CREATE OR REPLACE FUNCTION "ECISDRDM"."TRIGGER_FCT_TR_STG_APPLICATION_CDIM_INS"()  RETURNS trigger AS $$

declare

v_ErrorCode int;
v_ErrorMsg varchar(512);
v_Module varchar(32) := 'TR_STG_APPLICATION_CDIM_INS';

begin

----

-- If this is an INSERT operation

----

if TG_OP = 'INSERT' then

   ----
   -- This just ensures that the filename is not null
   ----

   if new.mig_filename IS NULL then
      new.mig_filename := 'Unknown';
   end if;

   new.mig_insert_dt = current_timestamp;

end if;

/* ===================================== HERE ================================*/

return new;

----
-- Exception error handler
----

   exception
      when others then

      v_ErrorCode := SQLSTATE;
      v_ErrorMsg := SQLERRM;

      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);

RETURN NEW;
end;
$$
language 'plpgsql';

 

From: Pepe TD Vo <pepevo@yahoo.com>
Sent: Friday, October 4, 2019 3:27 PM
To: Pgsql-admin <pgsql-admin@postgresql.org>; pgsql-sql@postgresql.org
Subject: Automatically updating a new information column in PostgreSQL

 

 

CAUTION: This email originated from outside of Moody's. Do not click links or open attachments unless you recognize the sender and know the content is safe.

 

good morning experts,

I have a trigger before insert (even with or update) and seem it doesnt' work.  The function simply sets both columns named mig_filename to "unknown if its null, and mig_insert_dt to current timestample for each row passed to the trigger.

 

here is my script even I take all the rest out and just simple function with new.mig_insert_dt := localtimestamp;

 

 

 

CREATE OR REPLACE FUNCTION "ECISDRDM"."TRIGGER_FCT_TR_STG_APPLICATION_CDIM_INS"()  RETURNS trigger AS $$

declare

v_ErrorCode int;
v_ErrorMsg varchar(512);
v_Module varchar(32) := 'TR_STG_APPLICATION_CDIM_INS';

begin

----

-- If this is an INSERT operation

----

if TG_OP = 'INSERT' then

   ----
   -- This just ensures that the filename is not null
   ----

   if new.mig_filename IS NULL then
      new.mig_filename := 'Unknown';
   end if;

   new.mig_insert_dt = current_timestamp;

end if;

----
-- Exception error handler
----

   exception
      when others then

      v_ErrorCode := SQLSTATE;
      v_ErrorMsg := SQLERRM;

      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);

RETURN NEW;
end;
$$
language 'plpgsql';

CREATE TRIGGER "TR_STG_APPLICATION_CDIM_INS" BEFORE INSERT OR UPDATE ON "ECISDRDM"."STG_APPLICATION_CDIM" FOR EACH ROW EXECUTE PROCEDURE "ECISDRDM"."TRIGGER_FCT_TR_STG_APPLICATION_CDIM_INS"() ;

I even " RAISE EXCEPTION 'UNKNOWN'; " and for the mig_insert_dt, I put either '=' or ':=' Now(), now(), localtimestamp, timestamp, and none of them would fill the time. Both mig.filename and mig_insert_dt are still blank.

"

if new.mig_filename IS NULL then 

    RAISE EXCEPTION 'UNKNOWN';

    new.mig_filename := 'Unknown';

end if;

new.mig_insert_dt '= now();   

 

According to the postgres example 39-3 "shows an example of trigger procedure in PL/pgSQL", I don't see any different with the example and don't know what I have missed here.  Would you please advise what I did wrong here?  

 

thank you,

 

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

-----------------------------------------
Moody's monitors email communications through its networks for regulatory compliance purposes and to protect its customers, employees and business and where allowed to do so by applicable law. The information contained in this e-mail message, and any attachment thereto, is confidential and may not be disclosed without our express permission. If you are not the intended recipient or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that you have received this message in error and that any review, dissemination, distribution or copying of this message, or any attachment thereto, in whole or in part, is strictly prohibited. If you have received this message in error, please immediately notify us by telephone, fax or e-mail and delete the message and all of its attachments. Every effort is made to keep our network free from viruses. You should, however, review this e-mail message, as well as any attachment thereto, for viruses. We take no responsibility and have no liability for any computer virus which may be transferred via this e-mail message.

This email was sent to you by Moody’s Investors Service EMEA Limited
Registered office address:
One Canada Square
Canary Wharf
London, E14 5FA
Registered in England and Wales No: 8922701
-----------------------------------------

В списке pgsql-sql по дате отправления:

Предыдущее
От: "Voillequin, Jean-Marc"
Дата:
Сообщение: RE: Automatically updating a new information column in PostgreSQL
Следующее
От: Marius Andreiana
Дата:
Сообщение: parser that could handle "FROM... SELECT..." as well as "SELECT... FROM..."