How to make a PL/SQL stored procedure display a table? - oracle

Lets say I have a table called student that has 3 attributes, the student number, name and the favorite color and when i call the procedure like this:
execute favColor('RED');
the output should be:
NAME FAV_COLOR
-------- -----------
MARY RED
JANE RED
PETER RED
JOHN RED
Can I have an example code to do this? So far I only see procedures inserting or updating records and if there is a select statement in the procedure, it must have an into clause, does that mean it is impossible for procedures to display this output?

As I don't have your table, I'll use Scott's with jobs instead of colors.
Sample data:
SQL> select ename, job from emp order by job, ename;
ENAME JOB
---------- ---------
FORD ANALYST
SCOTT ANALYST
ADAMS CLERK
JAMES CLERK
MILLER CLERK
SMITH CLERK
BLAKE MANAGER
CLARK MANAGER
JONES MANAGER
KING PRESIDENT
ALLEN SALESMAN
MARTIN SALESMAN
TURNER SALESMAN
WARD SALESMAN
14 rows selected.
Procedure will return employees who have certain job:
SQL> create or replace procedure favJob (par_job in varchar2, par_rc out sys_refcursor)
2 is
3 begin
4 open par_rc for
5 select ename, job
6 from emp
7 where job = par_job
8 order by ename;
9 end;
10 /
Procedure created.
Testing:
SQL> var rc refcursor
SQL> exec favJob('MANAGER', :rc);
PL/SQL procedure successfully completed.
SQL> print rc
ENAME JOB
---------- ---------
BLAKE MANAGER
CLARK MANAGER
JONES MANAGER
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>

Use wildcard to do fuzzy-search, with a variable

I am new to Oracle programming (started coding a month ago).
I am doing a fuzzy-search, as follows:
WHERE SQL_text like '%VARIABLE%'
The problem is, VARIABLE is a cursor that iterates through a table that looks like this:
USA
UK
Japan
...
Could you please advise how to specify a variable in that WHERE clause?
I've tried the following, but it doesn't work:
WHERE SQL_text like '%'||VARIABLE||'%'
Thank you very much! I greatly appreciate everyone's input!
It works, if you use it correctly. I don't have your tables, but - I have Scott's emp so I'll search for jobs that contain certain substring.
Table contents:
SQL> select ename, job from emp order by job;
ENAME JOB
---------- ---------
SCOTT ANALYST --> 2 analysts
FORD ANALYST
MILLER CLERK --> 4 clerks
JAMES CLERK
SMITH CLERK
ADAMS CLERK
BLAKE MANAGER
JONES MANAGER
CLARK MANAGER
KING PRESIDENT
TURNER SALESMAN
MARTIN SALESMAN
WARD SALESMAN
ALLEN SALESMAN
14 rows selected.
Code you're looking for:
SQL> set serveroutput on
SQL> declare
2 l_cnt number;
3 begin
4 for cur_r in (select 'ERK' var from dual union all --> clerks
5 select 'NALY' from dual --> analysts
6 )
7 loop
8 select count(*)
9 into l_cnt
10 from emp
11 where job like '%' || cur_r.var || '%';
12 dbms_output.put_line(cur_r.var || ' is contained in ' || l_cnt || ' row(s)');
13 end loop;
14 end;
15 /
ERK is contained in 4 row(s)
NALY is contained in 2 row(s)
PL/SQL procedure successfully completed.
SQL>

How can I output the result of a PL/SQL script to a grid?

