Assigning output parameter from Stored Procedure in oracle - oracle

Please help me with the following scenario
I need to call a Stored Procedure inside another one. the inner SP would return a number value which I need to store it in a local variable.
rough code
AssignSpResult NUMBER;
AssignSpResult = SPtobecalled(Param1,Param2, OutParam);
This syntax is most probably wrong, so please help correct it

Here is an example of how it might be:
HR\XE> create or replace procedure Proc1(p_out out number)
2 is
3 begin
4 p_out := 123;
5* end;
HR\XE> /
Procedure created.
HR\XE> create or replace procedure Proc2
2 is
3 l_val number;
4 begin
5 Proc1(l_val);
6 dbms_output.put_line('Here is a value returned by Proc1: ' || to_char(l_val));
7 end;
8 /
Procedure created.
HR\XE> set serveroutput on;
HR\XE> exec Proc2;
Here is a value returned by Proc1: 123
PL/SQL procedure successfully completed
Depending on your needs it might be more convenient to use functions to return a result of a procedural processing of data. Here is an example:
HR\XE> create or replace function F1 return number
2 is
3 l_ret_value number;
4 begin
5 l_ret_value := 123;
6 return l_ret_value;
7 end;
HR\XE> /
Function created.
HR\XE> create or replace procedure Proc3
2 is
3 l_val number;
4 begin
5 l_val := F1;
6 dbms_output.put_line('Value returned by the F1 function: ' ||
To_Char(l_val));
7 -- Or
8 dbms_output.put_line('Value returned by the F1 function: ' || To_Char(F1));
9 end;
HR\XE> /
Procedure created.
HR\XE> set serveroutput on;
HR\XE> exec proc3;
Value returned by the F1 function: 123
Value returned by the F1 function: 123
PL/SQL procedure successfully completed.
HR\XE>

A stored procedure does not return a value it takes IN, OUT or IN OUT parameters. So probably your have to call:
SPtobecalled(Param1,Param2, AssignSpResult );

Related

Printing a statement in stored procedure

