plsql using in datagrip ide - oracle

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. ?

Related

How to avoid this in PLSQL

I'm trying to create a function that looks like this
CREATE OR REPLACE FUNCTION get_sal
(dep_id IN departments.department_id%TYPE)
RETURN NUMBER IS
v_sal employees.salary%TYPE;
BEGIN
SELECT AVG(salary) INTO v_sal FROM Employees
WHERE department_id = dep_id;
RETURN v_sal;
END;
And I get an error that says
Error starting at line : 5 in command -
BEGIN
SELECT AVG(salary) INTO v_sal FROM Employees
WHERE department_id = dep_id;
RETURN v_sal;
END;
Error report -
ORA-06550: line 3, column 27:
PL/SQL: ORA-00904: "DEP_ID": invalid identifier
ORA-06550: line 2, column 5:
PL/SQL: SQL Statement ignored
ORA-06550: line 4, column 5:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 4, column 5:
PL/SQL: Statement ignored
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:
I got an example function from Oracle to see if it works but the same error appears.
There's nothing wrong with the function:
SQL> CREATE OR REPLACE FUNCTION get_sal (dep_id IN departments.department_id%TYPE)
2 RETURN NUMBER
3 IS
4 v_sal employees.salary%TYPE;
5 BEGIN
6 SELECT AVG (salary)
7 INTO v_sal
8 FROM Employees
9 WHERE department_id = dep_id;
10
11 RETURN v_sal;
12 END;
13 /
Function created.
SQL> select get_sal(10) from dual;
GET_SAL(10)
-----------
SQL>
as long as schema you're connected to contains DEPARTMENTS and EMPLOYEES tables with columns mentioned in that code. If you do not, then yes - expect errors.
As it seems you are using Sql Developer 20.4.0 as your SQL client, i guess the problem comes from the way you're compiling your SQL statement within this SQL client.
With SQL Developer, there is two ways to compile a SQL statement/execute a query. Selecting it in the editor and then click the green play button on the left as shown in the image below. The second way is to have only your statements in the editor and use the execute script button which is the second button to the right of the green play button. This one will execute all the statements in the editor.
I guess your error come from the use of the green play button while your cursor in the editor is on the statement but the statement is not fully selected or partially selected.
You can make SQL Developer complies with the behavior of Toad or have a custom behavior in this way in the SQL Developer options.

Exception PL/SQL

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>

when try to excure procedure it said compliation error in oracle?

I try to write select procedure in oracle.but it compile success, when I try to execute it given error.
set serveroutput on;
CREATE OR REPLACE PROCEDURE retrieve_decrypt(
custid in NUMBER,
column_name in VARCHAR2,
test_value OUT VARCHAR2
)
AS
BEGIN
-- enc_dec.decrypt(column_name,password) into test_value from employees where custid=5;
COMMIT;
END;
/
set serveroutput on;
EXEC retrieve_decrypt(5,'creditcardno');
the error says ,
This is your procedure:
SQL> create or replace procedure retrieve_decrypt
2 (custid in number,
3 column_name in varchar2,
4 test_value out varchar2
5 )
6 as
7 begin
8 -- your code goes here
9 null;
10 end;
11 /
Procedure created.
SQL>
This is how you call it (and get the error):
SQL> exec retrieve_decrypt(5, 'creditcardno');
BEGIN retrieve_decrypt(5, 'creditcardno'); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL>
The cause of the error is:
the procedure contains 3 parameters:
2 of them are IN - you provided their values
1 of them is OUT - you didn't provide it and got the error
Here's what you should have done: as the 3rd parameter is OUT, you'll have to DECLARE it:
SQL> declare
2 l_out varchar2(20);
3 begin
4 retrieve_decrypt(5, 'creditcardno', l_out);
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
EXEC you used is a SQL*Plus command so it might not work everywhere; DECLARE-BEGIN-END block will so I'd suggest you use it.
Alternatively, in SQL*Plus, it could be rewritten as
SQL> var l_out varchar2
SQL>
SQL> exec retrieve_decrypt(5, 'creditcardno', :l_out);
PL/SQL procedure successfully completed.
SQL>
but - once again - you'd better use DECLARE-BEGIN-END PL/SQL block.
The initial error is:
wrong number or types of arguments in call to 'RETRIEVE_DECRYPT'
The procedure requires 3 parameters. You are only passing 2 (or apparently only 1, in the attempt that generated the error message shown).
Why do you also see the message "Usually a PL/SQL compilation error"? The EXEC command in SQLPlus creates a PL/SQL block containing the text you provide, and sends that to Oracle for execution. Oracle attempts to compile that PL/SQL block (just like it compiles the procedure when you create it). In this case, the compilation fails because of the mismatch in the number of arguments.

