Store procedure to take Back up operation of certain column values.

Hi ,
Can anyone please tell me how to do this?
I have three table 1:emp(empno,ename,deptno,sal)
2:emp_changes(empno,changetime(timestamp),operation(varchar2))
3:emp_metadata(tablename(varchar),Maxtime(timestamp))
I want to create a trigger which will track all the DML operation on emp table and insert one row for each operation having empno(as primary in emp) ,timstamp of operation and operation name (Insert,update or delete) in emp_changes table.
As well as it will insert or update in emp_metadata table with name of the table and max time from emp_changes table.
emp_changes table will have only one row.if there is no row it will insert otherwise it will update that row.
can anone please write this trigger for me?
Thanks,
Deekay.
Edited by: Deekay on Aug 9, 2010 4:15 AM

Try this:
SQL> create table emp(empno number,ename varchar2(10),sal number);
Table created.
SQL> create table emp_Change(empno number,changetime timestamp,operation varchar2(10));
Table created.
SQL> create table emp_metadata(tablename varchar2(1000),maxtime timestamp);
Table created.
SQL> create or replace trigger my_emp_trig after insert or update or delete on emp
  2  for each row
  3  begin
  4  IF INSERTING THEN
  5  /
Warning: Trigger created with compilation errors.
SQL> ed
Wrote file afiedt.buf
  1  create or replace trigger my_emp_trig after insert or update or delete on emp
  2  for each row
  3  v_empno NUMBER := 0;
  4  begin
  5  BEGIN
  6  SELECT empno INTO v_empno FROM emp_Change where empno = :new.empno;
  7  exception
  8  when no_Data_found then
  9  v_empno := 0;
10  end;
11  IF (v_empno = 0) THEN
12  IF INSERTING THEN
13  insert into emp_change values(:new.empno,SYSTIMESTAMP,'INSERT');
14  ELSIF UPDATING THEN
15  insert into emp_change values(:new.empno,SYSTIMESTAMP,'UPDATE');
16  ELSE
17  insert into emp_change values(:new.empno,SYSTIMESTAMP,'DELETE');
18  END IF;
19  ELSE
20  IF INSERTING THEN
21  update emp_change set operation='INSERT',changetime=SYSTIMESTAMP
22  WHERE empno = :new.empno;
23  ELSIF UPDATING THEN
24  update emp_change set operation='UPDATE',changetime=SYSTIMESTAMP
25  WHERE empno = :new.empno;
26  ELSE
27  update emp_change set operation='DELETE',changetime=SYSTIMESTAMP
28  WHERE empno = :new.empno;
29  END IF;
30  END IF;
31* end;
32  /
v_empno NUMBER := 0;
ERROR at line 3:
ORA-04079: invalid trigger specification
SQL> ed
Wrote file afiedt.buf
  1  create or replace trigger my_emp_trig after insert or update or delete on emp
  2  for each row
  3  declare
  4  v_empno NUMBER := 0;
  5  begin
  6  BEGIN
  7  SELECT empno INTO v_empno FROM emp_Change where empno = :new.empno;
  8  exception
  9  when no_Data_found then
10  v_empno := 0;
11  end;
12  IF (v_empno = 0) THEN
13  IF INSERTING THEN
14  insert into emp_change values(:new.empno,SYSTIMESTAMP,'INSERT');
15  ELSIF UPDATING THEN
16  insert into emp_change values(:new.empno,SYSTIMESTAMP,'UPDATE');
17  ELSE
18  insert into emp_change values(:new.empno,SYSTIMESTAMP,'DELETE');
19  END IF;
20  ELSE
21  IF INSERTING THEN
22  update emp_change set operation='INSERT',changetime=SYSTIMESTAMP
23  WHERE empno = :new.empno;
24  ELSIF UPDATING THEN
25  update emp_change set operation='UPDATE',changetime=SYSTIMESTAMP
26  WHERE empno = :new.empno;
27  ELSE
28  update emp_change set operation='DELETE',changetime=SYSTIMESTAMP
29  WHERE empno = :new.empno;
30  END IF;
31  END IF;
32* end;
SQL> /
Trigger created.
SQL> insert into emp values(1,'A',10000);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from emp;
     EMPNO ENAME             SAL
         1 A               10000
