Compile error on if else condition in trigger - oracle

create or replace
TRIGGER TRG_DEPT_ID BEFORE INSERT ON DEPT FOR EACH ROW
BEGIN
IF NOT EXISTS (SELECT * FROM DEPT cd WHERE cd.st_num = :new.ST_NUMBER AND cd.td_NUMBER = :new.TD_NUMBER)
THEN
SELECT SEQ_DEPT_ID.NEXTVAL INTO :new.ID FROM dual;
ELSE
SELECT ID FROM DEPT INTO :new.ID WHERE cd.st_num = :new.ST_NUMBER AND cd.td_NUMBER = :new.TD_NUMBER;
END IF;
END ;
I am trying to check for duplicate entry. If not exists, then I will create a new Id from sequence. Else I will put the same id.
And the SaveOrUpdate(dept); will do the remaining.
But it gave me compile error.Error(8,9): PL/SQL: SQL Statement ignored.
UPDATE:
When I tried the following query:
CREATE OR REPLACE TRIGGER TRG_DEPT_ID
BEFORE INSERT ON DEPT FOR EACH ROW
BEGIN
BEGIN
SELECT ID
INTO :NEW.ID
FROM DEPT
WHERE cd.st_num = :NEW.ST_NUMBER
AND cd.td_NUMBER = :NEW.TD_NUMBER;
EXCEPTION WHEN no_data_found THEN
SELECT SEQ_DEPT_ID.NEXTVAL INTO :new.ID FROM dual;
END;
END;
Getting the error:
ORA-01422: exact fetch returns more than requested number of rows
How to handle this error?

You can simply code your trigger like this,
CREATE OR REPLACE TRIGGER TRG_DEPT_ID
BEFORE INSERT ON DEPT FOR EACH ROW
BEGIN
BEGIN
SELECT ID
INTO :NEW.ID
FROM DEPT
WHERE cd.st_num = :NEW.ST_NUMBER
AND cd.td_NUMBER = :NEW.TD_NUMBER
AND rownum = 1; --Use rownum = 1 to avoid selecting too many rows.
EXCEPTION WHEN no_data_found THEN
SELECT SEQ_DEPT_ID.NEXTVAL INTO :new.ID FROM dual;
--:NEW.id = seq_dept_id.nextval; -- Or you can use this if you are using 11g or higher versions.
END;
END;
Edited:
You can modify your trigger code as below if duplicate entries are there in your table.
CREATE OR REPLACE TRIGGER TRG_DEPT_ID
BEFORE INSERT ON DEPT FOR EACH ROW
DECLARE
l_id DEPT.id%type;
BEGIN
BEGIN
SELECT ID
INTO l_id
FROM DEPT
WHERE cd.st_num = :NEW.ST_NUMBER
AND cd.td_NUMBER = :NEW.TD_NUMBER
AND ROWNUM = 1; --Use rownum = 1 to avoid selecting too many rows.
IF l_id IS NOT NULL THEN --If same st_number and td_number exists.
raise_application_error( -20001, 'Duplicate entry.');
END IF;
EXCEPTION WHEN no_data_found THEN
SELECT SEQ_DEPT_ID.NEXTVAL INTO :new.ID FROM dual;
--:NEW.id = seq_dept_id.nextval; -- Or you can use this if you are using 11g or higher versions.
END;
END;

try adding rownum in the where section :
CREATE OR REPLACE TRIGGER TRG_DEPT_ID
BEFORE INSERT ON DEPT FOR EACH ROW
BEGIN
BEGIN
SELECT ID
INTO :NEW.ID
FROM DEPT
WHERE cd.st_num = :NEW.ST_NUMBER
AND cd.td_NUMBER = :NEW.TD_NUMBER
AND ROWNUM =1;
EXCEPTION WHEN no_data_found THEN
SELECT SEQ_DEPT_ID.NEXTVAL INTO :new.ID FROM dual;
END;
END;
That will make the query to just return 1 row in case cd.st_num and cd.td_NUMBER are repeated in more than one row.
On the other hand, if you need to detect that there are more than one row for these two columns , you can catch the exception TOO_MANY_ROWS in the exceptions section
EXCEPTION WHEN no_data_found THEN
SELECT SEQ_DEPT_ID.NEXTVAL INTO :new.ID FROM dual;
when TOO_MANY_ROWS then
-- your logic to do when there are too many rows
END;

