In Exception Log want to print data value - sql

declare
v number;
c varchar2(20);
begin
select c into v from dual;
exception
when others
then
raise;
end;
while running this block i am getting exception because i m providing input on c dynamically (suppose) as character. i want raise will print the error with value of C.
Is there any way that we can add message + value of C in raise statement.

By adding simple :
Select CONCAT(raise,'C');
will print its value in the console

Related

Function returning error texts in Oracle APEX

I am trying to take a count of the records in the interactive grid and based on that I am trying to pass a message to the user. However, I am getting error : ORA-06550: line 1, column 141: PLS-00103: Encountered the symbol "NUMBER" when expecting one of the following: := . ( # % ; The symbol "." was substituted for "NUMBER" to continue.Following is my code in the validation. Validation Type is : Function Returning Error Text.
l_count NUMBER := 0;
BEGIN
SELECT COUNT(1)
INTO l_count
FROM ugh
WHERE ugh.pre = :PRE
AND ugh.APP1 = :APP1
AND ugh.APP2 = :APP2
AND ugh.APP3 = :APP3
AND ugh.FINL_APP = :FINL_APP;
IF l_count > 1 THEN
IF END_DATE IS NULL THEN
RETURN 'Error Message to be displayed.';
ELSE
RETURN NULL;
END IF;
ELSE
RETURN NULL;
END IF;
END;
Can anyone please help ?
Looks like you're missing the DECLARE keyword:
DECLARE --> this
l_count NUMBER := 0;
BEGIN
SELECT COUNT (1)
INTO l_count
FROM ugh
Also, what is END_DATE? You never declared it. If it is a page item, then precede it with a colon, :END_DATE

Not able to create a matrix in PL/SQL using nested tables

I was trying to create a matrix in PL/SQL using nested tables as such:
firstly, I create a table type that is able to store varchars (that in my case is name lista_principala)
secondly, using the previously declare table type lista_principala I try to create a table type that contains items of type lista_principala.
I don't think that I'm wrong about my logic, but my PL/SQL implementation looks something like this:
set serveroutput on;
DECLARE
TYPE lista_principala_tip IS TABLE OF varchar2(30);
TYPE lista_liste_tip IS TABLE OF lista_principala_tip;
lista lista_liste_tip;
BEGIN
lista := lista_liste_tip();
lista.extend(20);
lista(1) := lista_principala_tip('Gorila', 'Babuin', 'Urangutan', 'Cimpanzeu', 'Gibon');
lista(2) := lista_principala_tip('Labrador', 'Bulldog', 'Bichon', 'Ciobanesc German');
lista(3) := lista_principala_tip('British Shorthair', 'Siamese', 'Scottish Fold', 'Chartreux');
for i in lista.first..lista.last loop
for j in lista(i).first..lista(i).last loop
DBMS_OUTPUT.PUT_LINE(i||' - '||lista(i)(j));
end loop;
end loop;
END;
And the problem that I have is that when I try to run this script I get the following errors:
Error report -
ORA-06531: Reference to uninitialized collection
ORA-06512: at line 12
You use lista.extend(20); to create a list with 20 items and these will all be initialised to NULL.
Then you set the values for the first 3 elements of the collection.
Then you loop through all 20 items and try to loop through the sub-list in each element; however, after the first 3, there is no sub-list contained in the element as the element is NULL.
Either:
Just lista.EXTEND(3); as you only want 3 elements to the array; or
Add a check if the list element is NULL and then skip looping through the sub-list if it is.
The second option can be implemented as:
DECLARE
TYPE lista_principala_tip IS TABLE OF varchar2(30);
TYPE lista_liste_tip IS TABLE OF lista_principala_tip;
lista lista_liste_tip;
BEGIN
lista := lista_liste_tip();
lista.extend(20);
lista(1) := lista_principala_tip('Gorila', 'Babuin', 'Urangutan', 'Cimpanzeu', 'Gibon');
lista(2) := lista_principala_tip('Labrador', 'Bulldog', 'Bichon', 'Ciobanesc German');
lista(3) := lista_principala_tip('British Shorthair', 'Siamese', 'Scottish Fold', 'Chartreux');
for i in lista.first..lista.last loop
IF lista(i) IS NOT NULL THEN
for j in lista(i).first..lista(i).last loop
DBMS_OUTPUT.PUT_LINE(i||' - '||lista(i)(j));
end loop;
END IF;
end loop;
END;
/
db<>fiddle here

SQL Trigger with cursor not working

I'm doing a sql work but I'm getting trouble with creating a trigger and I have no idea why.
Here's the code:
CREATE OR REPLACE TRIGGER artigo_funcionario
BEFORE INSERT ON encomendaartigo
FOR EACH ROW
DECLARE
artigo_invalido exception;
artigo number(3);
auxiliar number(1):=0;
auxiliar_numero number(3);
CURSOR dotacao_funcionario is
select a.artigo_id
from artigo a, familia fa, dotacao d, encomenda e, funcionario f, categoria c
where e.encomenda_id = NEW.encomenda_id
and f.funcionario_id=e.funcionario_id
and f.categoria_id=c.categoria_id
and d.categoria_id=c.categoria_id
and fa.familia_id=d.familia_id
and a.familia_id=fa.familia_id;
BEGIN
open dotacao_funcionario;
loop
fetch dotacao_funcionario into artigo;
EXIT WHEN dotacao_funcionario%notfound;
auxiliar_numero := NEW.artigo_id;
if(artigo = auxiliar_numero) then
auxiliar:=1;
end if;
end loop;
close dotacao_funcionario;
if(auxiliar=0) then
raise artigo_invalido;
end if;
EXCEPTION
WHEN artigo_invalido THEN
dbms_output.put_line('O funcionário não pode encomendar o artigo introduzido');
END;
/
Be aware that the problem is not with the cursor so don't worry about the tables I'm using. the only one that is relevant is the "encomendaartigo" and it has the following attributes: "encomenda_id", "artigo_id".
The problem is that I'm can't quite use the "new.xxxxx" operator. if ran, the compiler log shows the following:
Error(8,7): PL/SQL: SQL Statement ignored
Error(10,30): PL/SQL: ORA-00904: "NEW"."ENCOMENDA_ID": identificador inválido
Error(25,7): PL/SQL: Statement ignored
Error(25,29): PLS-00201: identifier 'NEW.ARTIGO_ID' must be declared
I've tried everything and nothing seems to be working.
You are missing the colon before the NEW. e.g.)
auxiliar_numero := :NEW.artigo_id;
Not
auxiliar_numero := NEW.artigo_id;
Optional implementation per your comments:
CREATE OR REPLACE TRIGGER artigo_funcionario
BEFORE INSERT ON encomendaartigo
FOR EACH ROW
DECLARE
l_check_artigo_id number := 0;
CURSOR dotacao_funcionario is
select 1
from artigo a, familia fa, dotacao d, encomenda e, funcionario f, categoria c
where e.encomenda_id = :NEW.encomenda_id
and f.funcionario_id=e.funcionario_id
and f.categoria_id=c.categoria_id
and d.categoria_id=c.categoria_id
and fa.familia_id=d.familia_id
and a.familia_id=fa.familia_id
and a.artigo_id = :NEW.artigo_id;
BEGIN
open dotacao_funcionario;
fetch dotacao_funcionario into l_check_artigo_id;
IF l_check_artigo_id = 1 then
raise_application_error(-20001, 'O funcionário não pode encomendar o artigo introduzido');
END IF;
CLOSE dotacao_funcionario;
EXCEPTION
WHEN OTHERS THEN
IF sqlcode = -20001 then
dbms_output.put_line('O funcionário não pode encomendar o artigo introduzido');
END IF;
-- a simple RAISE will re-raise the exception
RAISE;
END;
/

