NAME
SELECT INTO - 从一个查询的结果中创建一个新表
SYNOPSIS
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] * | expression [ AS output_name ] [, ...] INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table [ FROM from_item [, ...] ] [ WHERE condition ] [ GROUP BY expression [, ...] ] [ HAVING condition [, ...] ] [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ] [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ] [ LIMIT { count | ALL } ] [ OFFSET start ] [ FOR UPDATE [ OF tablename [, ...] ] ]
DESCRIPTION 描述
SELECT INTO 从一个查询的计算结果中创建一个新表。 数据并不返回给客户端,这一点和普通的 SELECT 不同。 新表的字段具有和 SELECT 的输出字段相关联(相同)的名字和数据类型。
PARAMETERS 参数
- TEMPORARY 或 TEMP
如果声明了这个关键字,那么该表是作为一个临时表创建的。 请参考 CREATE TABLE [create_table(7)] 获取细节。- new_table
要创建的表的表名(可以有模式修饰)。
所有其它输入的域都在 SELECT [select(7)] 中有详细描述。
NOTES 注意
CREATE TABLE AS [create_table_as(7)] 的作用和 SELECT INTO 相同。 我们建议使用 CREATE TABLE AS 语法, 因为 SELECT INTO 不是标准语法。 实际上,这种类型的 SELECT INTO 是不能在 ECPG 或者 PL/pgSQL 中使用的, 因为它们对 INTO 子句的解释是不同的。
COMPATIBILITY 兼容性
SQL 标准用 SELECT ... INTO 表示选取数值到一个宿主程序的标量变量中, 而不是创建一个新表。这种用法实际上就是在 ECPG (参阅 Chapter 29)和PL/pgSQL (Chapter 35)里的用途。 PostgreSQL 用 SELECT INTO 代表创建表的意思是历史原因。 在新代码里我们***使用 CREATE TABLE AS 实现这个目地。 (CREATE TABLE AS 也不是标准,但至少它出现混淆的机会少一些。)
#p#
NAME
SELECT INTO - create a new table from the results of a query
SYNOPSIS
SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] * | expression [ AS output_name ] [, ...] INTO [ TEMPORARY | TEMP ] [ TABLE ] new_table [ FROM from_item [, ...] ] [ WHERE condition ] [ GROUP BY expression [, ...] ] [ HAVING condition [, ...] ] [ { UNION | INTERSECT | EXCEPT } [ ALL ] select ] [ ORDER BY expression [ ASC | DESC | USING operator ] [, ...] ] [ LIMIT { count | ALL } ] [ OFFSET start ] [ FOR UPDATE [ OF tablename [, ...] ] ]
DESCRIPTION
SELECT INTO creates a new table and fills it with data computed by a query. The data is not returned to the client, as it is with a normal SELECT. The new table's columns have the names and data types associated with the output columns of the SELECT.
PARAMETERS
- TEMPORARY or TEMP
- If specified, the table is created as a temporary table. Refer to CREATE TABLE [create_table(7)] for details.
- new_table
- The name (optionally schema-qualified) of the table to be created.
All other parameters are described in detail under SELECT [select(7)].
NOTES
CREATE TABLE AS [create_table_as(7)] is functionally equivalent to SELECT INTO. CREATE TABLE AS is the recommended syntax, since this form of SELECT INTO is not available in ECPG or PL/pgSQL, because they interpret the INTO clause differently.
COMPATIBILITY
The SQL standard uses SELECT ... INTO to represent selecting values into scalar variables of a host program, rather than creating a new table. This indeed is the usage found in ECPG and PL/pgSQL. The PostgreSQL usage of SELECT INTO to represent table creation is historical. It's best to use CREATE TABLE AS for this purpose in new code. (CREATE TABLE AS isn't standard either, but it's less likely to cause confusion.)