Home » SQL & PL/SQL » SQL & PL/SQL » Trigger for updating all columns of a table (oracle plsql)
Trigger for updating all columns of a table [message #595604] Thu, 12 September 2013 07:23 Go to next message
narendra.treddi
Messages: 6
Registered: August 2007
Location: mumbai
Junior Member

Hi,

I have created the following trigger whcich will track all the column changes and insert the row in log table. here i have some doubt while substituting the cursor value.


create or replace trigger historylog_trigger
before update on log_dev_test
for each row
declare
PRAGMA AUTONOMOUS_TRANSACTION;
in_loamid number(10);
in_col_name varchar2(10);
in_old_val varchar2(100);
in_new_val varchar2(100);
begin
for r in (select column_name from all_tab_columns
where table_name = 'log_dev_test'
and owner = 'LOAM')
loop
if updating(r.column_name) then
in_col_name:=r.column_name;
in_old_val:= ':old.'||r.column_name;
in_new_val:= ':new.'||r.column_name;

in_loamid: = :new.LOAMID;
in_lastupdateuser: = :new.lastupdateuser
if((in_old_val <> in_new_val) or (in_old_val is null and in_new_val is not null) or (in_old_val is not null and in_new_val is null)) then
EXECUTE IMMEDIATE 'INSERT INTO historylog VALUES (log_history_sequence.NEXTVAL,in_loamid, to_char(in_col_name), to_char(in_old_val),to_char(in_new_val),sysdate,in_lastupdateuser)';
exit when r%notfound;
endif;
end if;
end loop;

end;

are the below assignement of values will work ?

in_old_val:= ':old.'||r.column_name;
in_new_val:= ':new.'||r.column_name;

i want to take the coulmname from the cursor and assign tat to psuedo columns like :new.r.column_name ..

Please suggest on this.

Regards,
Narendra.
Re: Trigger for updating all columns of a table [message #595606 is a reply to message #595604] Thu, 12 September 2013 07:27 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
narendra.treddi wrote on Thu, 12 September 2013 17:53


are the below assignement of values will work ?

in_old_val:= ':old.'||r.column_name;
in_new_val:= ':new.'||r.column_name;


Just execute it in SQL*Plus. Copy paste and let us know what you get. And please use code tags to post a test case, it's difficult to understand your code.
Re: Trigger for updating all columns of a table [message #595607 is a reply to message #595604] Thu, 12 September 2013 07:28 Go to previous messageGo to next message
Littlefoot
Messages: 21808
Registered: June 2005
Location: Croatia, Europe
Senior Member
Account Moderator
Put the EXECUTE IMMEDIATE statement into a variable and print it on the screen. Doing so, you'd see how it looks like and, possibly, make some changes to your code.
Re: Trigger for updating all columns of a table [message #595609 is a reply to message #595604] Thu, 12 September 2013 07:37 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Hi,

narendra.treddi wrote on Thu, 12 September 2013 14:23
are the below assignement of values will work ?

in_old_val:= ':old.'||r.column_name;
in_new_val:= ':new.'||r.column_name;

i want to take the coulmname from the cursor and assign tat to psuedo columns like :new.r.column_name ..

Simple answer: no, it is not possible.

Complex answer: you may "dynamically" construct the code of that trigger, as demonstrated in this thread on AskTom: http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:734825535375
Note, that table has fixed number of columns - there is no need to change the trigger until you add/drop column (which should be rare). It would need only simple script (or whatever it will contain) call for re-creating the trigger after DDL statement changing column definition anyway.

Oracle also has inbuilt tools for achieving auditing (e.g. AUDIT statement). You should definitively explore them.
Re: Trigger for updating all columns of a table [message #595610 is a reply to message #595604] Thu, 12 September 2013 07:37 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
narendra.treddi wrote on Thu, 12 September 2013 17:53

create or replace trigger historylog_trigger
before update on log_dev_test
for each row

declare
PRAGMA AUTONOMOUS_TRANSACTION;
in_loamid number(10);
in_col_name varchar2(10);
in_old_val varchar2(100);
in_new_val varchar2(100);
begin
for r in (select column_name from all_tab_columns
where table_name = 'log_dev_test' and owner = 'LOAM'


That's a potential bug in the code with the way PRAGMA AUTONOMOUS_TRANSACTION is used. You are querying the table on which the trigger is defined.

A small example is here
Re: Trigger for updating all columns of a table [message #595611 is a reply to message #595610] Thu, 12 September 2013 07:45 Go to previous messageGo to next message
flyboy
Messages: 1903
Registered: November 2006
Senior Member
Lalit Kumar B wrote on Thu, 12 September 2013 14:37
That's a potential bug in the code with the way PRAGMA AUTONOMOUS_TRANSACTION is used. You are querying the table on which the trigger is defined.

Huh? Original poster queries system view "ALL_TAB_COLUMNS", but the trigger is on "LOG_DEV_TEST". As he takes columns for "log_dev_test" (yes, case matters), I doubt it will process anything useful.

I am also curious whether auditting of columns "LOAMID" and "LASTUPDATEUSER" is intended.
Re: Trigger for updating all columns of a table [message #595614 is a reply to message #595604] Thu, 12 September 2013 07:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" or "Preview Quick Reply" button to verify.
Also always post your Oracle version, with 4 decimals.

"PRAGMA AUTONOMOUS_TRANSACTION;" in a trigger is a bug and just show you don't know and understand what you are doing.

Regards
Michel
Re: Trigger for updating all columns of a table [message #596126 is a reply to message #595611] Wed, 18 September 2013 03:32 Go to previous message
Maaher
Messages: 7065
Registered: December 2001
Senior Member
Have a look at audit options on the database. I really don't think you want to do this (triggers in combination with dynamic SQL).

MHE
Previous Topic: error while using sys_refcursor in anonymous block
Next Topic: How to remove when others then
Goto Forum:
  


Current Time: Thu Apr 25 15:35:17 CDT 2024