更新时间:2024-03-06 GMT+08:00

SELECT INTO

功能描述

SELECT INTO用于根据查询结果创建一个新表,并且将查询到的数据插入到新表中。

数据并不返回给客户端,这一点和普通的SELECT不同。新表的字段具有和SELECT的输出字段相同的名字和数据类型。

注意事项

CREATE TABLE AS的作用和SELECT INTO类似,且提供了SELECT INTO所提供功能的超集。建议使用CREATE TABLE AS语法替代SELECT INTO,因为SELECT INTO不能在存储过程中使用。

语法格式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
[ WITH [ RECURSIVE ] with_query [, ...] ]
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
    { * | {expression [ [ AS ] output_name ]} [, ...] }
    INTO [ UNLOGGED ] [ TABLE ] new_table
    [ FROM from_item [, ...] ]
    [ WHERE condition ]
    [ GROUP BY expression [, ...] ]
    [ HAVING condition [, ...] ]
    [ WINDOW {window_name AS ( window_definition )} [, ...] ]
    [ { UNION | INTERSECT | EXCEPT | MINUS } [ ALL | DISTINCT ] select ]
    [ ORDER BY {expression [ [ ASC | DESC | USING operator ] | nlssort_expression_clause ] [ NULLS { FIRST | LAST } ]} [, ...] ]
    [ { [ LIMIT { count | ALL } ] [ OFFSET start [ ROW | ROWS ] ] } | { LIMIT start, { count | ALL } } ]
    [ FETCH { FIRST | NEXT } [ count ] { ROW | ROWS } ONLY ]
    [ {FOR { UPDATE | SHARE } [ OF table_name [, ...] ] [ NOWAIT ]} [...] ];

参数说明

INTO [ UNLOGGED ] [ TABLE ] new_table

UNLOGGED指定表为非日志表。写入非日志表的数据不会写入预写日志中,这样就会比普通表快很多。但是,它也是不安全的,非日志表在冲突或异常关机后会被自动删截。非日志表中的内容也不会被复制到备用服务器中。在该类表中创建的索引也不会被自动记录。

new_table指定新建表的名字(可以用schema修饰)。

SELECT INTO的其它参数可参考SELECT的参数说明

示例

tpcds.reason表中r_reason_sk小于5的值加入到新建表中。

1
2
SELECT * INTO tpcds.reason_t1 FROM tpcds.reason WHERE r_reason_sk < 5;
INSERT 0 6

删除tpcds.reason_t1表。

1
DROP TABLE tpcds.reason_t1;

相关链接

SELECT