Exception is not getting called in plsql - exception

I am a beginner in plsql. The code below runs without any compilation error but the notFoundException is not getting called. Any help would be appriciated.
declare
abc exception;
notFoundException exception;
cursor c1(dd number) is select first_name from employees where salary = dd;
begin
for i in c1(&t)
loop
if(c1%rowcount!=1) then
raise abc;
elsif(c1%notfound) then
raise notFoundException;
else
dbms_output.put_line(i.first_name);
end if;
end loop;
Exception
when abc then
dbms_output.put_line('abc');
insert into messages values('too many rows exception');
when notFoundException then
dbms_output.put_line('notFoundException');
insert into messages values('Nobody with this salary : ');
end;
/

You may use the below anonymous block and try
`SET SERVEROUTPUT ON;
DECLARE
first_name_in employees.first_name%ROWTYPE;
Salary_in employees.salary%ROWTYPE;
BEGIN
Salary_in:=&Salary;
SELECT first_name
INTO first_name_in
FROM employees
WHERE salary = Salary_in;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Nobody with this salary :'||Salary_in);
INSERT
INTO messages
VALUES ('NO_DATA_FOUND exception');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('Too many with this salary :'||Salary_in);
INSERT
INTO messages
VALUES ('TOO_MANY_ROWS exception');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('select failed with error'||SUBSTR(SQLERRM,1,100))
INSERT
INTO messages
VALUES (SUBSTR(SQLERRM,1,100));
COMMIT;
END;
/`

You don't need to use cursor attributes when using FOR LOOP. You could just query the records and make decision afterwards. Here is a running example (I didn't have the employees table, so made up a stub). The convenient thing about FOR LOOP is that you don't have to worry about closing the cursor.
DECLARE
abc EXCEPTION;
notfoundexception EXCEPTION;
CURSOR c1(dd NUMBER) IS
SELECT *
FROM (SELECT MOD(rownum, 3) salary
,rownum first_name
FROM dual
CONNECT BY LEVEL < 10)
WHERE salary = dd;
v_count NUMBER := 0;
BEGIN
FOR i IN c1(2) LOOP
v_count := v_count + 1;
IF (v_count > 1) THEN
RAISE abc;
END IF;
dbms_output.put_line(i.first_name);
END LOOP;
IF (v_count = 0) THEN
RAISE notfoundexception;
END IF;
EXCEPTION
WHEN abc THEN
dbms_output.put_line('abc');
--insert into messages values('too many rows exception');
WHEN notfoundexception THEN
dbms_output.put_line('notFoundException');
--insert into messages values('Nobody with this salary : ');
END;
/

I suggest you to read about cursors in PL/SQL.
A cursor for loop will loop through every record that is retrieved in the cursor. If no data is found, execution will never proceed inside the loop, hence no data found exception will not be raised inside the loop. Your code should be like this
declare
abc exception;
notFoundException exception;
lv_first_name varchar2(240);
cursor c1(dd number) is select first_name from employees where salary = dd;
begin
open c1;
fetch c1 into lv_first_name;
if(c1%rowcount > 1) then
raise abc;
elsif(c1%notfound) then
raise notFoundException;
else
dbms_output.put_line(lv_first_name);
end if;
close c1;
Exception
when abc then
dbms_output.put_line('abc');
insert into messages values('too many rows exception');
when notFoundException then
dbms_output.put_line('notFoundException');
insert into messages values('Nobody with this salary : ');
end;

Related

Transaction error in stored procedure that is not using BEGIN or END TRANSACTION

