4.2.13. Row Constructors

4.2.13. Row Constructors
4.2.13.行构造函数
A row constructor is an expression that builds a row value (also called a composite value) using values  for its member fields. A row constructor consists of the key word ROW , a left parenthesis, zero or  more expressions (separated by commas) for the row field values, and finally a right parenthesis. For  example:
行构造函数是一个表达式,该表达式使用其成员字段的值来构建行值(也称为复合值)。行构造函数由关键字ROW,左括号,行字段值的零个或多个表达式(用逗号分隔)以及最后的一个右括号组成。例如:
 
SELECT ROW(1,2.5,'this is a test');
 
The key word ROW is optional when there is more than one expression in the list.
当列表中有多于一个的表达式,则关键词ROW可省略。
 
A row constructor can include the syntax rowvalue .* , which will be expanded to a list of the  elements of the row value, just as occurs when the .* syntax is used at the top level of a SELECT list  (see Section 8.16.5). For example, if table t has columns f1 and f2 , these are the same:
行构造函数可以包含rowvalue.*语法,该语法将扩展为该行值的元素列表,就像在SELECT列表的顶层使用.*语法时一样(请参阅第8.16.5节)。例如,如果表t具有列f1和f2,则以下语句是相同的:
 
SELECT ROW(t.*, 42) FROM t;
SELECT ROW(t.f1, t.f2, 42) FROM t;
 
Note
Before PostgreSQL 8.2, the .* syntax was not expanded in row constructors, so that  writing ROW(t.*, 42) created a two-field row whose first field was another row  value. The new behavior is usually more useful. If you need the old behavior of nested  row values, write the inner row value without .* , for instance ROW(t, 42) .
在PostgreSQL8.2之前,行构造函数中没有.*的语法,所以ROW(t.*,42)创建了一行的两个部分,其中第一部分为另一行值。新的行为通常更有用。如果想回到之前的语法,则可以在内部行值中不写.*,例如ROW(t,42)。
 
By default, the value created by a ROW expression is of an anonymous record type. If necessary, it can  be cast to a named composite type — either the row type of a table, or a composite type created with  CREATE TYPE AS . An explicit cast might be needed to avoid ambiguity. For example:
默认,由ROW表达式创建的值是匿名record数据类型。如有必要,其可以转换为命名的复合数据类型--表的行类型或者使用CREATE TYPE AS创建的复合类型。为避免歧义,最好使用显式的类型转换。例如:
 
CREATE TABLE mytable(f1 int, f2 float, f3 text);
CREATE FUNCTION getf1(mytable) RETURNS int AS 'SELECT $1.f1'
LANGUAGE SQL;
-- No cast needed since only one getf1() exists
SELECT getf1(ROW(1,2.5,'this is a test'));
getf1
-------
1
(1 row)
CREATE TYPE myrowtype AS (f1 int, f2 text, f3 numeric);
CREATE FUNCTION getf1(myrowtype) RETURNS int AS 'SELECT $1.f1'
LANGUAGE SQL;
-- Now we need a cast to indicate which function to call:
SELECT getf1(ROW(1,2.5,'this is a test'));
ERROR: function getf1(record) is not unique
SELECT getf1(ROW(1,2.5,'this is a test')::mytable);
getf1
-------
1
(1 row)
SELECT getf1(CAST(ROW(11,'this is a test',2.5) AS myrowtype));
getf1
-------
11
(1 row)
 
Row constructors can be used to build composite values to be stored in a composite-type table column, or to be passed to a function that accepts a composite parameter. Also, it is possible to compare two  row values or test a row with IS NULL or IS NOT NULL , for example:
行构造函数可以为表的复合类型列生成复合值,或者为需要复合参数的函数传值。当然,也可以比较两行的值,或者使用IS NULL或者IS NOT NULL测试一行,例如:
 
SELECT ROW(1,2.5,'this is a test') = ROW(1, 3, 'not the same');
SELECT ROW(table.*) IS NULL FROM table; -- detect all-null rows
 
For more detail see Section 9.23. Row constructors can also be used in connection with subqueries, as discussed in Section 9.22.
更多详情,请参见 9.23节。如 9.22节讨论的那样,行构造函数也可与子查询一起使用。
 
发布了341 篇原创文章 · 获赞 53 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104303623
Row