I have written some code to print whether the condition is true/false, but I can't get the output as true / false. Why?
Here is that code:
create or replace PROCEDURE TET_STOP_DID
IS
result VARCHAR2(10);
LastGeneratedcode TET_LASTGENERATEDMASKCODE.Maskedcode%TYPE;
BEGIN
select maskedcode into LastGeneratedcode from TET_LASTGENERATEDMASKCODE;
IF (SUBSTR(LastGeneratedcode,5,5) !='ZZZZZ') then
result := 'true';
else
result :='false';
END IF;
END TET_STOP_DID;
You aren't printing anything; you've just set the result variable to some value, and that's it. Though, that procedure doesn't look right as it'll raise too_many_rows error if there are two (or more) rows in the table (or no_data_found if it is empty). I presume you'd want to pass some parameter which restricts number of fetched rows.
For example:
SQL> select * from tet_lastgeneratedmaskcode;
ID MASK
---------- ----
1 abcd
2 fff
Procedure which accepts a parameter and handles exceptions (that's rather poor handling, just to show what to pay attention to):
SQL> create or replace procedure tet_stop_did
2 (par_id in tet_lastgeneratedmaskcode.id%type)
3 is
4 result varchar2(10);
5 lastgeneratedcode tet_lastgeneratedmaskcode.maskedcode%type;
6 begin
7 select maskedcode
8 into lastgeneratedcode
9 from tet_lastgeneratedmaskcode
10 where id = par_id;
11
12 if substr(lastgeneratedcode, 5, 5) != 'ZZZZZ' then
13 result := 'true';
14 else
15 result := 'false';
16 end if;
17
18 dbms_output.put_line('Result = ' || result);
19 exception
20 when no_data_found then
21 dbms_output.put_line('No rows for that ID');
22 when too_many_rows then
23 dbms_output.put_line('Two or more rows for that ID');
24 end tet_stop_did;
25 /
Procedure created.
Testing:
SQL> set serveroutput on
SQL> exec tet_stop_did (1);
Result = false --> here's your result
PL/SQL procedure successfully completed.
SQL>
If you can use SERVEROUTPUT clause to run the procedure you may insert DBMS_OUTPUT in stored procedure code:
CREATE OR REPLACE PROCEDURE TET_STOP_DID
IS
result VARCHAR2(10);
LastGeneratedcode TET_LASTGENERATEDMASKCODE.Maskedcode%TYPE;
BEGIN
select maskedcode into LastGeneratedcode from TET_LASTGENERATEDMASKCODE;
IF (SUBSTR(LastGeneratedcode,5,5) !='ZZZZZ') then
result := 'true';
ELSE
result := 'false';
END IF;
-- Print the output
DBMS_OUTPUT.PUT_LINE(result);
END TET_STOP_DID;
And then from terminal, it runs so:
SQL> SET SERVEROUTPUT ON;
SQL> EXECUTE TET_STOP_DID
false
Thank you

PL/SQL Procedure SELECT Input- Error PLS-00306

Hello I'm a beginner at PL/SQL and some help would be appreciated.
So I have this procedure here and my goal is to have it so that when this procedure is executed that I can enter a 5 digit integer (a zipcode) and it will just select those values from the table and display just as if I've done a query like
SELECT * FROM customers WHERE customer_zipcode = "input zipcode".
create or replace PROCEDURE LIST_CUSTOMER_ZIPCODE(
p_zipcode IN customers.customer_zipcode%TYPE,
p_disp OUT SYS_REFCURSOR)
-- User input Variable, Display Variable
IS
BEGIN
OPEN p_disp for SELECT customer_first_name, customer_zipcode FROM customers
WHERE customer_zipcode=p_zipcode;
EXCEPTION
-- Input Sanitization
WHEN no_data_found THEN
dbms_output.put_line('-1');
END;
EXEC LIST_CUSTOMER_ZIPCODE(07080);
When I execute this command I just keep getting this error.
https://i.stack.imgur.com/nCI8T.png
If you are using SQL*Plus or SQL Developer then you can declare a bind variable and then call the procedure passing the variable and then print it:
SELECT * FROM customers WHERE customer_zipcode = "input zipcode".
create or replace PROCEDURE LIST_CUSTOMER_ZIPCODE(
p_zipcode IN customers.customer_zipcode%TYPE,
p_disp OUT SYS_REFCURSOR
)
IS
BEGIN
OPEN p_disp FOR
SELECT customer_first_name, customer_zipcode
FROM customers
WHERE customer_zipcode = p_zipcode;
EXCEPTION
-- Input Sanitization
WHEN no_data_found THEN
dbms_output.put_line('-1');
END;
/
VARIABLE cur SYS_REFCURSOR;
EXEC LIST_CUSTOMER_ZIPCODE('07080', :cur);
PRINT cur;
However, your exception handling block is never going to be called as the cursor can return zero rows without raising that exception so the procedure could be simplified to:
create or replace PROCEDURE LIST_CUSTOMER_ZIPCODE(
p_zipcode IN customers.customer_zipcode%TYPE,
p_disp OUT SYS_REFCURSOR
)
IS
BEGIN
OPEN p_disp FOR
SELECT customer_first_name, customer_zipcode
FROM customers
WHERE customer_zipcode = p_zipcode;
END;
/
You can't just execute such a procedure as it expects 2 parameters; one is IN, while another is OUT (ref cursor which contains result set).
I don't have your tables so I'll demonstrate it using Scott's sample schema by passing department number and returning list of employees working in that department.
Procedure:
SQL> set serveroutput on
SQL> create or replace procedure p_list
2 (p_deptno in dept.deptno%type,
3 p_disp out sys_refcursor
4 )
5 is
6 begin
7 open p_disp for select ename, job from emp
8 where deptno = p_deptno;
9 end;
10 /
Procedure created.
This is how you use it:
SQL> declare
2 l_list sys_refcursor;
3 l_ename emp.ename%type;
4 l_job emp.job%type;
5 begin
6 p_list(10, l_list); --> calling the procedure; use 2 parameters
7
8 loop
9 fetch l_list into l_ename, l_job;
10 exit when l_list%notfound;
11 dbms_output.put_line(l_ename ||' - '|| l_job);
12 end loop;
13 end;
14 /
CLARK - MANAGER
KING - PRESIDENT
MILLER - CLERK
PL/SQL procedure successfully completed.
SQL>
TRY EXEC LIST_CUSTOMER_ZIPCODE(:p_Zipcode);
If you put ':' before any string, It will become substitution string and you can type your input.

Oracle return the value of a variable

I am migrating Microsoft Sql Server Database to Oracle 19c, a query migrated is failing:
DECLARE cardnum varchar2(100);
BEGIN
MyProcedure('ES', cardnum );
SELECT cardnum;
END;
The error returned is:
PL/SQL: ORA-00923: FROM keyword not found where expected
I think that the error is in the line 4:
SELECT cardnum;
The procedure sets the value of the variable, therefore I need to return the value of the variable "cardnum".
What should i use instead of select clausule?
Note --> this query is used from net core with Dapper.
Thank you!
Well, you already have cardnum, procedure set it as its OUT parameter, you don't have to do anything about it.
SQL> create or replace procedure myprocedure(par_1 in varchar2, par_2 out varchar2) is
2 begin
3 par_2 := 'ABC';
4 end;
5 /
Procedure created.
SQL> declare
2 cardnum varchar2(100);
3 begin
4 myprocedure('ES', cardnum);
5
6 dbms_output.put_line('Cardnum = ' || cardnum);
7 end;
8 /
Cardnum = ABC
PL/SQL procedure successfully completed.
SQL>
If you want to select it, you can - but PL/SQL requires an INTO clause. For example:
SQL> declare
2 cardnum varchar2(100);
3 l_var varchar2(100);
4 begin
5 myprocedure('ES', cardnum);
6
7 select cardnum into l_var from dual;
8 end;
9 /
PL/SQL procedure successfully completed.
SQL>
It depends on what you actually want to do with cardnum ...

How to get a string with more than 4000 characters as input to a Store Procedure in PLSQL?

I have a store procedure in oracle which takes string input through a varchar2 type input parameter,I believe that 4000 bytes is the max length we can pass through the parameter, i need to have more characters in my input, what would be a viable solution?
One option is to use CLOB datatype.
Though, are you sure in what you are saying? It is 11g, and no problem in passing parameter longer than 4000 characters:
SQL> create or replace procedure p_test (par_str in varchar2)
2 is
3 l_len number;
4 begin
5 l_len := length(par_str);
6 dbms_output.put_line('length of l_len = ' || l_len);
7 end;
8 /
Procedure created.
SQL> set serveroutput on
SQL> declare
2 l_val varchar2(5000);
3 begin
4 l_val := lpad('x', 5000, 'x');
5 dbms_output.put_line('length of l_val = ' || length(l_val));
6
7 p_test(l_val);
8 end;
9 /
length of l_val = 5000
length of l_len = 5000
PL/SQL procedure successfully completed.
SQL>
In PL/SQL, the max length of the varchar2 is 32767.
You can pass the string of the mentioned length to the procedure.

Is it possible to take a stored procedure name as input parameter in another stored procedure and execute it?

I have a set of stored procedures with the same interface and one of these stored procedures will be passed to a runner stored procedure as an input parameter. How can I execute this stored procedure within the runner proc.
I tried this by using dynamic SQL. The code snippet I wrote for this:
v_proc_query := ':1(:2, :3)';
execute immediate v_proc_query using p_proc_name, p_param1, p_param2;
But the above statement give error: ORA-00900: invalid SQL statement
I'm using Oracle 12c.
What is the right approach to achieve the goal?
Something like this, perhaps?
SQL> set serveroutput on
SQL> create or replace procedure p_test (par_deptno in number)
2 is
3 l_cnt number;
4 begin
5 select count(*)
6 into l_cnt
7 from emp
8 where deptno = par_deptno;
9 dbms_output.put_line('count = ' || l_cnt);
10 end;
11 /
Procedure created.
SQL> create or replace procedure p_test_2 (par_proc_name in varchar2, par_deptno in number)
2 is
3 l_str varchar2(200);
4 begin
5 l_str := 'begin ' ||
6 dbms_assert.sql_object_name(par_proc_name) ||
7 '(' || par_deptno || ');' ||
8 'end;';
9 execute immediate l_str;
10 end;
11 /
Procedure created.
SQL> exec p_test_2('p_test', 10);
count = 3
PL/SQL procedure successfully completed.
SQL>

Resources