I'm trying to figure out how I can output the results of a PL/SQL script to a results grid the same way results from a simple query are output. I'm new to Oracle from SQL Server, so I might be overlooking something very basic. For example how would I view the results of something like the following simple T-SQL (SQL Server's PL) script? Is there some sort of print_to_grid function? Or perhaps using a cursor? I know about dbms_output, but I definitely don't want to have to concatenate a string out of every script I want to see results from.
declare #emps table(employeeId int, name varchar(40)
insert into #emps
select employeeID, name
from employees
select * from #emps
This seems like it should be the simplest thing in the world, but I can't find a straight answer for it. Any help or tool recommendations would be greatly appreciated.
It sounds like you are after a ref cursor. For example
SQL> variable r refcursor
SQL>
SQL> begin
2 open :r for
3 select empno, ename
4 from scott.emp;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
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.
At the point you open that refcursor, the result is pre-ordained - so there is no need to worry about read locks or data consistency etc. For example, I'll open the ref cursor when there is rows in the table, and then delete them...my printed data is still as it was at cursor open time.
SQL>
SQL>
SQL>
SQL> begin
2 open :r for
3 select empno, ename
4 from scott.emp;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
SQL> delete from scott.emp;
14 rows deleted.
SQL>
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.
SQL>
SQL>
SQL> rollback;
Rollback complete.
SQL>

How to display sys_refcursor output pl sql with Toad tool

I have written a query to execute the SP. The execution works fine with this. However, I'm not able to see output results.
declare v_rc sys_refcursor;
begin
SUSER.TRANS_REP (v_rc ,'Investments Series','31-12-2012','Dealer Group','All Adv') ;
end;
How to display output result with sys_refcursor. Please help.
note: I tried to print cursor but did not get any help. Also refered this (How to display a sys_refcursor data in TOAD's DataGrid and https://community.oracle.com/thread/627571), but still no help.
In SQL*Plus you could easily do it using a refcursor variable.
SQL> var r refcursor
SQL>
SQL> BEGIN
2 OPEN :r FOR SELECT empno,ename FROM emp;
3 END;
4 /
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
EMPNO ENAME
---------- ----------
7900 JAMES
7902 FORD
7934 MILLER
14 rows selected.
SQL>
I guess in TOAD, you have some sort of output options. Select the variables you want to see the values in the output, the ref cursor result set would open in a different window.

What is the equivalent of SQL Server APPLY in Oracle?

I am new to Oracle. Is there a builtin keyword does the same job of SQL Server APPLY?
I think the equivalent of the APPLY clause in Oracle is called a lateral JOIN. A lateral join in Oracle is when you join a table A with a function F that outputs rows and this function has columns of A as parameters.
Let's build a small example with this setup:
SQL> CREATE OR REPLACE TYPE emp_row AS OBJECT (
2 empno NUMBER(4),
3 ename VARCHAR(10),
4 job VARCHAR(9),
5 deptno NUMBER(2)
6 );
7 /
Type created
SQL> CREATE OR REPLACE TYPE emp_tab AS TABLE OF emp_row;
2 /
Type created
SQL> CREATE OR REPLACE FUNCTION get_emp_dept(p_deptno NUMBER) RETURN emp_tab IS
2 l_result emp_tab;
3 BEGIN
4 SELECT emp_row(empno, ename, job, deptno)
5 BULK COLLECT INTO l_result
6 FROM emp
7 WHERE deptno = p_deptno;
8 RETURN l_result;
9 END get_emp_dept;
10 /
Function created
A lateral join is automatic in Oracle, there is no special keyword:
SQL> SELECT dept.dname, emp.empno, emp.ename, emp.job
2 FROM dept
3 CROSS JOIN TABLE(get_emp_dept(dept.deptno)) emp;
DNAME EMPNO ENAME JOB
-------------- ----- ---------- ---------
ACCOUNTING 7782 CLARK MANAGER
ACCOUNTING 7839 KING PRESIDENT
ACCOUNTING 7934 MILLER CLERK
RESEARCH 7369 SMITH CLERK
RESEARCH 7566 JONES MANAGER
RESEARCH 7788 SCOTT ANALYST
RESEARCH 7876 ADAMS CLERK
RESEARCH 7902 FORD ANALYST
SALES 7499 ALLEN SALESMAN
SALES 7521 WARD SALESMAN
SALES 7654 MARTIN SALESMAN
SALES 7698 BLAKE MANAGER
SALES 7844 TURNER SALESMAN
SALES 7900 JAMES CLERK
14 rows selected
In Oracle we can use a pipelined function in the FROM clause by using the TABLE() function.
SQL> select * from table( get_dept_emps (10) )
2 /
ENAME SAL MGR
------------------------------ ---------- ---------------------
BOEHMER 2450 SCHNEIDER
SCHNEIDER 5000
KISHORE 1300 BOEHMER
SQL>
This can be treated like any other table, for instance, by joining to it:
SQL> select t.*
2 , e.empno
3 from
4 table( get_dept_emps (10) ) t
5 join emp e
6 on e.ename = t.ename
7 /
ENAME SAL MGR EMPNO
---------- ---------- ---------- ----------
BOEHMER 2450 SCHNEIDER 7782
SCHNEIDER 5000 7839
KISHORE 1300 BOEHMER 7934
SQL>
Since 12c, Oracle supports both APPLY and LATERAL natively: https://docs.oracle.com/database/121/NEWFT/chapter12101.htm#FEATURENO10330

Resources