Function created with compilation error in PLSQL

When I compile the below code I am getting a error message "Function created with compilation errors"
create or replace function find_port(ip_ID in int) RETURN int
is
t_count number;
count varchar;
begin
select is_rail into count from table where id = ip_ID;
case
when count ='True' then t_count:=1;
when count ='False' then t_count:=0;
end case;
end;
/
i am getting a error message "Function created with compilation errors"
So the question you should be asking is, "how do I get a list of compilation errors for my PL/SQL code?"
Other people have told you how to fix the current errors in your code, but the more important skill is that you find out how to diagnose your code for yourself.
Oracle is a database, and it stores metadata in a set of special views called the data dictionary. These views include views for compilation errors. This query will work in any SQL environments:
select name, type, line, text -- or just *, obvs
from user_errors ue
order by ue.name, ue.type, ue.sequence;
There are also ALL_ERRORS and DBA_ERRORS views. Find out more.
In SQL*Plus you can run sho err (short for show errors). IDEs like PL/SQL Developer or Oracle SQL Developer will show compilation errors automatically.
Once you know how to get the text of the errors you need to know that LINE will tell you the line where the error is raised. Although with certain classes of error (such as missing commas or unmatched brackets) the indicated line may not be the line where the actual error resides. Unfortunately there is still a need for interpretation and understanding, which requires experience.
Actually, COUNT can be used as a PL/SQL variable:
SQL> create or replace function f_test return int is
2 count number;
3 begin
4 select 1 into count from dual;
5 return 2;
6 end;
7 /
Function created.
SQL> select f_test from dual;
F_TEST
----------
2
SQL>
However, you can't return it:
SQL> create or replace function f_test return int is
2 count number;
3 begin
4 select 1 into count from dual;
5 return count;
6 end;
7 /
Warning: Function created with compilation errors.
SQL> show err
Errors for FUNCTION F_TEST:
LINE/COL ERROR
-------- -----------------------------------------------------------------
5/3 PL/SQL: Statement ignored
5/10 PLS-00204: function or pseudo-column 'COUNT' may be used inside a
SQL statement only
SQL>
Here, #priya, you can see how to help yourself - SHOW ERR will tell you what's wrong with your code.
Apart from that, CASE statement you used was invalidly written; should have been similar to this:
SQL> create or replace function f_test return int is
2 l_count number;
3 t_count number;
4 begin
5 select 1 into l_count from dual;
6
7 t_count := case when l_count = 1 then 1
8 when l_count = 2 then 2
9 end;
10
11 return t_count;
12 end;
13 /
Function created.
SQL> select f_test from dual;
F_TEST
----------
1
SQL>
count is a SQL function and thus not a better choice to be used as a PL/SQL variable. The CASE block can be used within the select statement.
Furthermore, your function does not RETURN any value.
create or replace function find_port(ip_ID in int) RETURN int
is
t_count number;
begin
select case
when is_rail = 'True' then 1
when is_rail = 'False' then 0
end into t_count from yourtable where id=ip_ID;
RETURN t_count;
end;

Declare cursor in sqlplus command mode

I have an SQL file which uses declares a cursor and I am running it using #abc, but it did not execute all statements and waiting without returning to command prompt. It did not proceed after declare cursor statement. When I tried to run the declare cursor statement in command mode, the same problem is happening again. I am able to return to SQL priompt only after pressing Ctrl + C. I am very new to SQL world. Though this could be a basic mistake, I am not able to find out the solution in any site. Any help is greatly appreciated.
SQL> DECLARE CURSOR id_cursor IS SELECT id FROM user_names WHERE dept_no = 1002
AND BITAND(flags, 4) = 4 AND time_created BETWEEN 1137974400 AND 1326067199;
2
3
4 ;
5
6
All DECLARE and BEGIN blocks in SQL*Plus need to be ended with a / on a new empty line:
SQL> DECLARE
2 CURSOR c IS SELECT 1 FROM DUAL;
3 BEGIN
4 NULL;
5 END;
6 /
PL/SQL procedure successfully completed.
Without this / SQL*Plus has no way to know that your statement has ended (so in your example it waits for user input).
After the ; type a / and enter. This will run your PL/SQL statement. But the sample you've given does nothing but declare a cursor. You must then use it like so:
declare
cursor ID_CURSOR is
select ID
from USER_NAMES
where DEPT_NO = 1002
and bitand(FLAGS, 4) = 4
and TIME_CREATED between 1137974400 and 1326067199;
begin
for REC in ID_CURSOL loop
<do something with your data>;
end loop;
end;
/

Resources