1- <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.514 2010/06/0301:34:02 momjian Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.515 2010/06/0302:06:10 momjian Exp $ -->
22
33 <chapter id="functions">
44 <title>Functions and Operators</title>
@@ -11419,7 +11419,7 @@ SELECT * FROM generate_series('2008-03-01 00:00'::timestamp,
1141911419 elements). Some examples follow:
1142011420<programlisting>
1142111421-- basic usage
11422- select generate_subscripts('{NULL,1,NULL,2}'::int[], 1)as s;
11422+ SELECT generate_subscripts('{NULL,1,NULL,2}'::int[], 1)AS s;
1142311423 s
1142411424---
1142511425 1
@@ -11430,32 +11430,33 @@ select generate_subscripts('{NULL,1,NULL,2}'::int[], 1) as s;
1143011430
1143111431-- presenting an array, the subscript and the subscripted
1143211432-- value requires a subquery
11433- select *from arrays;
11433+ SELECT *FROM arrays;
1143411434 a
1143511435--------------------
1143611436 {-1,-2}
11437- {100,200}
11437+ {100,200,300 }
1143811438(2 rows)
1143911439
11440- select a as array, s as subscript, a[s] as value
11441- from (select generate_subscripts(a, 1) as s, a from arrays) foo;
11442- array | subscript | value
11443- -----------+-----------+-------
11444- {-1,-2} | 1 | -1
11445- {-1,-2} | 2 | -2
11446- {100,200} | 1 | 100
11447- {100,200} | 2 | 200
11448- (4 rows)
11440+ SELECT a AS array, s AS subscript, a[s] AS value
11441+ FROM (SELECT generate_subscripts(a, 1) AS s, a FROM arrays) foo;
11442+ array | subscript | value
11443+ ---------------+-----------+-------
11444+ {-1,-2} | 1 | -1
11445+ {-1,-2} | 2 | -2
11446+ {100,200,300} | 1 | 100
11447+ {100,200,300} | 2 | 200
11448+ {100,200,300} | 3 | 300
11449+ (5 rows)
1144911450
1145011451-- unnest a 2D array
11451- create or replace function unnest2(anyarray)
11452- returns setof anyelementas $$
11452+ CREATE OR REPLACE FUNCTION unnest2(anyarray)
11453+ RETURNS SETOF anyelementAS $$
1145311454select $1[i][j]
1145411455 from generate_subscripts($1,1) g1(i),
1145511456 generate_subscripts($1,2) g2(j);
11456- $$language sqlimmutable ;
11457+ $$LANGUAGE sqlIMMUTABLE ;
1145711458CREATE FUNCTION
11458- postgres=#select *from unnest2(array [[1,2],[3,4]]);
11459+ postgres=#SELECT *FROM unnest2(ARRAY [[1,2],[3,4]]);
1145911460 unnest2
1146011461---------
1146111462 1