Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
36
Добавлен:
18.03.2015
Размер:
608.7 Кб
Скачать

SQL Language Enhancements

Data Manipulation Language (DML)

Data manipulation language, or DML is the language of query statements, the commands we use to manipulate data, that is, to SELECT FROM, INSERT, UPDATE and DELETE from tables and to EXECUTE PROCEDUREs.

(1.5) Expressions and variables as procedure arguments

Dmitry Yemanov

Calls to EXECUTE PROCEDURE ProcName(<Argument-list>) and SELECT <Output-list> FROM ProcName(<Argument-list>) can now accept local variables (in PSQL) and expressions (in DSQL and PSQL) as arguments.

(1.5) New constructs for CASE expressions

Arno Brinkman

a) CASE

Allow the result of a column to be determined by the outcome of a group of exclusive conditions.

Syntax Pattern

<case expression> ::=

<case abbreviation> | <case specification>

<case abbreviation> ::=

NULLIF <left paren> <value expression> <comma> <value expression> <right paren>

| COALESCE <left paren> <value expression> { <comma> <value expression> }... <right paren>

<case specification> ::=

<simple case> | <searched case>

<simple case> ::=

CASE <value expression> <simple when clause>...

[ <else clause> ] END

<searched case> ::=

CASE <searched when clause>...

[ <else clause> ] END

<simple when clause> ::= WHEN <when operand> THEN <result> <searched when clause> ::= WHEN <search condition> THEN <result> <when operand> ::= <value expression>

<else clause> ::= ELSE <result>

<result> ::= <result expression> | NULL <result expression> ::= <value expression>

25

SQL Language Enhancements

Examples

i) simple

SELECT o.ID,

o.Description, CASE o.Status

WHEN 1 THEN 'confirmed' WHEN 2 THEN 'in production' WHEN 3 THEN 'ready'

WHEN 4 THEN 'shipped'

ELSE 'unknown status ''' || o.Status || ''''

END

FROM Orders o;

ii) searched

SELECT o.ID,

o.Description, CASE

WHEN (o.Status IS NULL) THEN 'new' WHEN (o.Status = 1) THEN 'confirmed'

WHEN (o.Status = 3) THEN 'in production' WHEN (o.Status = 4) THEN 'ready'

WHEN (o.Status = 5) THEN 'shipped'

ELSE 'unknown status ''' || o.Status || ''''

END

FROM Orders o;

b) COALESCE

Allows a column value to be calculated by a number of expressions, from which the first expression to return a non-NULL value is returned as the output value.

Syntax Pattern

<case abbreviation> ::=

| COALESCE <left paren> <value expression>

{ <comma> <value expression> }... <right paren>

Syntax Rules

1.COALESCE (V1, V2) is equivalent to the following <case specification>:

CASE WHEN V1 IS NOT NULL THEN V1 ELSE V2 END

2.COALESCE (V1, V2,..., Vn), for n >= 3, is equivalent to the following <case specification>:

26

Соседние файлы в папке doc