create audit trail using oracle trigger - oracle

I am trying to create a trigger that will audit all new and old columns or each row updated, inserted. here is the table
ID NUMBER(10,0) No 1
ENTITY_ID NUMBER(10,0) No 2
LOCATION_ID NUMBER(10,0) Yes 3
NAME VARCHAR2(128 CHAR) No 4
CREATED_BY VARCHAR2(255 CHAR) No 'unknown' 5
UPDATED_BY VARCHAR2(255 CHAR) No 'unknown' 6
TS_CREATED TIMESTAMP(6) No "SYSTIMESTAMP
" 7
I am not sure how to write the trigger.
CREATE OR REPLACE TRIGGER audit_security_zones
BEFORE DELETE OR INSERT OR UPDATE ON security_zones
FOR EACH ROW
DECLARE
BEGIN
END;
I also want to use client identifier senbt from application. Here is my audit trail table.
ID NUMBER(10,0) No 1
ACTION VARCHAR2(20 BYTE) Yes 2
TABLE_ID VARCHAR2(100 BYTE) Yes 3
OLD_VALUE VARCHAR2(1000 BYTE) Yes 4
NEW_VALUE VARCHAR2(1000 BYTE) Yes 5
USERNAME VARCHAR2(100 BYTE) Yes 6
TS_UPDATED TIMESTAMP(6) No systimestamp 7
Is this a good way to do this? I will then render a view for the user based on each table to show them what has changed.
UPDATE
I am trying to achieve something similar to the following
CREATE OR REPLACE TRIGGER audit_security_zones
BEFORE DELETE OR INSERT OR UPDATE ON security_zones
FOR EACH ROW
DECLARE
var_action CHAR(1);
BEGIN
IF INSERTING THEN var_action := 'INSERT';
ELSIF UPDATING THEN var_action := 'UPDATE';
ELSE var_action := 'DELETE';
END IF;
/* loop eahc column in the update or insert or delete statement executed */
for each col in stmt/row{
INSERT INTO audit_table
(
ACTION,
TABLE_ID,
OLD_VALUE,
NEW_VALUE,
USERNAME,
TS_CREATED
) VALUES (
var_action,
:OLD.ID,
:OLD.columnVALUE,
:new.COLUMNVALUE,
ociidentifier,
systimestamp
)
}
END;
UPDATE2
CREATE OR REPLACE TRIGGER audit_security_zones
BEFORE DELETE OR INSERT OR UPDATE ON security_zones
FOR EACH ROW
DECLARE
var_action CHAR(6);
BEGIN
IF INSERTING THEN var_action := 'INSERT';
ELSIF UPDATING THEN var_action := 'UPDATE';
ELSE var_action := 'DELETE';
END IF;
IF var_action = 'DELETE' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
O_ENTITY_ID,
O_LOCATION_ID,
O_NAME,
O_CREATED_BY,
O_UPDATED_BY,
O_TS_CREATED
) VALUES (
var_action,
USERNAME,
:OLD.ID,
:OLD.ENTITY_ID,
:OLD.LOCATION_ID,
:OLD.NAME,
:OLD.CREATED_BY,
:OLD.UPDATED_BY,
:OLD.TS_CREATED
);
ELSEIF var_action = 'INSERT' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
N_ENTITY_ID,
N_LOCATION_ID,
N_NAME,
N_CREATED_BY,
N_UPDATED_BY
N_TS_CREATED
) VALUES (
var_action,
USERNAME,
:NEW.ID,
:NEW.ENTITY_ID,
:NEW.LOCATION_ID,
:NEW.NAME,
:NEW.CREATED_BY,
:NEW.UPDATED_BY,
:NEW.TS_CREATED
);
ELSEIF var_action = 'UPDATE' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
O_ENTITY_ID,
O_LOCATION_ID,
O_NAME,
O_CREATED_BY,
O_UPDATED_BY,
O_TS_CREATED,
N_ENTITY_ID,
N_LOCATION_ID,
N_NAME,
N_CREATED_BY,
N_UPDATED_BY
N_TS_CREATED
) VALUES (
var_action,
USERNAME,
:OLD.ID,
:OLD.ENTITY_ID,
:OLD.LOCATION_ID,
:OLD.NAME,
:OLD.CREATED_BY,
:OLD.UPDATED_BY,
:OLD.TS_CREATED,
:NEW.ENTITY_ID,
:NEW.LOCATION_ID,
:NEW.NAME,
:NEW.CREATED_BY,
:NEW.UPDATED_BY,
:NEW.TS_CREATED
);
END IF;
END;
I get the following errors
Error(31,9): PLS-00103: Encountered the symbol "VAR_ACTION" when expecting one of the following: := . ( # % ;
Error(42,4): PLS-00103: Encountered the symbol "N_TS_CREATED" when expecting one of the following: . ) , # The symbol "." was substituted for "N_TS_CREATED" to continue.
Error(54,12): PLS-00103: Encountered the symbol "VAR_ACTION" when expecting one of the following: := . ( # % ;
Error(71,4): PLS-00103: Encountered the symbol "N_TS_CREATED" when expecting one of the following: . ) , # The symbol "." was substituted for "N_TS_CREATED" to continue.
Error(90,4): PLS-00103: Encountered the symbol ";" when expecting one of the following: if
UPDATE3
CREATE OR REPLACE TRIGGER audit_security_zones
BEFORE DELETE OR INSERT OR UPDATE ON security_zones
FOR EACH ROW
DECLARE
var_action CHAR(6);
BEGIN
IF INSERTING THEN var_action := 'INSERT';
ELSIF UPDATING THEN var_action := 'UPDATE';
ELSE var_action := 'DELETE';
END IF;
IF var_action = 'DELETE' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
O_ENTITY_ID,
O_LOCATION_ID,
O_NAME,
O_CREATED_BY,
O_UPDATED_BY,
O_TS_CREATED
) VALUES (
var_action,
USERNAME,
:OLD.ID,
:OLD.ENTITY_ID,
:OLD.LOCATION_ID,
:OLD.NAME,
:OLD.CREATED_BY,
:OLD.UPDATED_BY,
:OLD.TS_CREATED
);
ELSE IF var_action = 'INSERT' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
N_ENTITY_ID,
N_LOCATION_ID,
N_NAME,
N_CREATED_BY,
N_UPDATED_BY,
N_TS_CREATED
) VALUES (
var_action,
USERNAME,
:NEW.ID,
:NEW.ENTITY_ID,
:NEW.LOCATION_ID,
:NEW.NAME,
:NEW.CREATED_BY,
:NEW.UPDATED_BY,
:NEW.TS_CREATED
);
ELSE IF var_action = 'UPDATE' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
O_ENTITY_ID,
O_LOCATION_ID,
O_NAME,
O_CREATED_BY,
O_UPDATED_BY,
O_TS_CREATED,
N_ENTITY_ID,
N_LOCATION_ID,
N_NAME,
N_CREATED_BY,
N_UPDATED_BY,
N_TS_CREATED
) VALUES (
var_action,
USERNAME,
:OLD.ID,
:OLD.ENTITY_ID,
:OLD.LOCATION_ID,
:OLD.NAME,
:OLD.CREATED_BY,
:OLD.UPDATED_BY,
:OLD.TS_CREATED,
:NEW.ENTITY_ID,
:NEW.LOCATION_ID,
:NEW.NAME,
:NEW.CREATED_BY,
:NEW.UPDATED_BY,
:NEW.TS_CREATED
);
END IF;
END;
error i get now is
Error(90,4): PLS-00103: Encountered the symbol ";" when expecting one of the following: if
UPDATE4
CREATE OR REPLACE TRIGGER audit_security_zones
BEFORE DELETE OR INSERT OR UPDATE ON security_zones
FOR EACH ROW
DECLARE
var_action CHAR(6);
BEGIN
IF INSERTING THEN var_action := 'INSERT';
ELSIF UPDATING THEN var_action := 'UPDATE';
ELSE var_action := 'DELETE';
END IF;
IF var_action = 'DELETE' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
O_ENTITY_ID,
O_LOCATION_ID,
O_NAME,
O_CREATED_BY,
O_UPDATED_BY,
O_TS_CREATED
) VALUES (
var_action,
'test',
:OLD.ID,
:OLD.ENTITY_ID,
:OLD.LOCATION_ID,
:OLD.NAME,
:OLD.CREATED_BY,
:OLD.UPDATED_BY,
:OLD.TS_CREATED
);
ELSIF var_action = 'INSERT' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
N_ENTITY_ID,
N_LOCATION_ID,
N_NAME,
N_CREATED_BY,
N_UPDATED_BY,
N_TS_CREATED
) VALUES (
var_action,
'test',
:NEW.ID,
:NEW.ENTITY_ID,
:NEW.LOCATION_ID,
:NEW.NAME,
:NEW.CREATED_BY,
:NEW.UPDATED_BY,
:NEW.TS_CREATED
);
ELSIF var_action = 'UPDATE' THEN
INSERT INTO audit_table
(
ACTION,
USERNAME,
ID,
O_ENTITY_ID,
O_LOCATION_ID,
O_NAME,
O_CREATED_BY,
O_UPDATED_BY,
O_TS_CREATED,
N_ENTITY_ID,
N_LOCATION_ID,
N_NAME,
N_CREATED_BY,
N_UPDATED_BY,
N_TS_CREATED
) VALUES (
var_action,
'test',
:OLD.ID,
:OLD.ENTITY_ID,
:OLD.LOCATION_ID,
:OLD.NAME,
:OLD.CREATED_BY,
:OLD.UPDATED_BY,
:OLD.TS_CREATED,
:NEW.ENTITY_ID,
:NEW.LOCATION_ID,
:NEW.NAME,
:NEW.CREATED_BY,
:NEW.UPDATED_BY,
:NEW.TS_CREATED
);
END IF;
END;
new error i get
Error(9,3): PL/SQL: SQL Statement ignored
Error(19,4): PL/SQL: ORA-00904: "O_TS_CREATED": invalid identifier
Error(32,3): PL/SQL: SQL Statement ignored
Error(42,4): PL/SQL: ORA-00904: "N_TS_CREATED": invalid identifier
Error(55,9): PL/SQL: SQL Statement ignored
Error(71,4): PL/SQL: ORA-00904: "N_TS_CREATED": invalid identifier

