Oracle中的SQL Server的APPLY等价于什么?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (22)

我是Oracle的新手。有没有命令做SQL Server的APPLY相同效果的东西?

提问于
用户回答回答于

一个小示例:

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

在Oracle中:

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
用户回答回答于

在Oracle中,我们可以在FROM子句使用TABLE()功能。

SQL> select * from table( get_dept_emps (10) )
  2  /

ENAME                                 SAL MGR
------------------------------ ---------- ---------------------
BOEHMER                              2450 SCHNEIDER
SCHNEIDER                            5000
KISHORE                              1300 BOEHMER

SQL>

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>

扫码关注云+社区