Oracle with子句
以例子学习with: 1.with 2.--查询部门和部门的总薪水 3. dept_costs as ( 4. select d.department_name,sum(e.salary) dept_total 5. from departments d,employees e 6. where d.department_id=e.department_id 7. group by d.department_name 8. ), 9.--利
以例子学习with:1.with
2.--查询部门和部门的总薪水
3. dept_costs as (
4. select d.department_name,sum(e.salary) dept_total
5. from departments d,employees e
6. where d.department_id=e.department_id
7. group by d.department_name
8. ),
9.--利用上一个with查询的结果,计算部门的平均总薪水
10. avg_costs as (
11. select avg(dept_total) dept_avg
12. from dept_costs
13. )
14.--从两个with查询中比较并且输出查询结果
15. select *
16. from dept_costs
17. where dept_total > (select dept_avg from avg_costs)
18. order by department_name
注释:
① 子查询可重用相同或者前一个with查询块,通过select调用(with子句也只能被select调用)
② with子句的查询输出存储到用户临时表空间,一次查询,到处使用
③ 同级select前有多个查询定义,第一个用with,后面的不用with,并且用逗号分割
④ 最后一个with查询块与下面的select调用之间不能用逗号分割,只通过右括号分离,with子句的查询必须括号括起
⑤ 如果定义了with子句,而在查询中不使用,则会报ora-32035错误,只要后面有引用的即可,不一定在select调用,在后with查询块引用也是可以的
⑥ 前面的with子句定义的查询在后面的with子句中可以使用,但是一个with子句内部不能嵌套with子句
⑦ with查询的结果列有别名,引用时候必须使用别名或者*
再来看with的语法
with子句的优点
① with子句有可能会改变执行计划
② with子查询只执行一次,将结果存储在用户的临时表空间,可多次引用,增加性能
③ sql的可读性较强
案例:
-
Ⅰ一般使用方式
1.with
2.--查询销售部门员工的姓名
3. saler_name as (
4. select department_id from departments where department_name='SALES' order by department_id
5. )
6.select last_name,first_name
7. from employees e
8. where department_id in (select * from saler_name) 注释:使用with子句,可以在复杂的查询中预先定义好一个结果集,然后在查询中反复使用,不使用会报错。而且with子句获得的是一个临时表,必须采用select from (with查询名)
Ⅱ 在多数子查询中引用,同级可见
1.select last_name
2. from (with
3.--查询销售部门员工的姓名
4. saler_name as (
5. select department_id from departments where department_name='SALES' order by department_id
6. )
7.select last_name,first_name
8. from employees e
9. where department_id in (select * from

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



To query the Oracle tablespace size, follow the following steps: Determine the tablespace name by running the query: SELECT tablespace_name FROM dba_tablespaces; Query the tablespace size by running the query: SELECT sum(bytes) AS total_size, sum(bytes_free) AS available_space, sum(bytes) - sum(bytes_free) AS used_space FROM dba_data_files WHERE tablespace_

Uninstall method for Oracle installation failure: Close Oracle service, delete Oracle program files and registry keys, uninstall Oracle environment variables, and restart the computer. If the uninstall fails, you can uninstall manually using the Oracle Universal Uninstall Tool.

Oracle View Encryption allows you to encrypt data in the view, thereby enhancing the security of sensitive information. The steps include: 1) creating the master encryption key (MEk); 2) creating an encrypted view, specifying the view and MEk to be encrypted; 3) authorizing users to access the encrypted view. How encrypted views work: When a user querys for an encrypted view, Oracle uses MEk to decrypt data, ensuring that only authorized users can access readable data.

There are three ways to view instance names in Oracle: use the "sqlplus" and "select instance_name from v$instance;" commands on the command line. Use the "show instance_name;" command in SQL*Plus. Check environment variables (ORACLE_SID on Linux) through the operating system's Task Manager, Oracle Enterprise Manager, or through the operating system.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

Deleting all data in Oracle requires the following steps: 1. Establish a connection; 2. Disable foreign key constraints; 3. Delete table data; 4. Submit transactions; 5. Enable foreign key constraints (optional). Be sure to back up the database before execution to prevent data loss.

Oracle Invalid numeric errors may be caused by data type mismatch, numeric overflow, data conversion errors, or data corruption. Troubleshooting steps include checking data types, detecting digital overflows, checking data conversions, checking data corruption, and exploring other possible solutions such as configuring the NLS_NUMERIC_CHARACTERS parameter and enabling data verification logging.

SQL statements can be created and executed based on runtime input by using Oracle's dynamic SQL. The steps include: preparing an empty string variable to store dynamically generated SQL statements. Use the EXECUTE IMMEDIATE or PREPARE statement to compile and execute dynamic SQL statements. Use bind variable to pass user input or other dynamic values to dynamic SQL. Use EXECUTE IMMEDIATE or EXECUTE to execute dynamic SQL statements.