I have a stored procedure "let's call it MY_NEW_SP" in which I'm not using BEGIN TRY / BEGIN CATCH. but, when I'm excecuting this SP (MY_NEW_SP), I get the following error:
Msg 266, Level 16, State 2, Procedure <MY_NEW_SP>, Line 132
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.
This new stored procedure makes a big select basically, no transactions are made "in the sense of make DML operations on tables (INSERT, DELETE, UPDATE)", but in temp tables "i.e. #tmp".
I'm thinking this transaction error is due I'm using SET XACT_ABORT ON; in other stored procedures, but, I'm not sure.
I follow what it is said here: C. Using TRY...CATCH with XACT_STATE
The basic structure of the stored procedure that uses SET XACT_ABORT ON; is as follows:
IF NOT EXISTS (SELECT * FROM sysobjects WHERE TYPE = 'P' AND NAME = 'PROCEP_NEW_SP' )
BEGIN
EXEC dbo.sp_executesql #statement = N'CREATE PROCEDURE PROCEP_NEW_SP AS'
END
GO
ALTER PROCEDURE PROCEP_NEW_SP
(
#ID_TABLE INT
)
AS
BEGIN
DECLARE #TBL_CONSECUTIVE TABLE ( LOG_CONSECUTIVE INT );
SET XACT_ABORT ON;
BEGIN TRANSACTION
BEGIN TRY
IF ISNULL(#ID_TABLE, -1) = -1
BEGIN
SET #ID_TABLE = 1;
DELETE FROM #TBL_CONSECUTIVE;
INSERT INTO T_BH_LOG_TABLE (ASO_NCODE, CHA_NCODE, TSO_NCODE,
MSO_DACTION_DATE, MSO_CRESULT, MSO_CCAUSE_FAILURE)
OUTPUT INSERTED.MSO_NCODE INTO #TBL_CONSECUTIVE
SELECT #ASO_NCODE, ISNULL(#CHA_NCODE, 1), ISNULL(#TSO_NCODE, 1),
GETDATE() AS MSO_DACTION_DATE, #CST_FAIL_OR_SUC, #CST_GENERIC_MSG;
IF (XACT_STATE()) = 1
BEGIN
COMMIT TRANSACTION;
END
SELECT NULL Id_table, 'Failed' Result_process, 'Parameter (ID_TABLE) is required.' Result_process_message;
RETURN;
END
-- Operation:
UPDATE MY_TABLE
SET NAME = 'SAMPLE'
WHERE ID_TABLE = #ID_TABLE;
IF (XACT_STATE()) = 1
BEGIN
COMMIT TRANSACTION;
END
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
INSERT INTO T_BH_LOG_TABLE (ASO_NCODE, CHA_NCODE, TSO_NCODE,
MSO_DACTION_DATE, MSO_CRESULT, MSO_CCAUSE_FAILURE)
OUTPUT INSERTED.MSO_NCODE INTO #TBL_CONSECUTIVE
SELECT 1 AS ASO_NCODE, 1, 1 As TSO_NCODE,
GETDATE() AS MSO_DACTION_DATE, #CST_FAIL_OR_SUC, #CST_GENERIC_MSG;
SELECT NULL Id_table, 'Failed' Result_process, 'Internal error. See log # (' + CAST(L.LOG_CONSECUTIVE AS NVARCHAR) + ') for more details.' Result_process_message;
FROM #TBL_CONSECUTIVE L;
RETURN;
END CATCH
END;
I really don't know if by using SET XACT_ABORT ON; is causing this kind of error.
Anyone can point me in the right direction for solve this issue?

Handle exceptions on procedures PL/SQL

Hi I would like to handle exceptions on my procedures to make it more useful.
My procedure adding occupations and it's wage, here is table:
CREATE TABLE Occupations(
id_oc NUMBER(2),
Name_oc VARCHAR2(20) CONSTRAINT OC_name_cc NOT NULL ,
Min_wage NUMBER(7,2) CONSTRAINT OC_min_wg NOT NULL CHECK(Min_wage>100),
CONSTRAINT Oc_id_pk PRIMARY KEY (id_oc),
CONSTRAINT OC_na UNIQUE (Name_oc)
);
Here is the procedure
create or replace procedure zad_2a(name varchar2, min_wage number)
is
isFound occupations.name_oc%TYPE;
cursor c1
is
select name_oc from occupations where name_oc = name;
begin
open c1;
fetch c1 into isFound;
if c1%found then dbms_output.put_line('Occupation already exists');
else insert into occupations values(Seq_Occupations.NEXTVAL, name, min_wage);
end if;
close c1;
end;
/
This procedure should have following exceptions:
When I set wage fewer than 100 and more than 5000
While I add bad occupation name - using numbers instead of characters
handling other unexcepted errors
How to do this?
You can try as below and modify as per your requirements.
CREATE OR REPLACE PROCEDURE zad_2a (ename VARCHAR2, min_wage NUMBER)
IS
isfound occupations.name_oc%TYPE;
e_custom_exception EXCEPTION;
PRAGMA EXCEPTION_INIT (e_custom_exception, -20001);
l_count_int NUMBER;
CURSOR c1
IS
SELECT name_oc
FROM occupations
WHERE name_oc = ename;
BEGIN
IF (min_wage < 100 OR min_wage > 5000)
THEN
RAISE e_custom_exception;
END IF;
SELECT COUNT (*)
INTO l_count_int
FROM DUAL
WHERE REGEXP_LIKE (ename, '[[:digit:]]');
IF (l_count_int > 0)
THEN
RAISE e_custom_exception;
END IF;
OPEN c1;
FETCH c1 INTO isfound;
IF c1%FOUND
THEN
DBMS_OUTPUT.put_line ('Occupation already exists');
ELSE
INSERT INTO occupations
VALUES (Seq_Occupations.NEXTVAL, ename, min_wage);
END IF;
CLOSE c1;
EXCEPTION
WHEN e_custom_exception
THEN
DBMS_OUTPUT.put_line (SQLERRM);
WHEN OTHERS
THEN
RAISE;
END;
/

PL/SQL: ORA-00932: inconsistent datatypes: expected NUMBER got USER_NAME.VARCHAR_ARRAY

The below is a function that I am creating to accept an array of varchar2 items and return the internal pk of that record which is a NUMBER for each record. I am struggling to get the syntax right to pass an array of type VARCHAR_ARRAY to the simple sql query in the cursor and return the variable of type NUMBER_ARRAY. the Error is on line 8,42 i.e FROM table_name WHERE column_name IN VARCHAR_ARRAY which was passed to the function. Please help me with this error as I am learning plsql.
create or replace TYPE VARCHAR_ARRAY AS VARRAY(1000000) OF VARCHAR2(1000);
/
create or replace TYPE NUMBER_ARRAY AS VARRAY(1000000) OF NUMBER;
/
create or replace Function GET_PRODUCT_ID_ARR(V_PRODUCT_NUM_ARR IN VARCHAR_ARRAY)
RETURN NUMBER_ARRAY
IS
product_id_list number_array := number_array();
CURSOR c1
IS
SELECT cat_map_id
FROM mn_cat_map WHERE product_num IN (V_PRODUCT_NUM_ARR) and catalog_type = 'INT';
v_output NUMBER;
BEGIN
OPEN c1;
LOOP
fetch c1 into product_id_list;
EXIT WHEN c1%notfound;
product_id_list.extend;
product_id_list(product_id_list.count) := v_output;
dbms_output.put_line('Product ('||v_output ||'):'||product_id_list(v_output));
END LOOP;
Close c1;
RETURN product_id_list;
END;
/
There is two issues:
You have to cast varray to table:
CURSOR c1
IS
SELECT cat_map_id
FROM mn_cat_map
WHERE product_num IN (select column_value from table(V_PRODUCT_NUM_ARR))
and catalog_type = 'INT';
Add bulk collect after fetch:
LOOP
fetch c1 bulk collect into product_id_list limit 100;
EXIT WHEN c1%notfound;
product_id_list.extend;
product_id_list(product_id_list.count) := v_output;
dbms_output.put_line('Product ('||v_output ||'):'||product_id_list(v_output));
END LOOP;
If you write limit 100, each loop will put 100 records in product_id_list. You can omit limit clause, in this case you will get all records in one fetch.
EDIT
How to see results:
declare
myarray varchar_array;
my_num_array number_array;
begin
myarray := varchar_array();
myarray.extend(2);
myarray(1) := '151043';
myarray(2) := '2895';
my_num_array := GET_PRODUCT_ID_ARR(myarray);
for i in 1 .. my_num_array.count loop
dbms_output.put_line(my_num_array(i));
end loop;
end;
/
What #William is said is quiet true. VARRAY(1000000) is not recommended. Inplace you can create a type of table. However if i follow what you have done, there seems some mistakes in your code. Please see below how you can do it.
Tables Preparation:
create table mn_cat_map(cat_map_id number,
product_num varchar2(1000),
catalog_type varchar2(10));
/
INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
VALUES (10, 'A123', 'INT');
INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
VALUES (2, 'B121', '2Wheer');
INSERT INTO T541682.MN_CAT_MAP (CAT_MAP_ID, PRODUCT_NUM, CATALOG_TYPE)
VALUES (3, 'C645', '4Wheer');
COMMIT;
create or replace TYPE VARCHAR_ARRAY AS VARRAY(1000000) OF VARCHAR2(1000);
/
create or replace TYPE NUMBER_ARRAY AS VARRAY(1000000) OF NUMBER;
/
Code: read explainatory comments inline
CREATE OR REPLACE FUNCTION GET_PRODUCT_ID_ARR (V_PRODUCT_NUM_ARR VARCHAR_ARRAY)
RETURN NUMBER_ARRAY
IS
product_id_list number_array := number_array ();
CURSOR c1(tbl_list VARCHAR_ARRAY)
IS
SELECT cat_map_id
FROM mn_cat_map
WHERE product_num in (select column_value from table(tbl_list)) ---Checking if the item exists in the table with passed collection
AND catalog_type = 'INT';
v_output NUMBER:= 0;
BEGIN
--not opening cursor and am not looking for processing any records.
--OPEN c1(V_PRODUCT_NUM_ARR);
--passing the input varray to the cursor.
for i in c1(V_PRODUCT_NUM_ARR)
loop
v_output:=v_output + 1;
product_id_list.extend;
product_id_list(product_id_list.COUNT):= i.cat_map_id;
DBMS_OUTPUT.put_line('Product (' || v_output || '):' ||product_id_list(product_id_list.COUNT));
end loop;
RETURN product_id_list;
END;
/
Execution:
SQL> select GET_PRODUCT_ID_ARR(VARCHAR_ARRAY('A123','B121','C645')) COl1 from dual;
COL1
--------------------------------------------------------------------------------
NUMBER_ARRAY(10)
Product (1):10

Bulk collect and forall with save exception

I have the below code , emp1 table has data of 12 rows in which 2 empno's already present in emp table. i'm trying to save the exceptions for these 2 records and to insert remaining all to emp. But everything is getting error out and i couldn't able to insert into emp. Anyone please help.
SET SERVEROUTPUT ON;
DECLARE
CURSOR C1
IS
SELECT EMPNO FROM EMP1;
TYPE T
IS
TABLE OF C1%ROWTYPE;
L_DATA T;
L_ERRORS_COUNT NUMBER;
EX_DML_ERRORS EXCEPTION;
PRAGMA EXCEPTION_INIT (ex_DML_ERRORS, -47);
L_ERRNO NUMBER;
L_MSG VARCHAR2(4000);
L_IDX NUMBER;
BEGIN
OPEN C1;
LOOP
FETCH C1 BULK COLLECT INTO L_DATA;
BEGIN
FORALL I IN 1..L_DATA.COUNT SAVE EXCEPTIONS
INSERT
INTO EMP
(
EMPNO,
ENAME,
MGR
)
VALUES
(
L_DATA(I).EMPNO,
'SCOTT',
L_DATA(I).EMPNO
);
EXCEPTION
WHEN dup_val_on_index THEN
DBMS_OUTPUT.PUT_LINE('WHD');
L_ERRORS_COUNT := SQL%BULK_EXCEPTIONS.COUNT;
DBMS_OUTPUT.PUT_LINE('WHD::::'||L_ERRORS_COUNT);
FOR J IN 1..L_ERRORS_COUNT
LOOP
L_ERRNO := SQL%BULK_EXCEPTIONS
(
J
)
.ERROR_CODE;
L_MSG := SQLERRM(-L_ERRNO);
L_IDX := SQL%BULK_EXCEPTIONS(J).ERROR_INDEX;
DBMS_OUTPUT.PUT_LINE('I am here SVS:'||L_ERRNO||':'|| L_MSG||':'||L_IDX);
--INSERT INTO EMPS_EXC1(SQLC ,SQLE ,INDX ) VALUES(L_ERRNO, L_MSG,L_IDX);
DBMS_OUTPUT.PUT_LINE('I am here SVS:'||L_ERRNO||':'|| L_MSG||':'||L_IDX);
END LOOP;
END;
EXIT
WHEN C1%NOTFOUND;
END LOOP;
CLOSE C1;
END;

How to find problems in MySQL stored procedure?

I have the following stored procedure:
DELIMITER $$
CREATE DEFINER=`195414_py82740`#`%` PROCEDURE `getUserMail`(inUserId INT)
BEGIN
DECLARE done INT default 0;
DECLARE tmpMailId INT default -1;
DECLARE tmpFromUserId INT default -1;
DECLARE cursor1 cursor for select id from mail WHERE toUserId=inUserId OR fromUserId=inUserId ORDER BY sentDate desc;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
DROP TEMPORARY TABLE IF EXISTS temp_userMail;
CREATE TEMPORARY TABLE temp_userMail LIKE mail;
OPEN cursor1;
REPEAT
FETCH cursor1 INTO tmpMailId;
SET tmpFromUserId = IF ((SELECT fromUserId FROM mail WHERE id=tmpMailId)=inUserId, (SELECT toUserId FROM mail WHERE id=tmpMailId), (SELECT fromUserId FROM mail WHERE id=tmpMailId));
IF EXISTS(SELECT id FROM temp_userMail WHERE fromUserId=tmpFromUserId || toUserId=tmpFromUserId) THEN
SET tmpMailId = 0;
ELSE
INSERT INTO temp_userMail
(`id`,
`fromUserId`,
`toUserId`,
`sentDate`,
`readDate`,
`message`,
`fromUserNickName`,
`toUserNickName`,
`subject`,
`fromUserDeleted`,
`toUserDeleted`)
SELECT
(m1.id,
m1.fromUserId,
m1.toUserId,
m1.sentDate,
m1.readDate,
m1.message,
m1.fromUserNickName,
m1.toUserNickName,
m1.subject,
m1.fromUserDeleted,
m1.toUserDeleted)
FROM mail m1
WHERE m1.id=tmpMailId;
END IF;
UNTIL(done = 1)
END REPEAT;
CLOSE cursor1;
SELECT * FROM temp_userMail ORDER BY sentDate DESC;
END
The SP is saved successfully but when running it I get the cryptic exception
Error Code: 1241: Operand should contain 1 column(s).
I know what the error means but there is no line nr so I have no idea where to look for the problem?
Remove brackets from Your SELECT subquery
dont use
SELECT (id, ... m1.toUserDeleted) ...
use
SELECT id, ... m1.toUserDeleted ...