|
| 1 | +SQL Language Extension: UNLIST |
| 2 | + |
| 3 | +Function: |
| 4 | +The function parses the input string using the specified delimiter (comma "," is implied by default) and returns the identified substrings as discrete records containing a single field. Additionally, the desired type of the returned field can be specified. If the specified data type conversion is impossible, an error is raised at runtime. |
| 5 | + |
| 6 | +Author: |
| 7 | +Chudaykin Alex <chudaykinalex@gmail.com> |
| 8 | + |
| 9 | +Format |
| 10 | +<table value function> ::= |
| 11 | +UNLIST ( <input> [, <separator>] [, <data type conversion>] ) [AS] <correlation name> [ ( <derived column name> ) ] |
| 12 | + |
| 13 | +<input> ::= <value> |
| 14 | + |
| 15 | +<separator> ::= <value> |
| 16 | + |
| 17 | +<data type conversion> ::= RETURNING <data type> |
| 18 | + |
| 19 | +Syntax Rules: |
| 20 | + |
| 21 | +1) <input>: any value expression that returns a string/blob of characters (or may be converted to a string), including string literals, table columns, constants, variables, expressions, etc. This parameter is mandatory. |
| 22 | +2) <separator>: optional value expression that returns a string which is used as a delimiter (i.e. it separates one value from another inside the input string). It may also be a BLOB TEXT value, limited to 32KB. If an empty string is specified, the output will be just one record containing the input string. If omitted, the comma character is used as a delimiter. |
| 23 | +2) <data type>: target data type to convert the output values into. Alternatively, a domain can be specified as the returned type. If omitted, VARCHAR(32) is implied. Feel free to suggest any better alternative default. |
| 24 | +3) <correlation name>: alias of the record set returned by the UNLIST function. It is a mandatory parameter (per SQL standard). |
| 25 | +4) <derived column name>: optional alias of the column returned by the UNLIST function. If omitted, UNLIST is used as an alias. |
| 26 | + |
| 27 | +Example: |
| 28 | +A) |
| 29 | +SELECT * FROM UNLIST('1,2,3,4,5') AS U; |
| 30 | + |
| 31 | +B) |
| 32 | +SELECT * FROM UNLIST('100:200:300:400:500', ':' RETURNING INT) AS U; |
| 33 | + |
| 34 | +C) |
| 35 | +SELECT U.* FROM UNLIST(‘text1, text2, text3’) AS U; |
| 36 | + |
| 37 | +D) |
| 38 | +SELECT C0 FROM UNLIST(‘text1, text2, text3’) AS U(C0); |
| 39 | + |
| 40 | +E) |
| 41 | +SELECT U.C0 FROM UNLIST(‘text1, text2, text3’) AS U(C0); |
| 42 | + |
| 43 | +F) |
| 44 | +SET TERM ^ ; |
| 45 | +RECREATE PROCEDURE TEST_PROC RETURNS (PROC_RETURN_INT INT) |
| 46 | +AS |
| 47 | +DECLARE VARIABLE text VARCHAR(11); |
| 48 | +BEGIN |
| 49 | +text = '123:123:123'; |
| 50 | +FOR SELECT * FROM UNLIST( :text, ':' RETURNING INT) AS A INTO :PROC_RETURN_INT DO |
| 51 | +SUSPEND; |
| 52 | +END^ |
| 53 | +SET TERM ; ^ |
| 54 | +SELECT * FROM TEST_PROC; |
| 55 | + |
| 56 | +G) |
| 57 | +CREATE DOMAIN D1 AS INT; |
| 58 | +SELECT TEST_DOMAIN FROM UNLIST('1,2,3,4' RETURNING D1) AS A(TEST_DOMAIN); |
| 59 | + |
| 60 | +CREATE TABLE TABLE_TEST (COL1 INT); |
| 61 | +SELECT TEST_TYPE_OF FROM UNLIST('1,2,3,4' RETURNING TYPE OF COLUMN TABLE_TEST.COL1) AS A(TEST_TYPE_OF); |
| 62 | +H) |
| 63 | +CREATE VIEW TEST_VIEW AS SELECT * FROM UNLIST('1,2,3,4') AS A(B); |
| 64 | +SELECT B FROM TEST_VIEW; |
| 65 | + |
| 66 | +Unacceptable behavior: |
| 67 | +SELECT UNLIST FROM UNLIST('UNLIST,A,S,A') AS A; |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
0 commit comments