Exception PL/SQL - oracle

Caution my english is horrible.
No one answers in the spanish forum.
doubts :
¿How can I control the exception of a empty entry? (Explanation following)
¿What value is saved in the variable if there's an empty entry?
I created this variable:
DECLARE
v_input NUMBER(8) := &entry;
I don't give a value, I mean in the pop up I accept directly without writing anything.
POPUP IMG
when I read the error code ( that I will leave at the end), I came across this line.
PLS-00103: Encountered the symbol ' ; ' when expecting one of the following :
So I guess the variable default value if you do a empty entry is -> ;
then I thought of controling the exception creating a IF, this if compares my variable (with empty entry/value) to ' ; ' you can see below.
BEGIN
DBMS_OUTPUT.PUT_LINE(v_codigo);
IF(v_codigo = ';')THEN
RAISE v_excepcion;
END IF;
EXCEPTION
when v_excepcion then
DBMS_OUTPUT.PUTLINE('No has insertado un valor válido');
END;
ERROR CODE
Informe de error -
ORA-06550: línea 2, columna 32:
PLS-00103: Se ha encontrado el símbolo ";" cuando se esperaba uno de los siguientes:
( - + case mod new not null <an identifier>
<a double-quoted delimited-identifier> <a bind variable>
continue avg count current exists max min prior sql stddev
sum variance execute forall merge time timestamp interval
date <a string literal with character set specification>
<a number> <a single-quoted SQL string> pipe
<an alternatively-quoted string literal with character set specification>
<an al
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
sorry for my grammar mistakes, If it does not clear I will follow the post for give you more information.
thx

Exception handlers are for exceptions (errors) encountered during execution of your function or procedure or anonymous block. They don't handle syntax errors - where the function or procedure won't even compile.
You are using a substitution variable - one that is "substituted" (replaced) by your editor BEFORE the code is ever presented to the PL/SQL parser. THAT kind of error can't be anticipated and handled in the code - if you return nothing (an empty string) when prompted, the code itself will receive "nothing" in that place, and the syntax of your function or procedure will be incorrect, so you will get the syntax error you received. Your code looks exactly like this after the substitution:
v_input NUMBER(8) := ;
resulting exactly in the syntax error you have seen.
The correct way to pass in a value at run time is to use parameters (for functions or procedures), or bind variables in an anonymous block. The variable must be declared and initialized before executing the block (in a command line environment like SQL*Plus); or, in a GUI like Toad or SQL Developer, you will be prompted for a value for the bind variable. If you press Enter without giving a value, that is interpreted as NULL, which is perfectly valid. (Whether it is what you want or not, that's a different question.)
The code would have to look something like this:
declare
v_input number(8) := :entry; -- notice the leading colon :
begin
dbms_output.put_line(v_input);
end;
/
If you want to handle the case of NULL input, you would do it in code:
declare
v_input number(8) := :entry;
begin
if v_input is null then
-- handle it here
else
dbms_output.put_line(v_input); -- or whatever else you need to do
end if;
end;
/

You don't need exception to handle that; you could dbms_output.put_line (note that you used putline, which is wrong) within IF, or even raise_application_error if you want to stop execution. For example (valid value first):
SQL> declare
2 v_codigo varchar2(20) := '&entry';
3 begin
4 if v_codigo = ';' or v_codigo is null then
5 raise_application_Error(-20000, 'Invalid value');
6 else
7 dbms_output.put_line('You entered ' || v_codigo);
8 end if;
9 end;
10 /
Enter value for entry: Littlefoot
You entered Littlefoot
PL/SQL procedure successfully completed.
Invalid values: empty string and ;;
SQL> /
Enter value for entry:
declare
*
ERROR at line 1:
ORA-20000: Invalid value
ORA-06512: at line 5
SQL> /
Enter value for entry: ;
declare
*
ERROR at line 1:
ORA-20000: Invalid value
ORA-06512: at line 5
SQL>
Or, to reuse your own code: declare the exception first (line #3), then use it:
SQL> declare
2 v_codigo varchar2(20) := '&entry';
3 v_excepcion exception;
4 BEGIN
5 DBMS_OUTPUT.PUT_LINE(v_codigo);
6 IF(v_codigo = ';' or v_codigo is null) THEN
7 RAISE v_excepcion;
8 END IF;
9
10 EXCEPTION
11 when v_excepcion then
12 DBMS_OUTPUT.PUT_LINE('No has insertado un valor válido');
13 END;
14 /
Enter value for entry: Littlefoot
Littlefoot
PL/SQL procedure successfully completed.
SQL> /
Enter value for entry:
No has insertado un valor válido
PL/SQL procedure successfully completed.
SQL> /
Enter value for entry: ;
;
No has insertado un valor válido
PL/SQL procedure successfully completed.
SQL>

Related

plsql using in datagrip ide

When I am running PL/SQL scripts in Datagrip I am getting task compiled but I cannot see the output how to get an output in datagrip,
and when I am running dynamic block it's not taking input value in datagrip.
declare
v1 emp.empno%type := '&empno';
v2 emp.ename%type;
begin
select empno,ename into v1,v2 from emp where empno = v1;
DBMS_OUTPUT.PUT_LINE(v1 || ' ' || v2);
end;
it shows this error
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
Does anyone have any solution?
Enable DBMS_OUTPUT button is placed on the right toolbar of the Services window
I don't use DataGrip.
Looks like you should enable dbms_output. How? Try with [Ctrl + F8] (as a keyboard shortcut), or push the appropriate button (tooltip says "Enable SYS.DBMS_OUTPUT").
As of the second part of your question, documentation says
Generally, only the question mark ? is treated as a parameter in SQL statements, which means that you might need to do this:
v1 emp.empno%type := ?;
Because, code you posted works OK in e.g. SQL Developer or SQL*Plus:
Enabling DBMS_OUTPUT:
SQL> set serveroutput on
Code execution:
SQL> declare
2 v1 emp.empno%type := '&empno';
3 v2 emp.ename%type;
4 begin
5 select empno,ename into v1,v2 from emp where empno = v1;
6 DBMS_OUTPUT.PUT_LINE(v1 || ' ' || v2);
7 end;
8 /
Enter value for empno: 7369
7369 SMITH --> this is the result
PL/SQL procedure successfully completed.
SQL>
Finally, ORA-06502: it happens when you pass something that isn't a number (while number is expected); for example, I'll pass ABC:
SQL> /
Enter value for empno: ABC
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2
SQL>
See? The same error you got. Maybe it all has to do something with improper usage of a substitution variable, i.e. '&empno' vs. ?

PLS-00103: Encountered the symbol "IS" when expecting one of the following: := . ( # % ; not null range default character

I am facing this error:
LINE/COL ERROR
-------- -----------------------------------------------------------------
4/11 PLS-00103: Encountered the symbol "IS" when expecting one of the
following:
:= . ( # % ; not null range default character
My package is:
CREATE OR REPLACE PACKAGE BODY EMP_PK AS
PROCEDURE INSERT_TR(EMPNO EMP_20171250.EMPNO%TYPE,ENAME EMP_20171250.ENAME%TYPE,SAL EMP_20171250.SAL%TYPE) IS
INSERT_ERROR EXCEPTION;
CRUSOR C1 IS INSERT INTO EMP_20171250(EMPNO,ENAME,SAL) VALUES(EMPNO,ENAME,SAL);
BEGIN
IF(C1%FOUND) THEN
DBMS_OUTPUT.PUT_LINE('RECORD INSERTED');
ELSE
RAISE INSERT_ERROR;
END IF;
EXCEPTION
WHEN INSERT_ERROR THEN
DBMS_OUTPUT.PUT_LINE('ERROR WHILE RECORD INSERTION');
END INSERT_TR;
END EMP_PK;
it is not CRUSOR but CURSOR
cursor can't contain INSERT statement; it is a SELECT
you are checking whether cursor returned something, but - you never opened it nor fetched from it so it is pretty much useless
insert_error looks like there was an error while inserting a row, but - you are actually raising it if cursor didn't return anything
Basically, you don't need a cursor at all. Such a procedure would do:
PROCEDURE insert_tr (p_empno emp_20171250.empno%TYPE,
p_ename emp_20171250.ename%TYPE,
p_sal emp_20171250.sal%TYPE)
IS
BEGIN
INSERT INTO emp_20171250 (empno, ename, sal)
VALUES (p_empno, p_ename, p_sal);
END insert_tr;
If insert fails, Oracle will raise an exception anyway so - yes, you can handle it/them if you want.
Also, it is good to distinguish parameter names from column names; for example, precede their names with a p_ (as I did).

Creating a anonymous block to display 1 to 5 and getting the below error

ORA-06550: line 2, column 11:
PLS-00103: Encountered the symbol "=" when expecting one of the following:
constant exception <an identifier>
<a double-quoted delimited-identifier> table long double ref
char time timestamp interval date binary national character
nchar
The symbol "<an identifier>" was substituted for "=" to continue.
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
Here is the code:
SET SERVEROUTPUT ON;
clear scr;
DECLARE v_counter := 0;
BEGIN
LOOP
v_counter:= v_counter+1;
IF v_counter=3 THEN CONTINUE; END IF;
EXIT WHEN v_counter=5;
END LOOP;
DBMS_OUTPUT.PUT_LINE('v_counter='||v_counter);
END;
Creating a anonymous block to display 1 to 5 and getting the below error
Your code will never display 1 to 5. As #David said in the comments -
You haven't declared the data type for the variable.
The DBMS_OUTPUT is outside the LOOP, thus it will print only the last value held by the variable.
That IF-ELSE construct seems unnecessary if you just want to print 1 to 5.
You could achieve the same in just one FOR LOOP -
SQL> SET SERVEROUTPUT ON
SQL> BEGIN
2 FOR i IN 1..5
3 LOOP
4 DBMS_OUTPUT.PUT_LINE('v_counter='||i);
5 END LOOP;
6 END;
7 /
v_counter=1
v_counter=2
v_counter=3
v_counter=4
v_counter=5
PL/SQL procedure successfully completed.
SQL>
The same could be done in SQL -
SQL> select 'v_counter ='||level counter from dual connect by level <=5;
COUNTER
---------------------------------------------------
v_counter =1
v_counter =2
v_counter =3
v_counter =4
v_counter =5
SQL>
The problem is the line reading
DECLARE v_counter := 0;
You forgot to specify the variable's datatype.
It should read
DECLARE v_counter number := 0;
Of course, instead of number you might choose pls_integer if you're more confortable with it.

expression 'string' cannot be used as an assignment target -SQL PLUS

I wrote the following procedure which was meant to be anonymous and remove all the vowels from a string, but when I call it I get an error: I've followed the advice given in a similar post, but it didn't help:Oracle PLS-00363: expression '' cannot be used as an assignment target
SQL> CREATE OR REPLACE PROCEDURE disemvowel (string IN OUT NVARCHAR2)
2 IS
3 BEGIN
4 DBMS_OUTPUT.PUT_LINE(translate(string,'euioa',''));
5 END disemvowel;
6 /
Procedure created.
So good so far, but now I call it:
SQL> BEGIN
2 disemvowel('hahahahaha');
3 END;
4 /
The Error message says:
disemvowel('hahahahaha');
*
ERROR at line 2:
ORA-06550: line 2, column 12:
PLS-00363: expression 'hahahahaha' cannot be used as an assignment target
ORA-06550: line 2, column 1:
PL/SQL: Statement ignored
Your procedure has IN OUT parameter. So while calling the procedure you should supply a variable to it, so that it can hold the value that the procedure gives back. You cannot supply a value directly, as it cannot be modified by the procedure.
DECLARE
param NVARCHAR2 (20) := 'hahahahaha';
BEGIN
disemvowel (param);
END;
/
Generate new VARCHAR2 type variable to assign your IN (input) string.
PROCEDURE sp_name(
ps_list IN VARCHAR2,
...
write here other IN's and OUT's
...
)
AS
ps_list_copy VARCHAR2 (32000);
BEGIN
ps_list_copy := ps_list;
...
do your works with ps_list_copy
...
...
EXCEPTION WHEN OTHERS THEN
....
END sp_name;

Why do I get an error as I try to call a procedure?

I created a procedure named greet as :
create procedure greet(message in char(50))
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
The procedure compiled successfully but when I try to call it as :
execute greet('Hey ! This is a self created procedure :)');
I get an error :
execute greet('Hey ! This is a self created procedure :)')
Error report:
ORA-06550: line 1, column 7:
PLS-00905: object SUHAIL.GREET is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
What error is it ? Why do I get it ?
Note : 'suhail' is name of the current user connected to oracle server
I don't believe that your procedure compiled successfully. When I try to compile it on my system, I get syntax errors
SQL> create procedure greet(message in char(50))
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5 end;
6 /
Warning: Procedure created with compilation errors.
SQL> sho err
Errors for PROCEDURE GREET:
LINE/COL ERROR
-------- -----------------------------------------------------------------
1/32 PLS-00103: Encountered the symbol "(" when expecting one of the
following:
:= ) , default varying character large
The symbol ":=" was substituted for "(" to continue.
If I resolve the syntax errors (you cannot specify a length for an input parameter), it works
SQL> ed
Wrote file afiedt.buf
1 create or replace procedure greet(message in char)
2 as
3 begin
4 dbms_output.put_line('Greet Message : ' || message);
5* end;
SQL> /
Procedure created.
SQL> set serveroutput on;
SQL> execute greet('Hey ! This is a self created procedure :)');
Greet Message : Hey ! This is a self created procedure :)
PL/SQL procedure successfully completed.
I would be shocked if you really wanted the input parameter to be declared as CHAR. Almost always, you should use VARCHAR2 for character strings. It is exceptionally rare to come across a case where you really want the blank-padding semantics of a CHAR.
this is working dude;
create or replace
procedure greet(message in char)
as
begin
dbms_output.put_line('Greet Message : ' || message);
end;
see main property of char datatype is is the length of input data is less than the size you specified it'll add blank spaces.this case is not happened for varchar2.
in procedure above mentioned char property is violated so it's almost treat like varchar2. so if you remove size of input parameter it will work and also char support maximum length of input.

Resources