SQL> select * from emp_change;
     EMPNO
CHANGETIME
OPERATION
         1
06-AUG-10 06.42.37.309898 AM
INSERT
SQL> update emp set sal=200000 where empno = 1;
1 row updated.
SQL> commit;
Commit complete.
SQL> set linesize 10000
SQL> select * from emp_change;
     EMPNO CHANGETIME                                                                  OPERATION
         1 06-AUG-10 06.43.05.368925 AM                                                UPDATE
SQL> insert into emp values(2,'B',123456);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from emp;
     EMPNO ENAME             SAL
         1 A              200000
         2 B              123456
SQL> select * from emp_change;
     EMPNO CHANGETIME                                                                  OPERATION
         1 06-AUG-10 06.43.05.368925 AM                                                UPDATE
         2 06-AUG-10 06.43.29.383845 AM                                                INSERTSimilarly you can tryout for emp_metadata

Similar Messages

  • Performance operations based on Column values in SQL server 2008

    Hi ,
    I have a table which consist of following columns
    ID    Formula              
    Values                 
    DisplayValue
    1                    
    a*b/100       100*12/100    
          null
    2                    
    b*c/100       
    12*4/100              
    null
    I want to perform operation based on column "Values" and save data after operations in new column Name "Display Value" .i.e I want to get the below result . Can anyone please help.
    ID    Formula              
    Values                 
    DisplayValue
    1                    
    a*b/100       100*12/100    
          12
    2                    
    b*c/100       
    12*4/100             
    0.48
    Thanks for the help.
    Regards, Priti A

    Try this,
    create table #mytable (ID int,Formula varchar(10), [Values] varchar(10), DisplayValue decimal(10,4))
    insert into #mytable values(1 ,'a*b/100','100*12/100',null)
    insert into #mytable values(2 ,'b*c/100','12*4/100',null)
    declare @rowcount int=1
    while @rowcount <= (select max(id) from #mytable)
    begin
    declare @expression nvarchar(max)
    select @expression=[values] from #mytable where id = + @rowcount
    declare @sql nvarchar(max)
    set @sql = 'select @result = ' + @expression
    declare @result decimal(10,4)
    exec sp_executesql @sql, N'@result decimal(10,4) output', @result = @result out
    update #mytable set DisplayValue= @result where id = @rowcount
    set @rowcount=@rowcount+1
    end
    select * from #mytable
    Regards, RSingh

  • Store would not take back by broken studio beats cause the serial number and box didnt match

    so i went to return my studio wireless beats today cause the right side volume was not working.  the waterword lakes store manager would not take them cause the box and headphones serial numbers did not match and accused me and having more than one pair.  These are the only beats i own and have ever owned  they were bought online from best buy and box was sealed by my wife.  so how would you ever know the serial numbers dont match?  Open it in front of a best buy manager before you leave??  said for me to call beats myself.  So $360 bucks down the drain? They were probably refurbed and boxed during black friday is my guess.  not good best buy

    Hello and welcome to the forum coljeski,
    As someone who loves her Beats, I'm sorry to hear yours are not working well. They definitely are not a small investment, and I would be very frustrated also if I was told they could not be exchanged. It sounds like you might have received some misinformation, and I would be happy to look into this further. 
    Using the e-mail you registered on the forum, I was unable to locate your information or order history, so I have a couple of questions to help me assist you better. When did you purchase your Beats? Did you purchase the Geek Squad Protection plan (GSP) for them? Or were you still within the return/exchange period when you went to exchange them? To further clarify, if you did not purchase a GSP then you would have to contact the manufacturer directly for service. If you did purchase a GSP please send me a private message with your name, phone number we have on file for you, and the GSP plan number if you have it. To send me a private message simply click to the right of my signature below. 
    I hope this helps clarify things, and please do not hesitate to let me know if you have any further questions or concerns. 
    Regards, 

  • Re: store would not take back by broken studio beats cause the serial number and box didnt match

    I just came across the same issue! Tried to exchange a defective Studio model and the serial numbers did not match! How do know if Best Buy is not selling refurbished / fake headphones. I'm very disappointed with the service and quality of the headphones. I will be sure to let my friends and followers to avoid Best Buy / Beats. It's worth mentioning the fact that Steve - Assistant Manager seemed genuinely concern about that issue. Nice guy but he doesn't have the tools / authority to make a customer who spends thousands @ Best Buy whole. I don't mind taking my biz to Amazon

    Hello Techblogger45,
    I'm sorry to read your Beats headphones have developed a defect. While I'm happy to hear that Steve did his best on your behalf, I regret that he wasn't able to offer you the solution you were expecting.
    I was unable to locate your purchase using the information you provided when you signed up for Best Buy Unboxed, so I have some questions for you: when did you purchase your Beats? Did you purchase Geek Squad Protection (GSP) for them or were you still within the return/exchange period when you visited your local store? If you did not purchase GSP, I recommend that you contact Apple Support directly for options under the manufacturer warranty. If you did purchase a GSP, please send me a private message with your name, telephone number, and your GSP plan ID number if you have it. I'll do my best to help.
    I hope this helps clarify things, and please do not hesitate to let me know if you have any further questions or concerns. 
    Regards, 

  • Solaris 10_u8 upgrade slow down mysql store procedure

    Hi,
    Recently we upgraded the following and found out that MySQL store procedure takes more than 40 times longer to run:
    Old configurations:
    - Hardware T5120
    - Solaris 10_u4 8/7
    - MySQL 5.1.30
    - Primary service domain has 8 ldoms, primary has 4 cpus and 2G mem
    - Ldom mgr 1.0.3
    - Database server sits on LDOM2 with 12 vcpus and 8G mem, two image file disk export from primary, basically the whole 2nd disk raid 1 for LDOM2, one 15G for OS and other 100G for database
    - Store procedure only takes 0.87 seconds to finish
    New configurations:
    - everything is same as above, except I patched the primary to kernel patch 142900-02, then the mysql store procedure in LDOM2 takes about 35 seconds to finish
    I have tried all different kind of combinations, from the above configuration I think there might be some kernel parameters settings changed since the upgrade but not sure what are they. If any one know or has some kind issues please let me know. Any help would be appreciated.
    Thanks!

    Hello.
    Patch 142900-02 is old. You can try install 142909-17. This patch obsolete 142900.
    For rest you can remove patch 142900-02 and check performance.
    Regards.

  • Execute store procedure from form

    How do I execute a store procedure from FORM
    I have a store procedure in the database
    all it does insert a value to table test.
    in 'WHEN-NEW-INSTANT-TRIGGER' of the form MODLUE I put
    test_curor; (name of the store procedure)
    The module does have block or any thing it.
    There was no syntax errors or form error
    but it did not insert into the table.
    Why????

    I just tested this myself and you are right. If you have one block with no items in it the when-new-form-instance trigger will not be fired. I got a message back saying 'block had no items', though it ran OK. This kinda makes logical sense, since there is nothing for the form to do. I put a button onto the block and the procedure fired no problem. I must admit I have never tried creating a form with no items in it so I haven't seen this behaviour before.
    Shucks you live and learn every day!

  • Get the body of the store procedure

    Hi All,
    THe store procedure is created successfully and I don't know how to get the definition of the store procedure. In MSSQL server I used "sp_helptext <store procedure name>" but I don't know how to get text(description) of the store procedure in ORACLE. Your help is greatly appreciated.
    Thanks,
    JP

    Hi all,
    I ran the following statement to get the body of the store procedure:
    select text from user_source where name = <store procedure>
    and the long text(body of store procedure) is returned back. This store procedure is created with the error message return back as "Warning: Procedure created with compilation errors". So I were wondering why the store procdure is in user_source although the creation of this store procedure is returned an error message as mentioned above. Is there anyway that we can find out if the store procedure is created with the compilation errors.
    Thanks,
    JP

  • Store procedure to be triggered before approval procedure.

    Dear Experts,
    This is with reference to Approval Procedure and store procedure. I have created the store procedure which validates the series with user defined field value.
    If there is difference in the field value of series & user defined field, system will block the entry ( this is defined in store procedure) & also approval procedure is also defined for the same document (i.e. Document value greater than 1 Lakh).
    Now the problem, on creation of documents system trigger the Approval procedure and the document is sent for approval; but the store procedure defined is not reflected before approval & after the approval user cannot change series.
    Is there any way the store procedure can validate the value before approval procedure is triggered?
    Thanks & Regards,
    Yogesh Jadav.

    Hi Raj,
    Below are the queries for Purchase Order & Purchase Order Draft.
    Purchase Order Draft----
    if @object_type='22' and (@transaction_type='A' or @transaction_type='U')
    begin
    if exists(select T0.docentry from odrf T0 inner join NNM1 T1 on T0.series=T1.series where (T1.seriesname like 'Ord%') and (T0.U_Stage<>'Order') and t0.objtype='22' and T0.docentry=@list_of_cols_val_tab_del)
    begin
    select @error=1, @error_message='Series name and category should be both order'
    end
    end
    Purchase Order----
    if @object_type='22' and (@transaction_type='A' or @transaction_type='U')
    begin
    if exists(select T0.docentry from opor T0 inner join NNM1 T1 on T0.series=T1.series where (T1.seriesname like 'Ord%') and (T0.U_Stage<>'Order') and T0.docentry=@list_of_cols_val_tab_del)
    begin
    select @error=1, @error_message='Series name and category should be both order'
    end
    end
    Yogesh Jadav

  • Oralce Store Procedure not returning values in out parameters

    I am goig to execute an oracle store procedure using C# through through ODP.NET. The store procedure executes succsfully but it does not return any value in the out parameter.
    When I execute the same store procedure with the same input parameteres from within SQL*Plus. It does return values.
    This is to be clearify here that in the oracle store procedure the data type for the out parameter is number and in the c# the variavle that will hold its value is Integer. I also tried using Long on the C# side but it is still not working. After the successfull execution of the store procedure the C# variable contains nulll value.

    I am goig to execute an oracle store procedure using C# through through ODP.NET. The store procedure executes succsfully but it does not return any value in the out parameter.
    When I execute the same store procedure with the same input parameteres from within SQL*Plus. It does return values.
    This is to be clearify here that in the oracle store procedure the data type for the out parameter is number and in the c# the variavle that will hold its value is Integer. I also tried using Long on the C# side but it is still not working. After the successfull execution of the store procedure the C# variable contains nulll value.

  • My brand new iPad has flickering lines across the centre of the screen. It has just been taken out of it's box! Has anyone experienced this? Any ideas or simply take back to store?

    My brand new iPad has flickering lines across the centre of the screen. It has just been taken out of it's box! Has anyone experienced this? Any ideas or simply take back to store?

    This shouldn't happen. Take it back since it's new.

  • The app store will not take my security code on the back of my credit card

    The app store will not take my security code on the back of the credit card

    When there is a mismatch between the iTunes Store's records and your credit card company's records, you will receive an error message stating that your credit card's security code isn't valid or that your zip code does not match your bank's records. http://support.apple.com/kb/TS1646

  • I lost my application store, how to take it back

    I lost my application store, how to take it back

    Sorry, I don't understand what you're asking, so you'll need to explain further. If this question is about something do to with your iPhone, though, you should ask the question in the iPhone forums. This forum is for questions about the Communities themselves, not for technical questions about a product.
    Regards.

  • Deski report which is using a Store Procedure - Redirecting connection

    I have a DeskI report which is using a Store Procedure as its data provider.
    This is an Oracle based Store Procedure.
    Where can i see wich connection (connections listed in CMC) is used in DeskI report ?
    How can i redirect one connection to point to another on report?
    Thanks
    Georg

    Marina,
    The strategy for displaying LOV via Infoview needs to be different than what you've set up via DeskI.  The main difference is that when using a client/server program like DeskI, the LOV is cached to the client, thus producing a list more quickly, however, over the web, the web client does not cache LOV as readily.  To "solve" this "problem" you need to go back to your universe and trace what is happening.  If the LOV is getting generated through an essential "select distinct" on the fact/dimension table, and that table has over a million rows, then it will take some time to generate the list (and thus will timeout).  A different strategy to take is to maintain a "backend lookup" table that the LOV will point to instead.  The backend lookup table will contain the unique instances of the column getting scanned and thus when the LOV points to the new lookup table the response will be quicker versus the old method.  Care must be taken each time new data is loaded to WH to ensure that any new unique values from the "home" table/column are also placed in the backend lookup table.
    thanks,
    John

  • Execute Unix Mail Command from Store Procedure

    Hi Colleagues!
    I designed a store procedure that Monitoring some Critical tables in my Oracle 8.0.6.1.0 Database But I need to send some Emails from the Store Procedures to Information System Staff when the amount of rows increase too much. I read about UTL_SMTP but my database version is too old to run this package. Also I have the option of Maildemo8i that execute MS Oulook from a Client System But I looking to Send it from my Database(OS HPUX). So please take a look of this issue and I will appreciate any help.
    Your Friend.
    Emmanuel Carrillo Trejos.
    LNB, Republic of Panama(Central America).
    P.D. You can email to: [email protected]

    Do you mean that you want to execute mail or mailx unix commands to send mails.If i am right then probably you are looking for a way to execute operating system commands from your procedure.
    You can refer this link if you want to do that. You need java support in your Oracle database and i believe that Oracle 8 does have jvm running in the instance.
    http://asktom.oracle.com/pls/ask/f?p=4950:8:5079825575573830264::NO::F4950_P8_DISPLAYID,F4950_P8_CRITERIA:952229840241
    Thank you!

  • Executing Store Procedure random timeouts

    We have some simple stored procedures that run find when run under the Advantage Data Architect, but randomly times out when we call them from our ASP web site using the OLEDB driver.
    When we try to call them using the Advantage Java database driver we get and error code 9105 SQL state S0000 error with no descripive message.
    Does anyone have any insights into either of these issues?
    Dave Anderson

    Your stored procedure only takes Milliseconds to finish normally?
    How do you know it is timing out?  The default is 30 seconds so seeing it timeout I would have assumed it was a longer running one.  Are you getting the ADS error 7209 back or Run-Time error
    '3712' Operation has been cancelled by the user?
    It's been ages since I've looked at VB, but I think it should be something like this
    cmd = New ADODB.Command
    cmd.CommandText = "execute procedure ....."
    cmd.CommandType = adCmdText
    cmd.CommandTimeout = 0
    rs = cmd.Execute
    For the JDBC issue, it is difficult to tell by the date, but I think that date indicates likely a 10.x or 11.x driver.  The issue I was thinking of I believe was fixed in 9.x.
    You can try the latest 11.1 version (11.10.0.20) as I think that was released in July of this year.  Maybe it will help?  If not a support incident would be a good idea (particularly if you can duplicate it)
    Out of curiosity are you using the varying output type stored procedures?  (Not that I have anything to offer as a workaround, but the error is indicating there was a problem retrieving column informaiton and this is the only thing I can think of off the top of my head).
    Edgar

Maybe you are looking for