You cannot "loop" through the OLD and NEW values, you have to reference each one explicitly i.e.
INSERT INTO audit_table
(
ACTION,
TABLE_ID,
OLD_VALUE,
NEW_VALUE,
USERNAME,
TS_CREATED
) VALUES (
var_action,
:OLD.ID,
:OLD.column1VALUE,
:new.COLUMN1VALUE,
ociidentifier,
systimestamp
);
INSERT INTO audit_table
(
ACTION,
TABLE_ID,
OLD_VALUE,
NEW_VALUE,
USERNAME,
TS_CREATED
) VALUES (
var_action,
:OLD.ID,
:OLD.column2VALUE,
:new.COLUMN2VALUE,
ociidentifier,
systimestamp
);
... etc.
You may be able to simplify the code somewhat by writing a procedure to wrap the INSERT statement and calling that many times instead of the INSERT statement itself.

Related

Need to accept comma separated inputs from the stored procedure and have to process as I have explained in the below body but getting compilation err

CREATE TABLE STAGING
(
E_ID NUMBER(10),
E_NAME VARCHAR2(30),
E_LOC VARCHAR2(30),
VALIDATION_STATUS varchar2(30),
validation_msg varchar2(30),
req_id number(10)
);
INSERT INTO staging VALUES(1, 'A', 'AA', NULL, NULL, 1);
INSERT INTO staging VALUES(2, 'B', 'BB', NULL, NULL, 1);
INSERT INTO staging VALUES(3, 'C', 'CC', NULL, NULL, 1);
INSERT INTO staging VALUES(NULL, 'D', 'DD', NULL, NULL, 2);
INSERT INTO staging VALUES(NULL, 'E', 'EE', NULL, NULL, 2);
INSERT INTO staging VALUES(NULL, 'F', 'GG', NULL, NULL, 2);
CREATE TABLE tab_ref
(
ref_id number(10),
ref_name varchar2(30)
);
INSERT INTO tab_ref VALUES(1, 'aa');
INSERT INTO tab_ref VALUES(2, 'bb');
INSERT INTO tab_ref VALUES(3, 'cc');
INSERT INTO tab_ref VALUES(4, 'dd');
CREATE TABLE tab_ref_2
(
ref_id number(10),
ref_name varchar2(30)
);
INSERT INTO tab_ref_2 VALUES(1, 'ee');
INSERT INTO tab_ref_2 VALUES(2, 'ff');
INSERT INTO tab_ref_2 VALUES(3, 'gg');
INSERT INTO tab_ref_2 VALUES(4, 'hh');
CREATE TABLE SUMMARY_TAB
(
TOT_RECORDS NUMBER(10,0),
SUCCESS_RECORDS NUMBER(10,0),
FAILED_RECORDS NUMBER(10,0),
process_status varchar2(30)
);
CREATE TABLE TARGET_TAB
(
E_ID NUMBER(10,0),
E_NAME VARCHAR2(30),
E_LOC VARCHAR2(30)
);
Stored procedure:
create or replace procedure sp_stage_target(iv_req_id IN sys.OdciNumberList,ov_err_msg OUT varchar2) is
lv_succ_rec number(30);
lv_fail_rec number(30);
lv_count_ref number(10);
lv_count_ref2 number(10);
lv_threshold_cnt number(10);
lv_RejectedCount number(10);
lv_status varchar2(30);
begin
lv_succ_rec := 0;
lv_fail_rec := 0;
lv_threshold_cnt := 5;
/*First checking whether data is present in reference table or not.
If data is not present then process should stop*/
select count(1) into lv_count_ref from tab_ref;
select count(1) into lv_count_ref2 from tab_ref_2;
if lv_count_ref = 0 then
ov_err_msg := 'Records are not present in the reference table !!Cannot proceed';
elsif lv_count_ref2 = 0 then
ov_err_msg := 'Records are not present in the reference table !!Cannot proceed';
else
dbms_output.put_line('Data are present into reference tables');
merge into staging d
using (
select 'Fail' as validation_status, t.column_value as req_id
from table(iv_req_id) t
) s
on (d.req_id = s.req_id)
when matched then
update set
d.validation_status = s.validation_status
, d.validation_msg = case
when e_id is null then 'Id is not present'
else 'Id is longer than expected'
end
where e_id is null OR LENGTH(e_id) > 4;
lv_RejectedCount := SQL%ROWCOUNT;
end if;
--If rejected count is less than lv_threshold_cnt i.e 5
--then success records will go in target_tab and failed records will go in reject_tab
if lv_RejectedCount <= lv_threshold_cnt then
lv_status := 'Success';
dbms_output.put_line('Success');
merge into target_tab t
using (
select e_id, e_name, e_loc
from staging
where validation_status is null and req_id in (select column_value from table(iv_req_id))
) s
on (t.e_id = s.e_id)
when matched then
update set
t.e_name = s.e_name,
t.e_loc = s.e_loc
when not matched then
insert (t.e_id,t.e_name,t.e_loc)
values (s.e_id,s.e_name,s.e_loc);
lv_succ_rec := SQL%ROWCOUNT;
end if;
insert into reject_tab
select e_id, e_name, e_loc, validation_status,validation_msg
from staging
where validation_status = 'Fail' and req_id in (select column_value from table(iv_req_id));
lv_fail_rec := SQL%ROWCOUNT;
--In Summary table keeping track of all the records i.e success record, failed records
dbms_output.put_line('Inserting into Summary table');
insert into summary_tab(tot_records, success_records, failed_records, process_status)
values (lv_succ_rec + lv_fail_rec, lv_succ_rec, lv_fail_rec, lv_status);
ov_err_msg := 'Procedure completed succesfully';
end;
Calling Procedure:
set serveroutput on;
declare
err_msg;
begin
sp_main_target(sys.OdciNumberList(1,2),err_msg);
dbms_output.put_line(err_msg);
end;
Getting compilation error and also I am not not how to process for individually for each request_id and process so have highlighted the requirement in comment block.
Error :
I wanted to create a stored procedure that will handle all the below points and I have tried but not been able to get the results.
The stored procedure should accept multiple input parameters while calling a procedure and should process for every request-id given in the parameter with comma-separated.
Then stored procedure will check whether data is present or not in the ref table (tab_ref & tab_ref_2). If data is present then only the process should start otherwise it will not proceed further.
Then it will check the data in the staging table and will do validation for e_id is null or not or its length should not exceed the given limit. For every failed validation it will update the validation status and validation msg column and have to keep count of rejected columns.
After validation, if the threshold count is less then the lv_threshold_count then insertion will happen in both the tables with status as 'Success 'i.e target table and rejected table with validation_status as null and Fail respectively.
If threshold count is more than the lv_threshold_count then it will insert into the rejected table with status as 'Fail'.
Then at last it will show all the records count into the summary table.
You start an IF on line 20 of your procedure, but you don't have a corresponding END IF.
if lv_count_ref = 0 then

