Home » SQL & PL/SQL » SQL & PL/SQL » log_table
log_table [message #627270] Mon, 10 November 2014 05:34 Go to next message
sanodani
Messages: 98
Registered: October 2014
Member
Hallo
i am trying to create log table for employee table, but i am getting an error (ora-04082)while executing Trigger
can any one help me please?
thanking you. Smile

my code:

Create table employees_log as
(
Select  employees.*
from employees
where employee_id = 12345000
);

Alter table employees_log
 Add (
        log_id Number(4),
        log_oper Varchar(50),
        log_time Date,
        log_user Varchar(50)
     );

Create Sequence emp_log_seq
increment by 1
start with 100
nocache;


Create or replace trigger emp_change_trig
  after update or delete
  on Employees_log
Declare
  vlog_id number(4) := emp_log_seq.nextval;
  vlog_action varchar(50);
  vlog_time date := sysdate;
  vlog_user varchar(50) := sys_context('userenv', 'current_user');
Begin
  if updating then
    vlog_action := 'Update';
  elsif deleting then
    vlog_action := 'Delete';
  end if;
  
  insert into Employees_log     
      values (:old.employee_id, :old.first_name, :old.last_name, :old.email, :old.phone_number, :old.hire_date, :old.job_id
                , :old.salary, :old.commission_pct, :old.manager_id, :old.department_id, vlog_id, vlog_action, vlog_time,   
vlog_user);
    
end;

[Updated on: Mon, 10 November 2014 05:38]

Report message to a moderator

Re: log_table [message #627271 is a reply to message #627270] Mon, 10 November 2014 05:40 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
were the answers to your previous questions of any use to you? You have forgotten to update the topics to say so. And when you post code, please enclose it within [code] tags to make it readable, as described here, How to use [code] tags and make your code easier to read
Re: log_table [message #627272 is a reply to message #627270] Mon, 10 November 2014 05:40 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
Please read and follow How to use [code] tags and make your code easier to read?

You've created a statement level trigger, you can't reference new and old values in a statement level trigger, you need to use a row level trigger instead.
Re: log_table [message #627273 is a reply to message #627272] Mon, 10 November 2014 05:44 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
ya sorry for that, i am new here in this site, well i have tried it to modify as per your guidence thank u Smile
yes, i ma just improving my practice skill.

@cookiemonster:
how can i do this in row Level Trigger ?
Re: log_table [message #627274 is a reply to message #627273] Mon, 10 November 2014 05:50 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Database Application Developer's Guide - Fundamentals
Chapter 9 Coding Triggers

Re: log_table [message #627276 is a reply to message #627273] Mon, 10 November 2014 06:31 Go to previous messageGo to next message
cookiemonster
Messages: 13920
Registered: September 2008
Location: Rainy Manchester
Senior Member
sanodani wrote on Mon, 10 November 2014 11:44


@cookiemonster:
how can i do this in row Level Trigger ?


Code should work as it is in a row level trigger.
Re: log_table [message #627277 is a reply to message #627276] Mon, 10 November 2014 06:32 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Except that it on the wrong table.
Re: log_table [message #627289 is a reply to message #627277] Mon, 10 November 2014 12:35 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
thankyou for your suggestion and help Smile i have solved my problem..
Re: log_table [message #627290 is a reply to message #627289] Mon, 10 November 2014 12:37 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Can tell and show us how.

Re: log_table [message #627313 is a reply to message #627290] Mon, 10 November 2014 15:32 Go to previous messageGo to next message
sanodani
Messages: 98
Registered: October 2014
Member
yes definitely,
i have added a line (after "..on employees" above deceleration part)

referencing new as new old as old
for each row

and have corrected the table name as well Smile
Re: log_table [message #627314 is a reply to message #627313] Mon, 10 November 2014 15:49 Go to previous message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
sanodani wrote on Mon, 10 November 2014 16:32

referencing new as new old as old



This cracks me up that people still use that redundant line.
It's like saying "Dave, I am now going to refer to your as Dave, and Bill I am now going to refer to you as Bill."
Previous Topic: Delayed update to global variable via pipelined function?
Next Topic: Oracle-Should I use temp table or ref cursor
Goto Forum:
  


Current Time: Fri Apr 26 00:22:46 CDT 2024