开发者

Oracle create table using with clause

开发者 https://www.devze.com 2023-02-19 11:31 出处:网络
Can I cre开发者_如何学编程ate a table from a query formed using with clause?Sure: CREATE TABLE t

Can I cre开发者_如何学编程ate a table from a query formed using with clause?


Sure:

CREATE TABLE t
AS 
WITH some_data AS ( 
   SELECT 1 as some_value 
   FROM dual

   UNION ALL 

   SELECT 2 
   FROM dual
) 
SELECT * 
FROM some_data


The CREATE TABLE table_name AS statement creates a table based on a select statement. The solution for a with clause will be :

CREATE TABLE t
AS 
SELECT * FROM (
WITH some_data AS ( 
   SELECT 1 as some_value 
   FROM dual

   UNION ALL 

   SELECT 2 
   FROM dual
) 
);


For multiple CTE (common table expressions; i.e. multiple WITH clause), I found the same syntax worked. i.e.


CREATE TABLE schema.table_name as 
WITH table1 as (SELECT 1),
table2 as (SELECT 2)

select * from table2

will create the table_name in the schema from the select statement

0

精彩评论

暂无评论...
验证码 换一张
取 消