Oracle使用with子句创建表

时间:2011-03-25 06:49:03

标签: sql oracle common-table-expression

我可以根据使用with子句形成的查询创建表吗?

3 个答案:

答案 0 :(得分:48)

当然:

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

答案 1 :(得分:1)

CREATE TABLE table_name AS语句基于select语句创建表。 with子句的解决方案是:

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

   UNION ALL 

   SELECT 2 
   FROM dual
) 
);

答案 2 :(得分:0)

对于多个CTE(公用表表达式;即多个WITH子句),我发现相同的语法有效。即


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

select * from table2

将从table_name语句的schema中创建select