Quick Search:
 
 Oracle PL/SQL: WITH with CONNECT BY Jump to:  
Category: >> Oracle PL/SQL >> WITH with CONNECT BY  

<< lastnext >>

Snippet Name: WITH with CONNECT BY

Description: The WITH query_name clause provides a way assign a name to a subquery block. The subquery block can then be referenced in multiple places in the query by specifying the query name. The query is optimized by treating it as either an inline view or as a temporary table.

COMMENT(S): Original code by Michel Cadot at comp.databases.oracle.misc

Note that you cannot nest this clause. That is, you cannot specify the subquery_factoring_clause within the subquery of another subquery_factoring_clause.

However, a query_name defined in one subquery_factoring_clause can be used in the subquery of any subsequent subquery_factoring_clause.

In a query with set operators, the set operator subquery cannot contain the subquery_factoring_clause, but the FROM subquery can contain the subquery_factoring_clause.

Also see:
» AND Condition
» Distinct
» Having Clause
» EXISTS 2
» EXISTS
» WITH Clause: Single alias
» WITH Clause: Double alias

Comment: (none)

Language: PL/SQL
Highlight Mode: PLSQL
Last Modified: February 26th, 2009

CREATE TABLE table_1 (
p_name VARCHAR2(10),
cases NUMBER(3));
 
INSERT INTO table_1 VALUES ('James',2);
INSERT INTO table_1 VALUES ('Mary',3);
INSERT INTO table_1 VALUES ('Steve',2);
INSERT INTO table_1 VALUES ('Janice',1);
COMMIT;
 
SELECT * FROM table_1 ;
 
CREATE TABLE table_2 AS
SELECT p_name FROM table_1 
WHERE 1=2;
 
SELECT * FROM table_2;
 
INSERT INTO table_2
WITH rn AS (
     SELECT ROWNUM rn
     FROM dual
     CONNECT BY LEVEL <= (SELECT MAX(cases) FROM table_1))
SELECT p_name
FROM table_1, rn
WHERE rn <= cases
ORDER BY p_name;
 
SELECT * FROM table_2;


 
   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org