Quick Search:
CODE
Oracle PL/SQL Code Library
JOBS
Find Or Post Oracle Jobs
FORUM
Oracle Discussion & Chat
 Oracle PL/SQL: WITH with CONNECT BY Jump to:  
Category: >> Oracle PL/SQL >> WITH with CONNECT BY Bookmark and Share

<< 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;


Free
Oracle Magazine
Subscriptions
and Oracle White Papers


SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
 
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 91 users online    © 2009 psoug.org

PSOUG LOGIN
Username: 
Password: 
Forgot your password?