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
Related
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>
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>
We have a table in Oracle 11g DB which has millions of records. For a few months, we have noticed that duplicate records are getting inserted into the table. Is there any way to alert via email when the duplicate records get inserted into the table?
We are in the process of creating a unique index for the table, but it will take sometime.
Meanwhile, can an email-alert be created to notify us when duplicate records are getting inserted?
We have OEM installed to monitor this Oracle DB.
If selecting from table takes a long time, I guess you don't want to intercept duplicates as they happen because every insert/update would kill performance even more than it does now.
Therefore, if table isn't in use 24/7, perhaps you could schedule that check and either notify someone or - even better - take action immediately.
That would be a stored procedure, e.g.
create or replace procedure p_del_dup as
begin
delete from your_table a
where a.rowid > (select min(b.rowid)
from your_table b
where b.future_unique_column = a.future_unique_column
);
end;
Live example, based on Scott's sample schema. I'll create a table that contains EMP data, enter some duplicates and delete them.
SQL> create table test as select * from emp where deptno in (10, 20);
Table created.
SQL> create or replace procedure p_del_dup as
2 begin
3 delete from test a
4 where a.rowid > (select min(b.rowid)
5 from test b
6 where b.empno = a.empno --> column(s) which will enforce uniqueness
7 );
8 end;
9 /
Procedure created.
Create a database job; schedule it to run at 02:00 every night
SQL> declare
2 x number;
3 begin
4 dbms_job.submit
5 ( job => x
6 ,what => 'p_del_dup;'
7 ,next_date => to_date('07.04.2020 02:00:00','dd/mm/yyyy hh24:mi:ss')
8 ,interval => 'TRUNC (SYSDATE+1) + 2 / 24'
9 ,no_parse => false
10 );
11 dbms_output.put_line('Job Number is: ' || to_char(x));
12 commit;
13 end;
14 /
Job Number is: 104
PL/SQL procedure successfully completed.
OK, let's now insert some duplicates (all from deptno = 10):
SQL> insert into test select * from emp where deptno = 10;
3 rows created.
SQL> select * from test order by deptno, ename;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09.06.81 2450 10
7782 CLARK MANAGER 7839 09.06.81 2450 10
7839 KING PRESIDENT 17.11.81 5000 10
7839 KING PRESIDENT 17.11.81 5000 10
7934 MILLER CLERK 7782 23.01.82 1300 10
7934 MILLER CLERK 7782 23.01.82 1300 10
7876 ADAMS CLERK 7788 12.01.83 1100 20
7902 FORD ANALYST 7566 03.12.81 3000 20
7566 JONES MANAGER 7839 02.04.81 2975 20
7788 SCOTT ANALYST 7566 09.12.82 3000 20
7369 SMITH CLERK 7902 17.12.80 800 20
11 rows selected.
I won't wait until 02:00 so I'll run the job manually. Remember, its ID is 104?
SQL> exec dbms_job.run(104);
PL/SQL procedure successfully completed.
SQL> select * from test order by deptno, ename;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
7782 CLARK MANAGER 7839 09.06.81 2450 10
7839 KING PRESIDENT 17.11.81 5000 10
7934 MILLER CLERK 7782 23.01.82 1300 10
7876 ADAMS CLERK 7788 12.01.83 1100 20
7902 FORD ANALYST 7566 03.12.81 3000 20
7566 JONES MANAGER 7839 02.04.81 2975 20
7788 SCOTT ANALYST 7566 09.12.82 3000 20
7369 SMITH CLERK 7902 17.12.80 800 20
8 rows selected.
SQL>
OK, duplicates are silently deleted.
If you want, you can modify the procedure and - using UTL_MAIL package - send an e-mail message to someone. If number of rows deleted is enough, you'd send SQL%ROWCOUNT number. Or, send whatever you want.
I have a table called employees with the columns:
id
departament_id
salary
and I want to raise the salary by 10% for the employees working in the same department as the employee with id=115, how should I do that?
Here's one option:
Sample data first (based on Scott's EMP table):
SQL> create table test as select deptno, empno id, ename, sal from emp;
Table created.
SQL> select * From test where deptno = 10;
DEPTNO ID ENAME SAL
---------- ---------- ---------- ----------
10 7782 CLARK 2450
10 7839 KING 10000
10 7934 MILLER 1300
SQL>
Let's change salaries for all employees of department 10 - I'll pass Clark's ID as a parameter:
SQL> merge into test t
2 using (select a.deptno
3 from test a
4 where a.id = &par_id
5 ) x
6 on (x.deptno = t.deptno)
7 when matched then update set
8 t.sal = t.sal * 1.1;
Enter value for par_id: 7782
3 rows merged.
Result:
SQL> select * From test where deptno = 10;
DEPTNO ID ENAME SAL
---------- ---------- ---------- ----------
10 7782 CLARK 2695
10 7839 KING 11000
10 7934 MILLER 1430
SQL>
Now that you know how, apply it to your table.
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>