If condition is giving Error in Oracle Package - oracle

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;

Related

Updating column is not working in Oracle PLSQL

I have a Stored procedure in PLSQL which Inserts and Updates records on the basis of some condition.
Now here the issue is. While Inserting the record for the first time, it inserts records properly as required but
while updating it doesn't updates the record of the table.
Below is SP
PROCEDURE INSERT_INTO_VSAT_MST_DATA
(
P_SAPID IN NVARCHAR2,
P_CIRCLE IN NVARCHAR2,
P_CANDIDATEID IN NVARCHAR2,
P_SITEID IN NVARCHAR2,
P_PRIORITYID IN NVARCHAR2,
P_SITENAME IN NVARCHAR2,
P_LATITUDE IN NVARCHAR2,
P_LONGITUDE IN NVARCHAR2,
P_CONTACT_DETAILS IN CLOB,
P_SITETYPE IN NVARCHAR2,
P_SITE_PLOT_DIMENSION IN NUMBER,
P_TECHNOLOGY IN NVARCHAR2
)
AS
V_COUNT NUMBER:=0;
V_PANAROMICIMG_COUNT NUMBER:=0;
V_SATELLITEIMG_COUNT NUMBER:=0;
V_SITEPLOTIMG_COUNT NUMBER:=0;
V_VSAT_DETAIL_ID NUMBER:=0;
BEGIN
SELECT COUNT(VSAT_DETAIL_ID) INTO V_COUNT FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
IF V_COUNT > 0 THEN
SELECT VSAT_DETAIL_ID INTO TBL_INSERT FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
UPDATE TBL_VSAT_MST_DETAIL SET
CIRCLE = P_CIRCLE,
CONTACT_DETAILS = P_CONTACT_DETAILS,
SITE_TYPE = P_SITETYPE,
SITE_DETAILS_DIMENSION = P_SITE_PLOT_DIMENSION,
SITE_DETAILS_TECHNOLOGY = P_TECHNOLOGY
WHERE VSAT_DETAIL_ID = V_VSAT_DETAIL_ID
RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
ELSE
INSERT INTO TBL_VSAT_MST_DETAIL
(
SAP_ID,
CIRCLE,
CANDIDATE_ID,
SITE_ID,
PRIORITY,
SITE_NAME,
LATITUDE,
LONGITUDE,
CONTACT_DETAILS,
SITE_TYPE,
SITE_DETAILS_DIMENSION,
SITE_DETAILS_TECHNOLOGY
VALUES
(
P_SAPID,
P_CIRCLE,
P_CANDIDATEID,
P_SITEID,
P_PRIORITYID,
P_SITENAME,
P_LATITUDE,
P_LONGITUDE,
P_CONTACT_DETAILS,
P_SITETYPE,
P_SITE_PLOT_DIMENSION,
P_TECHNOLOGY
) RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
END IF;
IF TBL_INSERT > 0 THEN
BEGIN
SELECT COUNT(*) INTO V_PANAROMICIMG_COUNT FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Panaromic' AND IMG_ID = TBL_INSERT;
SELECT COUNT(*) INTO V_SATELLITEIMG_COUNT FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Satellite' AND IMG_ID = TBL_INSERT;
SELECT COUNT(*) INTO V_SITEPLOTIMG_COUNT FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'SitePlot' AND IMG_ID = TBL_INSERT;
IF V_PANAROMICIMG_COUNT > 0 THEN
BEGIN
DELETE FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Panaromic' AND IMG_ID = TBL_INSERT;
END;
END IF;
IF V_SATELLITEIMG_COUNT > 0 THEN
BEGIN
DELETE FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'Satellite' AND IMG_ID = TBL_INSERT;
END;
END IF;
IF V_SITEPLOTIMG_COUNT > 0 THEN
BEGIN
DELETE FROM TBL_VSAT_IMAGE_DETAIL WHERE IMG_TYPE = 'SitePlot' AND IMG_ID = TBL_INSERT;
END;
END IF;
FOR PMULTIFIELDS IN (SELECT REGEXP_SUBSTR(P_PANORAMIC_IMAGES,'[^,]+', 1, LEVEL) AS IMAGES FROM DUAL
CONNECT BY REGEXP_SUBSTR(P_PANORAMIC_IMAGES, '[^,]+', 1, LEVEL) IS NOT NULL
)
LOOP
INSERT INTO TBL_VSAT_IMAGE_DETAIL
(
IMG_ID,
IMG_NAME,
IMG_TYPE,
IMG_UPLOADED_DATE,
UPLOADED_BY
)
VALUES
(
TBL_INSERT,
PMULTIFIELDS.IMAGES,
'Panaromic',
SYSDATE,
P_CREATEDBY
);
END LOOP;
FOR PSATELLITEIMG IN (SELECT REGEXP_SUBSTR(P_SATELLITE_IMAGES,'[^,]+', 1, LEVEL) AS IMAGES FROM DUAL
CONNECT BY REGEXP_SUBSTR(P_SATELLITE_IMAGES, '[^,]+', 1, LEVEL) IS NOT NULL
)
LOOP
INSERT INTO TBL_VSAT_IMAGE_DETAIL
(
IMG_ID,
IMG_NAME,
IMG_TYPE,
IMG_UPLOADED_DATE,
UPLOADED_BY
)
VALUES
(
TBL_INSERT,
PSATELLITEIMG.IMAGES,
'Satellite',
SYSDATE,
P_CREATEDBY
);
END LOOP;
IF P_SITEPLOT_IMAGES IS NOT NULL THEN
BEGIN
INSERT INTO TBL_VSAT_IMAGE_DETAIL
(
IMG_ID,
IMG_NAME,
IMG_TYPE,
IMG_UPLOADED_DATE,
UPLOADED_BY
)
VALUES
(
TBL_INSERT,
P_SITEPLOT_IMAGES,
'SitePlot',
SYSDATE,
P_CREATEDBY
);
END;
END IF;
END;
END IF;
COMMIT;
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
NOTE
While updating the record my TBL_INSERT returns as NULL
Expanding on what #user7294900 pointed you towards... in the declare section you have:
V_VSAT_DETAIL_ID NUMBER:=0;
then if v_count > 0 you do:
SELECT VSAT_DETAIL_ID INTO TBL_INSERT FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
UPDATE TBL_VSAT_MST_DETAIL SET
CIRCLE = P_CIRCLE,
CONTACT_DETAILS = P_CONTACT_DETAILS,
SITE_TYPE = P_SITETYPE,
SITE_DETAILS_DIMENSION = P_SITE_PLOT_DIMENSION,
SITE_DETAILS_TECHNOLOGY = P_TECHNOLOGY
WHERE VSAT_DETAIL_ID = V_VSAT_DETAIL_ID
RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
The select is setting TBL_INSERT to the ID value from your table. But when you do the update your filter is using V_VSAT_DETAIL_ID, which is still set to its initial value of zero.
You probably meant to do:
SELECT VSAT_DETAIL_ID INTO V_VSAT_DETAIL_ID FROM TBL_VSAT_MST_DETAIL WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
although you could still with your current select, and use that in the update too (making the returning into a bit redundant.
Be aware though that if v_count is not exactly 1, i.e. you have more than one row matching the P_SAPID and P_CANDIDATEID values, the select will get a too-many-rows exception. You won't see that because you are silently squashing any errors you get at run time.
It's usually not a good idea to commit or rollback inside a procedure anyway; it should be up to the caller to decide what to do, as this could be one of a series of statements and calls that you really want to treat as an atomic transaction. (You may be interested in savepoints.)
If you really, really want to rollback on exception within the procedure, you should at least re-raise the exception so the caller knows there was a problem:
EXCEPTION WHEN OTHERS THEN
ROLLBACK;
RAISE;
END INSERT_INTO_VSAT_MST_DATA;
but I would avoid when others if you can.
You could also combine a few steps by getting the ID at the start (again kind of assuming there is at most one matching row), and dropping the separate count and v_count variable:
SELECT MAX(VSAT_DETAIL_ID) INTO V_VSAT_DETAIL_ID
FROM TBL_VSAT_MST_DETAIL
WHERE SAP_ID = P_SAPID AND CANDIDATE_ID = P_CANDIDATEID;
IF V_VSAT_DETAIL_ID IS NOT NULL THEN
UPDATE TBL_VSAT_MST_DETAIL SET
CIRCLE = P_CIRCLE,
CONTACT_DETAILS = P_CONTACT_DETAILS,
SITE_TYPE = P_SITETYPE,
SITE_DETAILS_DIMENSION = P_SITE_PLOT_DIMENSION,
SITE_DETAILS_TECHNOLOGY = P_TECHNOLOGY
WHERE VSAT_DETAIL_ID = V_VSAT_DETAIL_ID
RETURNING VSAT_DETAIL_ID INTO TBL_INSERT;
ELSE
...
And I'm not sure why you're doing counts before your deletes later on, and it looks like all your tbl_insert references could/should be v_vast_detail_id - there doesn't seem an obvious reason to have two variables for that. Passing in a comma-delimited string that you then have to tokenize is also a bit painful - you should consider passing in a collection of values instead, if whatever calls this can manage that.
As also pointed out, you could use merge instead of the separate insert/update logic.
You don't assign value to V_VSAT_DETAIL_ID which is used in your update as a key.
You should use merge for this kind of operations

create audit trail using oracle trigger

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.

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

Writing a PLSQL Oracle function to query a table

I have the following table
CREATE TABLE Book
(
book_id INTEGER NOT NULL ,
isbn VARCHAR2 (20) NOT NULL,
tittle VARCHAR2 (100) NOT NULL ,
shelf_letter CHAR (1) NOT NULL ,
call_number INTEGER ,
no_of_copies INTEGER NOT NULL ,
) ;
I need to write a function to retrieve book_id, title,call_number, shelf_letter, no_of_copies for a given isbn.
Input parameters: isbn
Output parameters: title, no_of_copies,call_number,shelf_letter.
Return book_id if the query is successful and -1 if not.
How can I properly write this function?
create OR replace FUNCTION get_book_id
(
p_isbn IN VARCHAR2
, po_title OUT VARCHAR2
, po_no_of_copies OUT NUMBER
, po_call_number OUT NUMBER
, po_shelf_letter OUT NUMBER
)
RETURN NUMBER
IS
v_book_id NUMBER;
BEGIN
BEGIN
SELECT
book_id
, title
, no_of_copies
, call_number
, shelf_letter
INTO
v_book_id
, po_title
, po_no_of_copies
, po_call_number
, po_shelf_letter
FROM book
WHERE isbn = 'p_isbn'
;
EXCEPTION
WHEN NO_DATA_FOUND THEN
v_book_id := -1;
END;
RETURN v_book_id;
END;
/
DECLARE
TYPE book_info_rec IS RECORD
(
book_id NUMBER(1)
, title VARCHAR2(30)
, call_number NUMBER(1)
, shelf_letter VARCHAR2(30)
, no_of_copies NUMBER(1)
);
l_book book_info_rec;
FUNCTION get_book_info(isbn_in IN VARCHAR2) RETURN book_info_rec
AS
l_book_info book_info_rec;
BEGIN
SELECT 1
, 'A Book'
, 2
, 'A'
, 3
INTO l_book_info
FROM DUAL
WHERE dummy = isbn_in;
RETURN l_book_info;
END;
BEGIN
l_book := get_book_info('X');
DBMS_OUTPUT.PUT_LINE
(
l_book.book_id
|| ' ' || l_book.title
|| ' ' || l_book.call_number
|| ' ' || l_book.shelf_letter
|| ' ' || l_book.no_of_copies
);
END;

How to declare a Cursors With different conditions

I have a procedure EMPHIRESEPCHAN which is used to fetch the employees list who are hired, seperated and changed their titles based on a particular time frame. The procedure is as follows:
PROCEDURE EMPHIRESEPCHAN ( p_Start in VarChar2, p_End in VarChar2,
p_Hire IN VarChar2, p_Sep IN VarChar2, p_Changed IN VarChar2, p_Condition1 IN VarChar2, p_Condition2 IN VarChar2)
IS
CURSOR c_emplst ( p_listtype varchar2 ) IS
select e.emp_id, e.name, e.Rank
from person.emp e
where emp_id in (select distinct(emp_id) from person.promo
where pdate between p_startDate and p_endDate
and dcode in
(select adj from support.descr where typ = 'PROMO' and smeaning = p_listtype) );
CURSOR c_promolst ( p_emp_id varchar2 ) IS
select pdate
from person.promo
where emp_id = p_emp_id
order by 2 desc;
Begin
for EmpRec in c_emplst ('HIRE')
LOOP
for PromoRec in c_PromoLst ( EmpRec.emp )
LOOP
if PromoRec.Dcode in ('TEMPORARY','RETURN','APPOINTED' )
-- Do all the operation
end if;
end loop;
end loop;
end EMPHIRESEPCHAN;
I have to modify the procedure to retrieve the employee list based on p_Condition1 and p_Condition2 parameters.
If the p_Condition1 is not null and p_Condition2 is null, I have to retrieve the employees who have Rank = 'Developer'
If the p_Condition1 is null and p_Condition2 is not null I have to retrieve the employees who have Rank = 'Tester'
If the p_Condition1 and p_Condition2 is not null I have to retrieve the employees who have Rank both 'Developer' and 'Tester'.
I read so many posts in various sites and found answers which I was not able to follow.
Based on the posts, I made modifications to the cursor as follows
CURSOR c_emplst ( p_listtype varchar2 ) IS
select e.emp_id, e.name, e.Rank
from person.emp e
where ( p_Condition1 = null and p_Condition2 = null = and emp_id in (select distinct(emp_id) from person.promo
where pdate between p_startDate and p_endDate
and dcode in (select adj from support.descr where typ = 'PROMO' and smeaning = p_listtype) )
or ( p_Condition1 > null and p_Condition2 = null = and emp_id in (select distinct(emp_id) from person.promo
where pdate between p_startDate and p_endDate
and Rank ='Developer'
and dcode in (select adj from support.descr where typ = 'PROMO' and smeaning = p_listtype) )
or ( p_Condition1 = null and p_Condition2 > null = and emp_id in (select distinct(emp_id) from person.promo
where pdate between p_startDate and p_endDate
and Rank = 'Tester'
and dcode in (select adj from support.descr where typ = 'PROMO' and smeaning = p_listtype) );
However it's not working.
Thanks for your time and consideration.
I suspect these conditions are your problem:
p_Condition1 = null
Nothing is ever equal to NULL. NULL is not even equal to NULL. Instead, use:
p_Condition1 IS NULL

Resources