Invalid Identifier When Trying to Use Package Function in Summation Expression

I'm trying to use the sum function with a package function but running into an "invalid identifier" bug. Here's some example code with the error causing function commented
create or replace type numType as object
(
myNum number
)
;
/
create or replace type numTypes is table of numType;
/
create or replace package testNumberPackage as
function ReturnNum(in_numType numType) return number;
end;
/
create or replace package body testNumberPackage as
function ReturnNum(in_numType numType) return number is
begin
return in_numType.myNum;
end;
end;
/
declare l_numTypes numTypes;
l_count number;
begin
l_numTypes := numTypes();
for i in 1 .. 100 loop
l_numTypes.extend(1);
l_numTypes(l_numTypes.last) := numType(i);
end loop;
select sum(n.myNum) into l_count from table(l_numTypes) n;
select sum(testNumberPackage.ReturnNum(n)) into l_count from table(l_numTypes) n; --causes the error
dbms_output.put_line(l_count);
end;
/
The exact error for this code is
ORA-06550: line 11, column 42
PL/SQL: ORA-00904: "N": invalid identifier
ORA-6550: line 11, column 3:
PL/SQL: SQL Statement ignored
Thanks for any help.
The first issue is that you can't pass a table into a parameter by using its alias. It doesn't even make sense to try doing that.
The next issue is how to get the column mynum that is returned from the table(l_numTypes) into the correct format to pass into testNumberPackage.ReturnNum, since it's of NUMBER datatype, and the function is expecting a numtype parameter.
To do that, you need to pass in an object with that column, like so: numtype(n.mynum).
The following works for me:
declare
l_numTypes numTypes;
l_count number;
begin
l_numTypes := numTypes();
for i in 1 .. 100 loop
l_numTypes.extend(1);
l_numTypes(l_numTypes.last) := numType(i);
end loop;
select sum(n.myNum) into l_count from table(l_numTypes) n;
select sum(testNumberPackage.ReturnNum(numtype(n.mynum))) into l_count from table(l_numTypes) n; --causes the error
dbms_output.put_line(l_count);
end;
/
5050
Clear as mud?