SQL Developer will not insert data

create or replace PROCEDURE ADD_TO_BLACKLIST(
P_EMPLOYEE_USERNAME IN VARCHAR2,
T_CURSOR OUT SYS_REFCURSOR
)
AS
BEGIN
DECLARE
E_COUNT PLS_INTEGER := 0;
BEGIN
SELECT COUNT(*) INTO E_COUNT FROM EXAMPLE_TABLE
WHERE UPPER(EMPLOYEE_USERNAME) LIKE UPPER(P_EMPLOYEE_USERNAME)||'%';
IF E_COUNT = 0 THEN
INSERT INTO EXAMPLE_TABLE
(employee_number, employee_username)
SELECT EMPLOYEE_NUMBER, EMAIL FROM EXAMPLE_VIEW
WHERE UPPER(EMAIL)=CONCAT(UPPER(P_EMPLOYEE_USERNAME), '#microsoft.com');
ELSE
UPDATE EXAMPLE_TABLE
SET (EMPLOYEE_NUMBER, EMPLOYEE_USERNAME) =
(SELECT EMPLOYEE_NUMBER, EMAIL FROM EXAMPLE_VIEW
WHERE UPPER(EMAIL) = CONCAT(UPPER(P_EMPLOYEE_USERNAME), '#microsoft.com'));
COMMIT;
END IF;
OPEN T_CURSOR For
SELECT * FROM EXAMPLE_VIEW
WHERE EMAIL LIKE CONCAT(UPPER(P_EMPLOYEE_USERNAME), '%');
END;
END ADD_TO_BLACKLIST;
This compiles, but when I try to test it with a valid P_EMPLOYEE_USERNAME (which I've confirmed to be in the EXAMPLE_VIEW), I do not see any data being inserted.
I am new to PLSQL and not sure how to figure out the value of E_COUNT
The Example_Table DDL is
CREATE TABLE "Example_Table"
( "EMPLOYEE_NUMBER" NUMBER NOT NULL ENABLE,
"EMPLOYEE_USERNAME" VARCHAR2(250 BYTE) NOT NULL ENABLE,
"ACCOUNT_STATUS" NUMBER DEFAULT 0,
"ACCOUNT_STATUS_LAST_UPDATE" TIMESTAMP (6) DEFAULT SYSDATE NOT NULL ENABLE,
CONSTRAINT "BOE_SAFEGAURD_PK" PRIMARY KEY ("EMPLOYEE_USERNAME"))
The issue is in below line,you are not converting the case after concatenation.please modify and try below,
WHERE UPPER(EMAIL) = UPPER(CONCAT(UPPER(P_EMPLOYEE_USERNAME), '#microsoft.com'));
EDIT : To prove the theory please find below the details.
I have tested this and it works,
DDL to create the tables:
CREATE TABLE Example_Table
( EMPLOYEE_NUMBER NUMBER NOT NULL ENABLE,
EMPLOYEE_USERNAME VARCHAR2(250 BYTE) NOT NULL ENABLE,
ACCOUNT_STATUS NUMBER DEFAULT 0,
ACCOUNT_STATUS_LAST_UPDATE TIMESTAMP (6) DEFAULT SYSDATE NOT NULL ENABLE,
CONSTRAINT BOE_SAFEGAURD_PK PRIMARY KEY (EMPLOYEE_USERNAME));
CREATE TABLE Example_view
( EMPLOYEE_NUMBER NUMBER NOT NULL ENABLE,
EMAIL VARCHAR2(250 BYTE) NOT NULL ENABLE,
ACCOUNT_STATUS NUMBER DEFAULT 0,
ACCOUNT_STATUS_LAST_UPDATE TIMESTAMP (6) DEFAULT SYSDATE NOT NULL ENABLE
);
DML to populate data to example_view that will be used for the test.
insert into example_view values(1,'Test#microsoft.com',1,sysdate);
Modified the procedure to add UPPER on the rightside of the join for both insert and update conditions and place the commit after end if.A good code should have only one commit and that should be at the end of execution before the exception block of main begin..end block.
create or replace PROCEDURE ADD_TO_BLACKLIST(
P_EMPLOYEE_USERNAME IN VARCHAR2,
T_CURSOR OUT SYS_REFCURSOR
)
AS
BEGIN
DECLARE E_COUNT PLS_INTEGER := 0;
BEGIN
SELECT COUNT(*) INTO E_COUNT FROM EXAMPLE_TABLE WHERE UPPER(EMPLOYEE_USERNAME) LIKE UPPER(P_EMPLOYEE_USERNAME)||'%';
IF E_COUNT = 0 THEN
INSERT INTO EXAMPLE_TABLE
(employee_number, employee_username)
SELECT EMPLOYEE_NUMBER, EMAIL FROM EXAMPLE_VIEW WHERE UPPER(EMAIL)=UPPER(CONCAT(UPPER(P_EMPLOYEE_USERNAME), '#microsoft.com'));
ELSE
UPDATE EXAMPLE_TABLE SET (EMPLOYEE_NUMBER, EMPLOYEE_USERNAME) = (SELECT EMPLOYEE_NUMBER, EMAIL FROM EXAMPLE_VIEW WHERE UPPER(EMAIL)=UPPER(CONCAT(UPPER(P_EMPLOYEE_USERNAME), '#microsoft.com')));
END IF;
COMMIT;
OPEN T_CURSOR For
SELECT * FROM EXAMPLE_VIEW WHERE EMAIL LIKE CONCAT(UPPER(P_EMPLOYEE_USERNAME), '%');
END;
END ADD_TO_BLACKLIST;
In an anonymous block invoked the procedure,
DECLARE
T_CURSOR SYS_REFCURSOR;
BEGIN
ADD_TO_BLACKLIST('test',T_CURSOR);
end;
Ran a query to check if records are inserted,
select * from example_table;
Output is below,
You just need a commit after IF-ELSE condition rather than inside it. I have updated your code along with some other minor updates -
create or replace PROCEDURE ADD_TO_BLACKLIST( P_EMPLOYEE_USERNAME IN VARCHAR2,
T_CURSOR OUT SYS_REFCURSOR
)
AS
E_COUNT PLS_INTEGER := 0;
BEGIN
SELECT COUNT(*)
INTO E_COUNT
FROM EXAMPLE_TABLE
WHERE UPPER(EMPLOYEE_USERNAME) LIKE UPPER(P_EMPLOYEE_USERNAME)||'%';
IF E_COUNT = 0 THEN
INSERT INTO EXAMPLE_TABLE
(employee_number, employee_username)
SELECT EMPLOYEE_NUMBER, EMAIL
FROM EXAMPLE_VIEW
WHERE UPPER(EMAIL) = CONCAT(UPPER(P_EMPLOYEE_USERNAME), '#microsoft.com');
ELSE
UPDATE EXAMPLE_TABLE
SET (EMPLOYEE_NUMBER, EMPLOYEE_USERNAME) = (SELECT EMPLOYEE_NUMBER, EMAIL
FROM EXAMPLE_VIEW
WHERE UPPER(EMAIL)=CONCAT(UPPER(P_EMPLOYEE_USERNAME), '#microsoft.com'));
END IF;
COMMIT;
OPEN T_CURSOR For
SELECT *
FROM EXAMPLE_VIEW
WHERE EMAIL LIKE CONCAT(UPPER(P_EMPLOYEE_USERNAME), '%');
END ADD_TO_BLACKLIST;

If condition is giving Error in Oracle Package

I'm new to oracle database i created a PL/SQL package with some if else statements. It contains three procedures respectively for insert, update and delete. That is giving me error. Can anyone help me sorting out this..
create or replace
PACKAGE body USERS_tapi
IS
-- insert
PROCEDURE ins(
p_FIRSTNAME IN USERS.FIRSTNAME%type ,
p_ADDRESS IN USERS.ADDRESS%type ,
p_ROLEID IN USERS.ROLEID%type DEFAULT NULL ,
p_USERNAME IN USERS.USERNAME%type ,
p_PASSWORD IN USERS.PASSWORD%type ,
p_USERID IN USERS.USERID%type ,
p_LASTNAME IN USERS.LASTNAME%type,
O_val OUT NUMBER
)
IS
BEGIN
select count(*) as cnt from USERS where username=p_USERNAME and password=p_PASSWORD;
if cnt = 0 then
INSERT INTO USERS(
FIRSTNAME ,
ADDRESS ,
ROLEID ,
USERNAME ,
PASSWORD ,
USERID ,
LASTNAME
)
VALUES
(
p_FIRSTNAME ,
p_ADDRESS ,
p_ROLEID ,
p_USERNAME ,
p_PASSWORD ,
USERS_SEQ.nextval ,
p_LASTNAME
);
O_val:=0;
else if cnt > 0 then
O_val:=1;
else
O_val:=2;
END IF;
return O_val;
END;
-- update
PROCEDURE upd
(
p_FIRSTNAME IN USERS.FIRSTNAME%type ,
p_ADDRESS IN USERS.ADDRESS%type ,
p_ROLEID IN USERS.ROLEID%type DEFAULT NULL ,
p_USERNAME IN USERS.USERNAME%type ,
p_PASSWORD IN USERS.PASSWORD%type ,
p_USERID IN USERS.USERID%type ,
p_LASTNAME IN USERS.LASTNAME%type
)
IS
BEGIN
UPDATE USERS
SET FIRSTNAME = p_FIRSTNAME ,
ADDRESS = p_ADDRESS ,
ROLEID = p_ROLEID ,
USERNAME = p_USERNAME ,
PASSWORD = p_PASSWORD ,
LASTNAME = p_LASTNAME
WHERE USERID = p_USERID;
END;
-- del
PROCEDURE del(
p_USERID IN USERS.USERID%type )
IS
BEGIN
DELETE FROM USERS WHERE USERID = p_USERID;
END;
END USERS_tapi;
Use elsif instead of else if in the following line :
else if cnt > 0 then
The former is just one more case in your condition block.
The latter syntax is starting a nested condition block in the else part of the first condition block. That nested block would need its own END IF to work.
You could also use a CASE statement :
CASE
WHEN cnt = 0 then
-- insert statement
O_val:=0;
WHEN cnt > 0 then
O_val:=1;
else
O_val:=2;
END CASE;

Why does this get-or-create function never seem to insert?

I'm writing a function for batch-importing data into our org-chart. It seems to work fine for retrieving entries that already exists, but when an entry does not already exist, and it's supposed to insert, commit, and return the result of a re-attempt (so as to get the auto-generated ID), it always returns NULL.
I'm sure I'm doing something wrong here, but what? Help appreciated.
Note: there's a before-insert trigger that fills in DEPT_ID if it's not specified. Works fine if the insert statement is executed by hand.
CREATE TABLE DEPTS
(
"DEPT_ID" VARCHAR2(10 BYTE),
"HEADER_ID" VARCHAR2(10 BYTE),
"COMMENTS" VARCHAR2(100 BYTE),
"CATEGORY" VARCHAR2(2 BYTE)
);
CREATE OR REPLACE FUNCTION get_or_make_unit(
, in_cat VARCHAR2
, in_cmt VARCHAR2
, in_hdr VARCHAR2 DEFAULT NULL
) RETURN VARCHAR2 AS
unit_id VARCHAR2(10);
BEGIN
unit_id := NULL;
IF in_hdr IS NULL THEN
SELECT dept_id
INTO unit_id
FROM depts unit
WHERE unit.category = in_cat
AND unit.comments = in_cmt
AND unit.header_id IS NULL;
ELSE
SELECT dept_id
INTO unit_id
FROM depts unit
WHERE unit.category = in_cat
AND unit.comments = in_cmt
AND unit.header_id = in_hdr;
END IF;
IF unit_id IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Inserting!');
INSERT INTO depts (
header_id
, comments
, category
) VALUES (
in_hdr
, in_cmt
, in_cat);
COMMIT;
unit_id := get_or_make_unit(in_cat, in_cmt, in_hdr);
RETURN unit_id;
ELSE
DBMS_OUTPUT.PUT_LINE('Not inserting!');
RETURN unit_id;
END IF;
END get_or_make_unit;
And the trigger:
CREATE OR REPLACE TRIGGER HRD.DEPTS_BIR
BEFORE INSERT
ON HRD.DEPTS
FOR EACH ROW
DECLARE
JML NUMBER;
BEGIN
SELECT SEQ_DEPT_ID.NEXTVAL INTO JML FROM DUAL;
:NEW.DEPT_ID:='D'||to_char(JML);
END DEPTS_BIR;
Examples
This works:
INSERT INTO depts (
header_id
, comments
, category
) VALUES (
'D532'
, 'ACCOUNTING'
, '2');
COMMIT;
SELECT get_or_make_unit('2', 'ACCOUNTING', 'D532') FROM DUAL;
=> 'D533'
This does not:
SELECT get_or_make_unit('2', 'NEW DEPT', 'D532') FROM DUAL;
=> NULL
Instead of:
INSERT INTO depts (
header_id
, comments
, category
) VALUES (
in_hdr
, in_cmt
, in_cat);
COMMIT;
unit_id := get_or_make_unit(in_cat, in_cmt, in_hdr);
use RETURNING INTO:
INSERT INTO depts (
header_id
, comments
, category
) VALUES (
in_hdr
, in_cmt
, in_cat) RETURNING dept_id INTO unit_id;
COMMIT;
I think recursive call in not the best approach, but if you are strict of using it, then please post definition of mentioned before-insert trigger.
UPDATE: You cannot call functions containing DML operations from SQL statement. Please see this answer for details. Example of correct call:
DECLARE
unit_id varchar2(32);
BEGIN
unit_id := get_or_make_unit('2', 'NEW DEPT', 'D532');
dbms_output.put_line(unit_id);
END;
UPDATE2: Also you need to catch NO_DATA_FOUND exception that is raised when you call your function with not existent combination. Below is example:
CREATE OR REPLACE FUNCTION get_or_make_unit(in_cat VARCHAR2,
in_cmt VARCHAR2,
in_hdr VARCHAR2 DEFAULT NULL)
RETURN VARCHAR2 AS
unit_id VARCHAR2(10);
BEGIN
unit_id := NULL;
IF in_hdr IS NULL THEN
SELECT dept_id
INTO unit_id
FROM depts unit
WHERE unit.category = in_cat
AND unit.comments = in_cmt
AND unit.header_id IS NULL;
ELSE
SELECT dept_id
INTO unit_id
FROM depts unit
WHERE unit.category = in_cat
AND unit.comments = in_cmt
AND unit.header_id = in_hdr;
END IF;
DBMS_OUTPUT.PUT_LINE('Not inserting!');
RETURN unit_id;
exception
when NO_DATA_FOUND then
DBMS_OUTPUT.PUT_LINE('Inserting!');
INSERT INTO depts
(header_id, comments, category)
VALUES
(in_hdr, in_cmt, in_cat)
returning dept_id into unit_id;
COMMIT;
RETURN unit_id;
END get_or_make_unit;
Your call to get_or_make_unit appears to be missing the first parameter.
INSERT INTO depts (
header_id
, comments
, category
) VALUES (
in_hdr
, in_cmt
, in_cat);
This code is not inserting anything into dept_id of depts table. Thats the reason you are getting null

Error at line 14: PL/SQL: SQL Statement ignored

I keep getting the error below and I can't figure out why. I've included the tables in the code and then also the code portion that is presenting the error.
Tables in the code:
CREATE TABLE Employee (
emp_id number(8) primary key,
f_name VarChar2(20),
l_name VarChar2(20),
address VarChar2(40),
city VarChar2(20),
state Char(2),
zip Number(5),
phone_Number Number(10),
email_address VarChar2(30),
dept_id number(8) references department(Dept_ID),
office_location VarChar2(30)
);
CREATE TABLE audit_detail (
audit_detail_id number(8) primary key,
field varchar2(30),
old_value varchar2(30),
new_value varchar2(30)
);
CREATE TABLE audit_trail (
audit_trail_id number(8) primary key,
user_id number(8)references user_id_table(user_id),
table_name VarChar2(25),
process VarChar2(25),
emp_id number(8)references employee(emp_id),
timestamp Date
);
Create table Department (
dept_id number(8) primary key,
dept_name VarChar2(20) not null,
dept_start_date date
);
create table user_id_table(
user_id number(8) primary key,
emp_id number(8) references Employee(emp_id)
);
CREATE SEQUENCE audit_seq;
This is the portion that is presenting the error, Specifically the elsif Deleting portion
CREATE OR REPLACE TRIGGER audit_employee
AFTER INSERT OR UPDATE OR DELETE ON Employee
FOR EACH ROW
DECLARE
timestamp DATE;
session_user number(8);
BEGIN
timestamp := SYSDATE;
session_user := USERENV('SESSION_USER');
IF INSERTING THEN
INSERT INTO audit_trail VALUES
(Audit_seq.NEXTVAL, session_user, 'Employee', 'INSERT', :new.emp_id, timestamp);
ElSIF DELETING THEN
INSERT INTO audit_trail Values
(Audit_seq.NEXTVAL, session_user, 'Employee', 'DELETE', :old.emp_id, timestamp);
Else
INSERT INTO audit_trail
Values(Audit_seq.NEXTVAL, session_user, 'Employee', 'UPDATE', :old.emp_id);
IF UPDATING ('f_name') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'f_name', :old.f_name, :new.f_name);
ELSIF UPDATING('l_name') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'l_name', :old.l_name, :new.l_name);
ELSIF UPDATING('address') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'address', :old.address, :new.address);
ELSIF UPDATING('city') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'city', :old.city, :new.city);
ELSIF UPDATING('state') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'state', :old.state, :new.state);
ELSIF UPDATING('zip') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'zip', :old.zip, :new.zip);
ELSIF UPDATING('phone_Number') THEN
INSERT INTO Audit_detail VALUES
(Audit_seq.CURRVAL, 'phone_Number', :old.phone_number, :new.phone_number);
ELSIF UPDATING('email_address') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'email_address', :old.email_address, :new.email_address);
ELSIF UPDATING('dept_id') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'dept_id', :old.dept_id, :new.dept_id);
ELSIF UPDATING('office_location') THEN
INSERT INTO audit_detail VALUES
(Audit_seq.CURRVAL, 'office_location', :old.office_location, :new.office_location);
End IF;
End IF;
End;
/
Your else statement after the deleting has 5 columns whereas audit_trail has 6.
ElSIF DELETING THEN
INSERT INTO audit_trail
Values ( Audit_seq.NEXTVAL, session_user, 'Employee'
, 'DELETE', :old.emp_id, timestamp);
Else
INSERT INTO audit_trail
Values (Audit_seq.NEXTVAL, session_user, 'Employee'
, 'UPDATE', :old.emp_id );
You're also referencing columns in lower case not upper, UPDATING ('L_NAME'). I'm not sure whether that would cause a problem.
You're doing a lot of implicit number to character conversion which will eventually catch you out and table_name in audit_trail should by 30 characters not 25. Why risk it?

Resources