Related

how can i put a query in decode function?

create table ss( no number, filepath varchar2(300) )
I want to have 5 or less duplicate values of 'no' in this table
when select count(no) from ss where no=#{no} <5, insert into ss values({no},{filepath})
so duplicate values of 'no' can't be over 5.
how can i do this?
You could create a similar trigger to implement this logic:
CREATE OR REPLACE TRIGGER set_no_ss_tbl_trg
BEFORE INSERT ON ss_tbl
FOR EACH ROW
BEGIN
DECLARE
l_cnt_no NUMBER;
BEGIN
SELECT COUNT(1)
INTO l_exceeding
FROM g_piece
WHERE refdoss = :new.no;
IF l_cnt_no > 5 THEN
SELECT MIN(no)
INTO :new.no
FROM (SELECT COUNT(1), no
FROM ss_tbl
GROUP BY no
HAVING COUNT(1) + 1 <= 5);
END IF;
END;
END;

Oracle PL/SQL No data found - non consecutive Id values

I have a report with the following structure:
Column 1 ID, Column 2 Approved_Rejected_Status, Column 3 Rep_Id
I'm getting a no data found error while running the following:
DECLARE
V_REP_ID VARCHAR2(100);
V_ROWS_APPROVED_min NUMBER;
V_ROWS_APPROVED_max NUMBER;
V_STAT VARCHAR2(100);
BEGIN
SELECT min(id) INTO V_ROWS_APPROVED_min FROM MY_TABLE;
SELECT max(id) INTO V_ROWS_APPROVED_max FROM MY_TABLE;
FOR i IN V_ROWS_APPROVED_min..V_ROWS_APPROVED_max LOOP
SELECT APPROVED_REJECTED_STATUS INTO V_STAT
FROM MY_TABLE WHERE MY_TABLE.ID =i;
SELECT REP_ID INTO V_REP_ID
FROM MY_TABLE
WHERE MY_TABLE.ID = i;
END LOOP;
END;
I think it has something to do with the ID having non consecutive values perhaps?
(I can't have consecutive values on the report for functionality, neither include an exception cause I need to to perform actions depending on the type_of_change)
Thank you
Edit:
Thank you MT0 for your review/suggestion!
I tried using a cursor for loop and something really weird is happening:
for cur_m in (select id, approved_rejected_status
from bdc_bench_watchlist_t
order by id)
loop
if cur_m.approved_rejected_status = 'Approved' then
--Inserting into a test table:
insert into index_test (ID_COPY, MY_APPROVED_STATUS)
values (cur_m.id, cur_m.approved_rejected_status);
end if;
end loop;
The result brings all the range of IDs (from the min to the max) ok but! the approved_rejected_status column is empty (only the header is showing with null data on the rows). I have no idea why.
If I do a basic select id, approved_rejected_status from the source table it brings everything ok (id and status info ok).
Thank you
Assuming you need to iterate through every number and do something even if there is no row there then just handle the NO_DATA_FOUND exception:
DECLARE
V_REP_ID MY_TABLE.REP_ID%TYPE;
V_ROWS_APPROVED_min MY_TABLE.ID%TYPE;
V_ROWS_APPROVED_max MY_TABLE.ID%TYPE;
V_STAT MY_TABLE.APPROVED_REJECTED_STATUS%TYPE;
BEGIN
SELECT min(id), max(id)
INTO V_ROWS_APPROVED_min, V_ROWS_APPROVED_max
FROM MY_TABLE;
FOR i IN V_ROWS_APPROVED_min..V_ROWS_APPROVED_max LOOP
BEGIN
SELECT APPROVED_REJECTED_STATUS, REP_ID
INTO V_STAT, V_REP_ID
FROM MY_TABLE
WHERE MY_TABLE.ID =i;
EXCEPTION
WHEN NO_DATA_FOUND THEN
V_STAT := NULL; -- Or put default values here.
V_REP_ID := NULL;
END;
-- Do stuff with the stat/rep_id
END LOOP;
END;
If you don't want to iterate through every row and want to skip the missing id values then you could use a cursor and order by id.

If exist then update in oracle forms 11g

I am trying to write a code block where record insert if record already exist then update
table. i am trying If (sql%rowcount = 0) then but it's not working in cursor and more one records.
What I tried so far as the code block is
declare
remp_id varchar2(60);
remp_name varchar2(100);
rdesig varchar2(100);
rdept_no number;
rdesig_no number;
rdept_name varchar2(60);
cursor alfa is
select emp_code, emp_name, desig, dept_name, dept_no, desig_no
from emp
where emp_code between :first_code and :second_code;
begin
open alfa;
loop
fetch alfa
into remp_id, remp_name, rdesig, rdept_name, rdept_no, rdesig_no;
exit when alfa%notfound;
update att_reg_mo
set emp_code = remp_id,
emp_name = remp_name,
desig = rdesig,
dept_name = rdept_name,
dept_no = rdept_no,
desig_no = rdesig_no,
att_date = :att_date,
emp_att = :emp_att,
att_type = 'MA',
reg_date = :reg_date
where emp_code between :first_code and :second_code
and reg_date = :reg_date
and att_date = :att_date;
commit;
if (sql%rowcount = 0) then
insert into att_reg_mo
(emp_code,
emp_name,
desig,
dept_name,
att_date,
emp_att,
att_type,
reg_date,
dept_no,
desig_no)
values
(remp_id,
remp_name,
rdesig,
rdept_name,
:att_date,
:emp_att,
'MA',
:reg_date,
rdept_no,
rdesig_no);
end if;
commit;
end loop;
close alfa;
end;
when i am fire the trigger then record is insert but where need to update record it's update with null values
Or you could use something like that:
DECLARE
cursor test is
select 1 as v from dual
union
select 2 as v from dual;
n_var NUMBER;
BEGIN
for rec in test loop
BEGIN
select 1 into n_var from dual where rec.v=2;
DBMS_OUTPUT.PUT_LINE('Please Update Any Table');
EXCEPTION
WHEN no_data_found THEN
DBMS_OUTPUT.PUT_LINE('Please Insert Any Table');
END;
end loop;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ('Unexpected error');
END;
SQL%attribute always refers to the most recently run SELECT or DML statement. It refreshes as to start from zero after any of transaction statement such as COMMIT, ROLLBACK or SAVEPOINT is issued, for a session in which AUTOCOMMIT is presumed to be OFF by default. Therefore, you always get zero from SQL%ROWCOUNT which is just before the INSERT statement, and keeping inserting to the concerned table during every run of code block.
So, remove the f i r s t COMMIT, removing also keeps the atomicity of the whole transaction, from your code block.
Demo

Passing data when query returns value and No "EXCEPTION WHEN NO_DATA_FOUND THEN" (Oracle 11g)

I have created a procedure for updating my t_ritm table. First I have select rrcd_qnty (which is my product quantity) of a product id from t_rrcd table. Then I update the rrcd_qnty value in t_ritm table.
Here is my procedure:
procedure update_ritm_new_rate(p_oid in varchar2, p_ritm_rate in varchar2, p_euser in varchar2)
is
nrate varchar2(4);
begin
SELECT rrcd_rate into nrate
FROM (select oid, t_rrcd.rrcd_rate
from t_rrcd
where rrcd_ritm= p_oid
ORDER BY oid DESC )
WHERE rownum <= 1
ORDER BY rownum DESC ;
EXCEPTION
WHEN NO_DATA_FOUND THEN nrate := 0;
update t_ritm
set ritm_rate = nrate, euser = p_euser, edat = sysdate
where oid = p_oid;
commit;
end update_ritm_new_rate;
Some of my product id Quantity was null. so I was getting No_Data_Found error. But when and which product id has Quantity value they were successfully updating. For avoiding No_Data_Found I used EXCEPTION WHEN NO_DATA_FOUND THEN nrate := 0; which solved my no_Data_Found error. But when product id has quantity value they were not updating.
I had search lot of for this issue but not get good solution. What should be the best practice for avoiding No_Data_Found error? Could I pass my value if I don't get any No_Data_Found error?
thank in advance
That's because - if your SELECT returns something, it never reaches UPDATE as it is hidden behind the EXCEPTION handler.
Therefore, enclose it (SELECT) into its own BEGIN-END block, and put UPDATE out of it so that it is executed with whichever NRATE value is used.
PROCEDURE update_ritm_new_rate (p_oid IN VARCHAR2,
p_ritm_rate IN VARCHAR2,
p_euser IN VARCHAR2)
IS
nrate VARCHAR2 (4);
BEGIN
BEGIN --> this
SELECT rrcd_rate
INTO nrate
FROM ( SELECT oid, t_rrcd.rrcd_rate
FROM t_rrcd
WHERE rrcd_ritm = p_oid
ORDER BY oid DESC)
WHERE ROWNUM <= 1
ORDER BY ROWNUM DESC;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
nrate := 0;
END; --> this
UPDATE t_ritm
SET ritm_rate = nrate, euser = p_euser, edat = SYSDATE
WHERE oid = p_oid;
COMMIT;
END update_ritm_new_rate;
I have fixed the issue by adding EXCEPTION WHEN NO_DATA_FOUND THEN nrate := 0; after the update query.
procedure update_ritm_new_rate(p_oid in varchar2, p_ritm_rate in varchar2, p_euser in varchar2)
is
nrate varchar2(4);
begin
SELECT rrcd_rate into nrate FROM (select oid, t_rrcd.rrcd_rate from t_rrcd where rrcd_ritm= p_oid ORDER BY oid DESC )
WHERE rownum <= 1 ORDER BY rownum DESC ;
update t_ritm set ritm_rate = nrate, euser = p_euser, edat = sysdate where oid = p_oid;
commit;
EXCEPTION WHEN NO_DATA_FOUND THEN nrate := 0;
end update_ritm_new_rate;

How to get the number of rows in ROWTYPE variable

I have two tables Table1 an dTable2 that have identical columns. I need to check if a particular id is in one of them and return the row of data from whichever table.
I have the following PL/SQL code:
v_result Table1%ROWTYPE;
BEGIN
SELECT a.*
INTO v_result
FROM Table1 a
WHERE a.id = 123;
EXCEPTION
WHEN NO_DATA_FOUND THEN -- when record not found
SELECT b.*
INTO v_result
FROM Table2 b
WHERE b.id = 123;
END;
The issue is that the exception does not get thrown, so v_result returns no data. How can I check v_result for the number of rows?
For cursor I can use ROWCOUNT but v_result is not a cursor.
I also tried using count property but it errored out.
I changed my code to:
v_result Table1%ROWTYPE;
BEGIN
SELECT a.*
INTO v_result
FROM Table1 a
WHERE a.id = 123;
if v_result.count =0 then
SELECT b.*
INTO v_result
FROM Table2 b
WHERE b.id = 123;
end if;
EXCEPTION
WHEN NO_DATA_FOUND THEN -- when record not found
SELECT b.*
INTO v_result
FROM Table2 b
WHERE b.id = 123;
END;
And got an error component 'count' must be declared
What am I doing wrong?
You may use only a single row in a record variable. If you want to store and count multiple rows, you may define a collection of records and use BULK COLLECT INTO to load all of them at once and it won't raise a NO_DATA_FOUND. The count function works on collections.
DECLARE
TYPE type_tab1 IS TABLE OF Table1%ROWTYPE;
TYPE type_tab2 IS TABLE OF Table2%ROWTYPE;
v_result1 type_tab1;
v_result2 type_tab2;
BEGIN
SELECT a.*
BULK COLLECT INTO v_result1
FROM Table1 a
WHERE a.id = 123;
if v_result1.count = 0 then
SELECT b.* BULK COLLECT
INTO v_result2
FROM Table2 b
WHERE b.id = 123;
end if;
DBMS_OUTPUT.PUT_LINE('v_result1 ='|| v_result1.count);
DBMS_OUTPUT.PUT_LINE('v_result2 ='|| v_result2.count);
END;
/
Output for the Demo
v_result1 =0
v_result2 =1
If your intention is to simply check if a row exists, then a simpler and efficient approach would be to use EXISTS
SELECT
CASE WHEN
EXISTS (
SELECT 1
FROM table1
WHERE id = 123
) THEN 1
ELSE 0
END
INTO v_count
FROM dual;
IF v_count = 0
THEN
..
..

Resources