How to Convert Output of listagg() function into XMLTYPE

I have one stored Procedure P_FP_GET_PATTERN.I expect output in following format
PATTERN_ID |PATTERN_NAME | SHIFT
1 Pattern 1 A,B,C,B,A
2 Pattern 2 C,B,A,C
I'm getting this output in SYS_REFCURSOR ALL_RESULT_SET.I need to pass it in XML format.But the moment i put that output in ALL_RESULT_SET_XML which is my out parameter of stored procedure of XMLTYPE using ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);
Im getting an error as
Error encountered: ORA-31061: XDB error: special char to escaped char conversion failed.
I`m getting this error due to column shown using LISTAGG() function.Anybody can please tell me how to handle this ?
My stored Procedure
create or replace
PROCEDURE P_FP_GET_PATTERN
(
ALL_RESULT_SET_XML OUT XMLTYPE,
P_MESSAGE_ALL OUT VARCHAR2
)
AS
V_ERROR VARCHAR2(2000);
ALL_RESULT_SET SYS_REFCURSOR;
BEGIN
OPEN ALL_RESULT_SET FOR
SELECT PM.PATTERN_ID ,PM.PATTERN_NAME,
LISTAGG(SM.SHIFT_NUMBER) WITHIN GROUP (ORDER BY PD.INSTANCE_DAY) "SHIFT"
FROM T_FP_PATTERN_MASTER PM,
T_FP_PATTERN_DETAILS PD,
T_FP_SHIFT_MASTER SM
WHERE SM.SHIFT_ID= PD.SHIFT_ID
AND PM.PATTERN_ID = PD.PATTERN_ID
GROUP BY PM.PATTERN_NAME,PM.PATTERN_ID;
--Adding output in XML output parameter
ALL_RESULT_SET_XML:= XMLTYPE(ALL_RESULT_SET);
EXCEPTION
WHEN OTHERS
THEN
V_ERROR := SUBSTR(SQLERRM,1,1000);
P_MESSAGE_ALL := 'Error encountered: '||V_ERROR ;
END;