Print a rollback query of a update query using a query - oracle

I need to print the rollback query of a update query.
My original query is
UPDATE EMPLOYEE SET NAME = 'SAMAN' WHERE ID=4;
The corresponding update for above query is
UPDATE EMPLOYEE SET NAME=(SELECT NAME FROM EMPLOYEE WHERE ID=4);
I need to print a query to rollback if above original query went wrong. I need it to print it via using a query also.
I'm using oracle 11g database.

You can do it by running a select query like below,
SELECT 'UPDATE EMPLOYEE SET NAME = '''||name||''' WHERE id = '||id||';' FROM employee;
and of course, run the select query before your update query.

As far as I understood the question, it is some kind of a log table you're looking for. I have no idea what you mean by "printing the rollback query"; never heard of anything like that.
So, let me demonstrate what I have on my mind. Review it, apply if it makes sense in your case. The code is commented, I hope you'll understand it
Prepare the scene:
SQL> -- Log table
SQL> create table emp_log
2 (id number constraint pk_el primary key,
3 empno number constraint fk_el_emp references emp (empno) not null,
4 datum date not null,
5 ename varchar2(20)
6 );
Table created.
SQL> -- A sequence which will be used to populate the ID column in the EMP_LOG table
SQL> create sequence seqa;
Sequence created.
SQL> -- Trigger; if new ENAME is different from the last one, log the change (i.e.
SQL> -- store the old ENAME)
SQL> create or replace trigger trg_bu_emp
2 before update on emp
3 for each row
4 begin
5 if :new.ename <> :old.ename then
6 insert into emp_log (id, empno, datum, ename)
7 values (seqa.nextval, :new.empno, sysdate, :old.ename);
8 end if;
9 end;
10 /
Trigger created.
OK, let's see how it works:
SQL> -- Some employees in department 10
SQL> select empno, ename from emp where deptno = 10;
EMPNO ENAME
---------- ----------
7782 CLARK
7839 KING
7934 MILLER
SQL> -- Update KING's name to a better one (just kidding)
SQL> update emp set ename = 'LITTLEFOOT' where empno = 7839;
1 row updated.
SQL> -- What's in the log?
SQL> select * From emp_log order by id desc;
ID EMPNO DATUM ENAME
---------- ---------- ------------------- --------------------
5 7839 30.03.2018 20:22:15 KING
SQL> -- I don't like the new name after all; return the previous one
SQL> update emp e set
2 e.ename = (select l.ename from emp_log l
3 where l.empno = e.empno
4 and l.id = (select max(l1.id) from emp_log l1
5 where l1.empno = l.empno
6 )
7 )
8 where e.empno = 7839;
1 row updated.
SQL> -- What we've done?
SQL> select empno, ename from emp where deptno = 10;
EMPNO ENAME
---------- ----------
7782 CLARK
7839 KING
7934 MILLER
SQL> -- What's in the log now?
SQL> select * From emp_log order by id desc;
ID EMPNO DATUM ENAME
---------- ---------- ------------------- --------------------
6 7839 30.03.2018 20:22:33 LITTLEFOOT
5 7839 30.03.2018 20:22:15 KING
SQL>

Related

Simple Stored Procedure for learning

My journey is progressing and I'm learning rapidly. I can't get enough of this stuff... alas I am at a dead end here and need some help.
I am running Visual Studio, am connected to a database (that's filled with dummy data). I am able to run queries on it as expected. However, I'm learning about Procedures right now and I'm coming up with a problem.
I am trying to simply run a query that will select all from table.
CREATE PROCEDURE nearthetop()
BEGIN
SELECT * FROM RESULTS WHERE VOLUME = (SELECT MAX(VOLUME) FROM RESULTS WHERE VOLUME NOT In (SELECT Max(VOLUME) from RESULTS))
END;
When I run this inside Visual Studio I get an error:
EXECUTE FAIL:
CREATE PROCEDURE twofromtop() BEGIN SELECT * FROM RESULTS WHERE VOLUME = (SELECT MAX(VOLUME) FROM RESULTS WHERE VOLUME NOT In (SELECT Max(VOLUME) from RESULTS)) END
Message :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 3
When I remove BEGIN and END; from the procedure it creates it fine.
Why is that?
And then, once created, how do I "call" that procedure so as I can see the returned results?
In Oracle, when PL/SQL code uses a SELECT statement, you have to select into something - a local variable, a collection, whatever - or use a cursor loop and deal with cursor variable.
You're selecting row(s) whose volume value is the 2nd largest. Code you wrote works, but it fetches from the same table 3 times which isn't optimal. For example (based on Scott's sample schema), fetching the 2nd largest salary:
SQL> select ename, sal from emp order by sal desc;
ENAME SAL
---------- ----------
KING 5000 --> largest
FORD 3000 --> Ford and Scott both share
SCOTT 3000 --> the 2nd largest salary
JONES 2975
BLAKE 2850
CLARK 2450
<snip>
Your query returns correct result:
SQL> select *
2 from emp
3 where sal = (select max(sal) from emp
4 where sal not in (select max(sal) from emp)
5 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 09.12.82 3000 20
7902 FORD ANALYST 7566 03.12.81 3000 20
Consider doing it differently:
SQL> select *
2 from (select e.*,
3 rank() over (order by sal desc) rnk
4 from emp e
5 )
6 where rnk = 2;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RNK
---------- ---------- --------- ---------- -------- ---------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 09.12.82 3000 20 2
7902 FORD ANALYST 7566 03.12.81 3000 20 2
SQL>
Now, back to your procedure. A simple option which doesn't require much effort is to use a cursor FOR loop. Procedure's IN parameter says which largest salary you want:
SQL> create or replace procedure nearthetop (par_n in number) as
2 begin
3 for cur_r in (select *
4 from (select e.*,
5 rank() over (order by sal desc) rnk
6 from emp e
7 )
8 where rnk = par_n
9 )
10 loop
11 dbms_output.put_line(cur_r.ename ||': '|| cur_r.sal);
12 end loop;
13 end;
14 /
Procedure created.
Testing:
SQL> set serveroutput on
SQL> begin
2 nearthetop(2); --> give me the 2nd largest salary
3 end;
4 /
SCOTT: 3000
FORD: 3000
PL/SQL procedure successfully completed.
SQL> begin
2 nearthetop(5); --> give me the 5th largest salary
3 end;
4 /
BLAKE: 2850
PL/SQL procedure successfully completed.
SQL>
I'm just displaying those values on the screen; you never said what you'd want to do with them.
If you'd like to return the result to the caller, a better option is to use a function instead of a procedure. In this case, it would be a ref cursor it returns.
SQL> create or replace function f_nearthetop (par_n in number)
2 return sys_refcursor
3 as
4 rc sys_refcursor;
5 begin
6 open rc for select *
7 from (select e.*,
8 rank() over (order by sal desc) rnk
9 from emp e
10 )
11 where rnk = par_n;
12 return rc;
13 end;
14 /
Function created.
Testing:
SQL> var l_rc refcursor
SQL>
SQL> exec :l_rc := f_nearthetop(2);
PL/SQL procedure successfully completed.
SQL> print :l_rc
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RNK
---------- ---------- --------- ---------- -------- ---------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 09.12.82 3000 20 2
7902 FORD ANALYST 7566 03.12.81 3000 20 2
SQL>
So, yes - there are various options. Which one you'll actually use depends on what you want to do.
(As of Visual Studio: I can't help about it, I don't use it.)

Procedure with exceptions

I have try to create procedure for updating row in table 'Departments'. I have to check if department name is unique. If is not unique it has to be stored in new table 'ERROR_DEPART'. I have tried to do that with exception but I fail to execute code.
CREATE PROCEDURE UPD_DEPARTMENT IS
v_depid department.department_id%TYPE;
v_depn department.department_name%TYPE;
v_lid department.location_id%TYPE;
v_phn employees.phone_number%TYPE;
BEGIN
select distinct departments.department_name, locations.location_id, employees.phone_number
into v_depn, v_lid, v_phn
from departments
inner join locations
on departments.location_id = locations.location_id
inner join employees
on departments.manager_id = employees.employee_id
END;
I'd suggest a unique index on department name column. Doing so, Oracle won't let you update any department name so that it would make a duplicate.
Here's an example.
First, test case, based on Scott's DEPT table:
SQL> create table depart as select * From dept;
Table created.
SQL> create unique index ui1_dept on depart (dname);
Index created.
SQL> create table error_depart as select * From depart where 1 = 2;
Table created.
SQL>
The procedure - in the exception handler section - inserts a row into the ERROR_DEPART table if uniqueness is violated.
SQL> create or replace procedure p_upd_dept (par_deptno in number, par_dname in varchar2)
2 is
3 begin
4 update depart d set
5 d.dname = par_dname
6 where d.deptno = par_deptno;
7
8 exception
9 when dup_val_on_index then
10 insert into error_Depart(deptno, dname) values (par_Deptno, par_dname);
11 end;
12 /
Procedure created.
SQL>
Testing: First, what we have now?
SQL> select * From depart;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> begin
2 p_upd_dept(10, 'SALES'); --> a duplicate
3 p_upd_dept(20, 'new dept'); --> not a duplicate
4 end;
5 /
PL/SQL procedure successfully completed.
SQL> select * from depart;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 new dept DALLAS --> not a duplicate - updated
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> select * From error_depart;
DEPTNO DNAME LOC
---------- -------------- -------------
10 SALES --> duplicate
SQL>

How to create a trigger to call the procedure with an update statement

I need to write a procedure and update trigger. When any update is done on the table, the trigger should make a call to the procedure. Procedure should update the changes in another table. In that another table old value, updated value should be there.
What you described sounds like an ordinary logging; you don't really need a procedure, trigger does it all. Here's an example:
SQL> create table emp_log (empno number, sal_old number, sal_new number);
Table created.
SQL> create or replace trigger trg_bu_emp
2 before update of sal on emp
3 for each row
4 begin
5 insert into emp_log (empno, sal_old, sal_new)
6 values
7 (:new.empno, :old.sal, :new.sal);
8 end;
9 /
Trigger created.
SQL> select empno, ename, sal from emp where ename = 'KING';
EMPNO ENAME SAL
---------- ---------- ----------
7839 KING 5000
SQL> update emp set sal = 7000 where ename = 'KING';
1 row updated.
SQL> select * from emp_log;
EMPNO SAL_OLD SAL_NEW
---------- ---------- ----------
7839 5000 7000
SQL>
[EDIT, after reading a comment]
Homework, eh? So - create a procedure:
SQL> rollback;
Rollback complete.
SQL> create or replace procedure p_emp_sal_log
2 (par_empno in emp.empno%type, par_sal_old in emp.sal%type,
3 par_sal_new in emp.sal%type)
4 is
5 begin
6 insert into emp_log (empno, sal_old, sal_new)
7 values
8 (par_empno, par_sal_old, par_sal_new);
9 end;
10 /
Procedure created.
SQL> create or replace trigger trg_bu_emp
2 before update of sal on emp
3 for each row
4 begin
5 p_emp_sal_log(:new.empno, :old.sal, :new.sal);
6 end;
7 /
Trigger created.
SQL> update emp set sal = 2000 where ename = 'KING';
1 row updated.
SQL> select * from emp_log;
EMPNO SAL_OLD SAL_NEW
---------- ---------- ----------
7839 5000 2000
SQL>

Change detection capture in Oracle DB tables

I wanted to if it is possible to detect changes in Oracle DB table for each column and its value and capture the change is a separate temporary table ?
Yes; people usually do that using triggers.
Here's a simple example:
SQL> create table dept_log
2 (deptno number,
3 dname varchar2(20),
4 loc varchar2(20),
5 when date
6 );
Table created.
SQL>
SQL> create or replace trigger trg_bu_dept
2 before update on dept
3 for each row
4 begin
5 if :new.dname <> :old.dname or
6 :new.loc <> :old.loc
7 then
8 insert into dept_log (deptno, dname, loc, when)
9 values (:new.deptno, :old.dname, :old.loc, sysdate);
10 end if;
11 end;
12 /
Trigger created.
SQL> select * from dept;
DEPTNO DNAME LOC
---------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> update dept set loc = 'LONDON' where deptno = 40;
1 row updated.
SQL> select * From dept_log;
DEPTNO DNAME LOC WHEN
---------- -------------------- -------------------- -------------------
40 OPERATIONS BOSTON 11.04.2018 22:00:23
SQL>

Need Column name , old value and new value in Trigger

Our requirement is if any 1 of the column name in table is updating we need to insert the column name in another table so I had written this code
CREATE OR REPLACE TRIGGER Test AFTER
UPDATE ON XX_table
FOR EACH Row
BEGIN FOR C IN
(SELECT column_name
FROM User_Tab_Columns
WHERE Upper(Table_Name) = 'XX_table_name' ORDER BY column_id ASC)
LOOP
IF Updating (c.column_name)
THEN
INSERT INTO Xx_Trigger_table (Rt_Id ,Updated_Column ,updated_status) VALUES(:Old.Rt_Id,C.Column_Name,'Y');
END IF;
END LOOP;
END;
Now Client need Old value as well as new value in the XX_Trigger_Table.I cant Write
INSERT INTO Xx_Trigger_table (Rt_Id ,Updated_Column ,updated_status,old_value, new_value) VALUES(:Old.Rt_Id,C.Column_Name,'Y',:old.c.column_name,:new.c.column_name);
Please suggest me some idea to insert new and old value in the table.
Thanks in Advance.
If you are following this approach , like #Alex Poole suggested you will have to include all the columns that you would like to log. Please see this procedure and the output of it.
CREATE OR REPLACE TRIGGER Test1 AFTER
UPDATE ON TESTEMP
FOR EACH Row
BEGIN FOR C IN
(SELECT column_name
FROM User_Tab_Columns
WHERE Upper(Table_Name) = 'TESTEMP' ORDER BY column_id ASC)
LOOP
IF Updating (c.column_name) then
IF (c.column_name='EMPNO') then
INSERT INTO test_audit (col_name,old_val,new_val,upd_stat) VALUES (C.column_name,:Old.empno,:New.empno,'Y');
ELSIF
(c.column_name='ENAME') then
INSERT INTO test_audit (col_name,old_val,new_val,upd_stat) VALUES (C.column_name,:Old.ENAME,:New.ENAME,'Y');
ELSIF
(c.column_name='MGR') then
INSERT INTO test_audit (col_name,old_val,new_val,upd_stat) VALUES (C.column_name,:Old.MGR,:New.MGR,'Y');
END IF;
END IF;
END LOOP;
END;
SQL> select * from testemp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
SQL> select * from test_audit;
no rows selected
SQL> update testemp set empno=6677 , ename='JUPITER' where empno=1234;
1 row updated.
SQL> COMMIT;
Commit complete.
SQL> select * from test_audit;
COL_NAME OLD_VAL NEW_VAL U
---------- ---------- ---------- -
EMPNO 1234 6677 Y
ENAME SMITH JUPITER Y

Resources