Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...