How to pass multiple parameters in oracle stored procedure - oracle

i am trying to execute like exec print_emp(1010,1111) but it will showing error.
create or replace
procedure print_emp(
P_empno NUMBER
)
IS
begin
for c in ( SELECT *
from emp
where empno in p_empno)
loop
dbms_output.put_line( c.empno||' '||c.ename||' '||c.job||' '||c.sal);
end loop;
END;

You'll need to create a type that is a collection of numbers, and then a procedure that accepts that collection.
Rather than use the IN operator, you should use MEMBER OF to test whether a scalar value is in a collection.
create or replace type tab_number is table of number
/
create or replace procedure print_nums
(p_nums in tab_number)
is
cursor c_main is
select column_value
from table(p_nums)
order by 1;
begin
for r_main in c_main loop
dbms_output.put_line(r_main.column_value);
end loop;
--
if 33 member of p_nums then
dbms_output.put_line('In the list');
end if;
end;
/
exec print_nums(tab_number(10,20,50));
exec print_nums(tab_number(10,20,33));

Obviously, if all parameters you'd like to pass represent the same column value, you can't just list them as if they were two different parameters. If that was the case, procedure should actually name them all, e.g.
create procedure print_emp(par_emp_1 in number, par_emp_2 in number)
but - what if there are 3 or 4 EMPNOs you'd like to pass? You can't modify the procedure every time (not just while declaring it, but also in SELECT statements, as you'd have to add new parameters there as well).
Therefore, one option might be this (as far as I understood the question):
one parameter, whose datatype is varchar2
it means that you'd have to enclose list of EMPNOs into single quotes and separate them with the same separator every time; let it be a comma , sign
cursor's query would contain a subquery (lines #6 - 9) which splits that comma-separated values list of EMPNO values into rows
the rest is simple
Here you go:
SQL> create or replace procedure print_emp(par_empno in varchar2)
2 is
3 begin
4 for c in (select *
5 from emp
6 where empno in (select to_number(regexp_substr(par_empno, '[^,]+', 1, level))
7 from dual
8 connect by level <= regexp_count(par_empno, ',') + 1
9 )
10 )
11 loop
12 dbms_output.put_line(c.empno||' '||c.ename||' '||c.job||' '||c.sal);
13 end loop;
14 end;
15 /
Procedure created.
Scott's EMP sample table:
SQL> select * from emp where deptno = 20;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- ------------------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17.12.1980 00:00:00 1000 20
7566 JONES MANAGER 7839 02.04.1981 00:00:00 2975 20
7788 SCOTT ANALYST 7566 09.12.1982 00:00:00 3000 20
7876 ADAMS CLERK 7788 12.01.1983 00:00:00 1100 20
7902 FORD ANALYST 7566 03.12.1981 00:00:00 3000 20
Testing:
SQL> set serveroutput on
SQL> exec print_emp('7369,7566,7788');
7566 JONES MANAGER 2975
7788 SCOTT ANALYST 3000
7369 SMITH CLERK 1000
PL/SQL procedure successfully completed.
SQL>

Related

Write a function to find out the hire date of an employee who has joined just prior to the given date passed as input parameter in plsql

how to use ampersand in this program
create or replace function p_hire_date return date is
&v_hire_date employees.hire_date%type;
begin
select hire_date into v_hire_date
from employees
where hire_date < v_hire_date;
return v_hire_date;
end;
error PLS-00103:
Why would you use a substitution variable? This is a function, pass parameter to it and use it in its code.
Example is based on Scott's sample schema; adjust it to your own.
SQL> CREATE OR REPLACE FUNCTION f_hire_date (par_date IN DATE)
2 RETURN DATE
3 IS
4 retval DATE;
5 BEGIN
6 SELECT MAX (a.hiredate)
7 INTO retval
8 FROM emp a
9 WHERE a.hiredate < par_date;
10
11 RETURN retval;
12 END;
13 /
Function created.
SQL>
(You don't have to do that; it's just to know what dates represent):
SQL> ALTER SESSION SET NLS_DATE_FORMAT = 'dd.mm.yyyy';
Session altered.
Sample data:
SQL> SELECT ename, hiredate
2 FROM emp
3 ORDER BY hiredate;
ENAME HIREDATE
---------- ----------
SMITH 17.12.1980
ALLEN 20.02.1981
WARD 22.02.1981
JONES 02.04.1981
BLAKE 01.05.1981
CLARK 09.06.1981
TURNER 08.09.1981
MARTIN 28.09.1981 --> If I pass 30.09.1981, I'll get this date
KING 17.11.1981
JAMES 03.12.1981
FORD 03.12.1981
MILLER 23.01.1982
SCOTT 09.12.1982
ADAMS 12.01.1983
14 rows selected.
Let's try it:
SQL> SELECT f_hire_date (DATE '1981-09-30') FROM DUAL;
F_HIRE_DAT
----------
28.09.1981
SQL>

Simple Stored Procedure for learning

My journey is progressing and I'm learning rapidly. I can't get enough of this stuff... alas I am at a dead end here and need some help.
I am running Visual Studio, am connected to a database (that's filled with dummy data). I am able to run queries on it as expected. However, I'm learning about Procedures right now and I'm coming up with a problem.
I am trying to simply run a query that will select all from table.
CREATE PROCEDURE nearthetop()
BEGIN
SELECT * FROM RESULTS WHERE VOLUME = (SELECT MAX(VOLUME) FROM RESULTS WHERE VOLUME NOT In (SELECT Max(VOLUME) from RESULTS))
END;
When I run this inside Visual Studio I get an error:
EXECUTE FAIL:
CREATE PROCEDURE twofromtop() BEGIN SELECT * FROM RESULTS WHERE VOLUME = (SELECT MAX(VOLUME) FROM RESULTS WHERE VOLUME NOT In (SELECT Max(VOLUME) from RESULTS)) END
Message :
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END' at line 3
When I remove BEGIN and END; from the procedure it creates it fine.
Why is that?
And then, once created, how do I "call" that procedure so as I can see the returned results?
In Oracle, when PL/SQL code uses a SELECT statement, you have to select into something - a local variable, a collection, whatever - or use a cursor loop and deal with cursor variable.
You're selecting row(s) whose volume value is the 2nd largest. Code you wrote works, but it fetches from the same table 3 times which isn't optimal. For example (based on Scott's sample schema), fetching the 2nd largest salary:
SQL> select ename, sal from emp order by sal desc;
ENAME SAL
---------- ----------
KING 5000 --> largest
FORD 3000 --> Ford and Scott both share
SCOTT 3000 --> the 2nd largest salary
JONES 2975
BLAKE 2850
CLARK 2450
<snip>
Your query returns correct result:
SQL> select *
2 from emp
3 where sal = (select max(sal) from emp
4 where sal not in (select max(sal) from emp)
5 );
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 09.12.82 3000 20
7902 FORD ANALYST 7566 03.12.81 3000 20
Consider doing it differently:
SQL> select *
2 from (select e.*,
3 rank() over (order by sal desc) rnk
4 from emp e
5 )
6 where rnk = 2;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RNK
---------- ---------- --------- ---------- -------- ---------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 09.12.82 3000 20 2
7902 FORD ANALYST 7566 03.12.81 3000 20 2
SQL>
Now, back to your procedure. A simple option which doesn't require much effort is to use a cursor FOR loop. Procedure's IN parameter says which largest salary you want:
SQL> create or replace procedure nearthetop (par_n in number) as
2 begin
3 for cur_r in (select *
4 from (select e.*,
5 rank() over (order by sal desc) rnk
6 from emp e
7 )
8 where rnk = par_n
9 )
10 loop
11 dbms_output.put_line(cur_r.ename ||': '|| cur_r.sal);
12 end loop;
13 end;
14 /
Procedure created.
Testing:
SQL> set serveroutput on
SQL> begin
2 nearthetop(2); --> give me the 2nd largest salary
3 end;
4 /
SCOTT: 3000
FORD: 3000
PL/SQL procedure successfully completed.
SQL> begin
2 nearthetop(5); --> give me the 5th largest salary
3 end;
4 /
BLAKE: 2850
PL/SQL procedure successfully completed.
SQL>
I'm just displaying those values on the screen; you never said what you'd want to do with them.
If you'd like to return the result to the caller, a better option is to use a function instead of a procedure. In this case, it would be a ref cursor it returns.
SQL> create or replace function f_nearthetop (par_n in number)
2 return sys_refcursor
3 as
4 rc sys_refcursor;
5 begin
6 open rc for select *
7 from (select e.*,
8 rank() over (order by sal desc) rnk
9 from emp e
10 )
11 where rnk = par_n;
12 return rc;
13 end;
14 /
Function created.
Testing:
SQL> var l_rc refcursor
SQL>
SQL> exec :l_rc := f_nearthetop(2);
PL/SQL procedure successfully completed.
SQL> print :l_rc
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO RNK
---------- ---------- --------- ---------- -------- ---------- ---------- ---------- ----------
7788 SCOTT ANALYST 7566 09.12.82 3000 20 2
7902 FORD ANALYST 7566 03.12.81 3000 20 2
SQL>
So, yes - there are various options. Which one you'll actually use depends on what you want to do.
(As of Visual Studio: I can't help about it, I don't use it.)

PL/SQL Procedure/function with params to return a table from(of) a select query

how can a (Oracle)function/Procedure with parameters can be created to return a table, which was returned by a select statement, inside a block
tried with refcursor
getting data as below ,all in a single row
{<G_ID=748,P_G_ID=746,P_NO=null,M_DATE=06-OCT-20 >,
<G_ID=749,P_G_ID=746,P_NO=null,M_DATE=06-OCT-20>} instead of a table format
image link for the sample output
P.S. I am pretty new to PL/SQL
One option is to return refcursor. Here's an example:
SQL> create or replace function f_test (par_deptno in number)
2 return sys_refcursor
3 is
4 rc sys_refcursor;
5 begin
6 open rc for select * from emp where deptno = par_deptno;
7 return rc;
8 end;
9 /
Function created.
SQL> select f_test(10) from dual;
F_TEST(10)
--------------------
CURSOR STATEMENT : 1
CURSOR STATEMENT : 1
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09.06.81 2450 10
7839 KING PRESIDENT 17.11.81 10000 10
7934 MILLER CLERK 7782 23.01.82 1300 10
SQL>

Insert into table collection type in table function without using explicit cursor in PL/SQL

I write the following code in PL/SQL and it works:
declare
type deliveryStat_o IS record (
warehouseName varchar2(20), shipMode char(30), thirty_days number, sixty_days number,
ninety_days number, oneTwenty_days number, veryLate number
);
type deliveryStat_t is TABLE OF deliveryStat_o;
statTable deliveryStat_t;
begin
SELECT w_warehouse_name, sm_type, 1 AS thirty_days, 1 AS sixty_days, 1 AS ninety_days,
1 AS oneTwenty_days, 1 AS veryLateDelivery
bulk collect into statTable
FROM catalog_sales, warehouse, ship_mode, date_dim
WHERE cs_ship_date_sk = d_date_sk
AND cs_warehouse_sk = w_warehouse_sk
AND cs_ship_mode_sk = sm_ship_mode_sk
GROUP BY w_warehouse_name,
sm_type ;
end;
How can I do this inside a table function that returns the nested collection statTable. I understand that this can probably be accomplished using explicit cursors; however, is it possible to do it without using a cursor?
For context, I'm starting with this as the base
SQL> set serverout on
SQL> declare
2 type deliveryStat_o IS record (
3 empno number, ename varchar2(20)
4 );
5 type deliveryStat_t is TABLE OF deliveryStat_o;
6
7 statTable deliveryStat_t;
8
9 begin
10
11 SELECT empno, ename
12 bulk collect into statTable
13 FROM emp;
14 dbms_output.put_line('recs='||statTable.count);
15 end;
16 /
recs=14
PL/SQL procedure successfully completed.
To convert that to allow a table function, then those types need to be SQL types, hence
SQL> create or replace
2 type deliveryStat_o as object (
3 empno number, ename varchar2(20)
4 );
5 /
Type created.
SQL> create or replace
2 type deliveryStat_t as table of deliveryStat_o
3 /
Type created.
Now that this is done, the query must return a table of objects, so
SQL> set serverout on
SQL> declare
2 statTable deliveryStat_t;
3 begin
4
5 SELECT deliveryStat_o(empno, ename)
6 bulk collect into statTable
7 FROM emp;
8 dbms_output.put_line('recs='||statTable.count);
9 end;
10 /
recs=14
PL/SQL procedure successfully completed.
which can now be easily converted to a table function
SQL> create or replace
2 function my_func return deliveryStat_t is
3 statTable deliveryStat_t;
4 begin
5
6 SELECT deliveryStat_o(empno, ename)
7 bulk collect into statTable
8 FROM emp;
9 return statTable;
10 end;
11 /
Function created.
SQL> select * from my_func();
EMPNO ENAME
---------- --------------------
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
14 rows selected.
If you're returning a LOT of rows, then consider a pipelined function instead to avoid the memory overhead of collecting all the rows into the nested table

Oracle PL/SQL collect values from a loop into a cursor

I have a PL/SQL TABLE TYPE result set that contains document ids.
I can loop over the result set without a problem, but the issue is that I have to return a sys_refcursor from the function, but I am unable to collect the values from the loop into the cursor.
TYPE table_typ IS TABLE OF DOCUMENT_QUEUE.ENV_ID%TYPE INDEX BY PLS_INTEGER;
FUNCTION GET_DOCS()
RETURN SYS_REFCURSOR
IS
LS_CUR SYS_REFCURSOR;
LR_UPDATED_ROWS table_typ;
BEGIN
UPDATE DOCUMENT_QUEUE DQ
...
RETURNING DQ.ENV_ID BULK COLLECT INTO LR_UPDATED_ROWS;
-- Need to collect all of the following rows into the cursor
FOR indx IN NVL (LR_UPDATED_ROWS.FIRST, 0) .. NVL (LR_UPDATED_ROWS.LAST, -1)
LOOP
SELECT * FROM DOCUMENT_QUEUE DQ WHERE DQ.ENV_ID = LR_UPDATED_ROWS(indx);
END LOOP;
RETURN LS_CUR;
END GET_DOCS;
All help and hints are welcome.
FOR indx IN NVL (LR_UPDATED_ROWS.FIRST, 0) .. NVL (LR_UPDATED_ROWS.LAST, -1)
LOOP
SELECT * FROM DOCUMENT_QUEUE DQ WHERE DQ.ENV_ID = LR_UPDATED_ROWS(indx);
END LOOP;
RETURN LS_CUR;
You do not need the cursor FOR LOOP. You could use OPEN CURSOR FOR statement and return a SYS_REFCURSOR.
For example,
OPEN LS_CUR FOR SELECT * FROM DOCUMENT_QUEUE DQ
WHERE DQ.ENV_ID IN (SELECT * FROM TABLE(LR_UPDATED_ROWS));
RETURN LS_CUR;
or,
OPEN LS_CUR FOR SELECT * FROM DOCUMENT_QUEUE DQ
WHERE DQ.ENV_ID MEMBER OF LR_UPDATED_ROWS;
RETURN LS_CUR;
However, in order to do that, you must CREATE the type at SQL level not at PL/SQL level. Else, you would receive PLS-00642: local collection types not allowed in SQL statements.
A small demo:
Create the type at SQL level:
SQL> CREATE OR REPLACE TYPE table_typ AS TABLE OF NUMBER
2 /
Type created.
Let's get the output in SQL*Plus using a refcursor:
Using MEMBER OF syntax:
SQL> variable r refcursor
SQL> DECLARE
2 l_typ table_typ;
3 TYPE numbers IS TABLE OF NUMBER;
4 n numbers;
5 BEGIN
6 SELECT empno BULK COLLECT INTO l_typ FROM emp;
7 OPEN :r FOR SELECT empno,
8 ename FROM emp WHERE empno member OF l_typ;
9 END;
10 /
PL/SQL procedure successfully completed.
SQL> print r
EMPNO ENAME
---------- ----------
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
14 rows selected.
Using TABLE function:
SQL> variable r refcursor
SQL> DECLARE
2 l_typ table_typ;
3 TYPE numbers IS TABLE OF NUMBER;
4 n numbers;
5 BEGIN
6 SELECT empno BULK COLLECT INTO l_typ FROM emp;
7 OPEN :r FOR SELECT empno,
8 ename FROM emp WHERE empno IN (SELECT * from TABLE(l_typ));
9 END;
10 /
PL/SQL procedure successfully completed.
SQL> print r
EMPNO ENAME
---------- ----------
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
14 rows selected.
For the mentioned requirement i have mentioned below a snippet which will help to fetch all the rows into ref cursor for every rowid. Let me know if this helps.
CREATE OR REPLACE TYPE table_typ
IS
TABLE OF DOCUMENT_QUEUE.ENV_ID%TYPE INDEX BY PLS_INTEGER;
CREATE OR REPLACE FUNCTION GET_DOCS
RETURN SYS_REFCURSOR
IS
LS_CUR SYS_REFCURSOR;
LR_UPDATED_ROWS table_typ;
lv_rows_lst VARCHAR2(32676);
BEGIN
SELECT <COL1> BULK COLLECT INTO LR_UPDATED_ROWS FROM <TABLE_NAME>;
FOR I IN LR_UPDATED_ROWS.FIRST..LR_UPDATED_ROWS.LAST
LOOP
lv_rows_lst:=lv_rows_lst||','||LR_UPDATED_ROWS(I);
END LOOP;
lv_rows_lst:=SUBSTR(lv_rows_lst,2,LENGTH(lv_rows_lst));
OPEN LS_CUR FOR 'SELECT * FROM DOCUMENT_QUEUE DQ WHERE DQ.ENV_ID IN ('||lv_rows_lst||')';
RETURN LS_CUR;
END GET_DOCS;

Resources