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

SQL Language Enhancements

CASE WHEN V1 IS NOT NULL THEN V1

ELSE COALESCE (V2,...,Vn) END

Examples

SELECT

PROJ_NAME AS Projectname,

COALESCE(e.FULL_NAME,'[< not assigned >]') AS Employeename

FROM

PROJECT p

LEFT JOIN EMPLOYEE e

ON (e.EMP_NO = p.TEAM_LEADER);

SELECT

COALESCE(Phone,MobilePhone,'Unknown') AS "Phonenumber"

FROM

Relations;

c) NULLIF

Returns NULL for a sub-expression if it has a specific value, otherwise returns the value of the sub-expression.

Syntax Pattern

<case abbreviation> ::=

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

Syntax Rules

NULLIF (V1, V2) is equivalent to the following <case specification>:

CASE WHEN V1 = V2 THEN NULL ELSE V1 END

Example

UPDATE PRODUCTS

SET STOCK = NULLIF(STOCK,0)

(1.5) SQL99-compliant Savepoints

Nickolay Samofatov

User savepoints (alternative name nested transactions) provide a convenient method to handle business logic errors without needing to roll back the transaction. Available only in DSQL.

Use the SAVEPOINT statement to identify a point in a transaction to which you can later roll back.

Syntax Patterns

27

SQL Language Enhancements

SAVEPOINT <identifier>;

<identifier> specifies the name of a savepoint to be created. After a savepoint has been created, you can either continue processing, commit your work, roll back the entire transaction, or roll back to the savepoint.

Savepoint names must be distinct within a given transaction. If you create a second savepoint with the same identifer as an earlier savepoint, the earlier savepoint is erased.

ROLLBACK [WORK] TO [SAVEPOINT] <identifier>;

This statement performs the following operations:

Rolls back changes performed in the transaction after the savepoint

Erases all savepoints created after that savepoint. The named savepoint is retained, so you can roll back to the same savepoint multiple times. Prior savepoints are also retained.

Releases all implicit and explicit record locks acquired since the savepoint. Other transactions that have requested access to rows locked after the savepoint must continue to wait until the transaction is committed or rolled back. Other transactions that have not already requested the rows can request and access the rows immediately.

Note

This behaviour may change in future product versions.

Important

The Savepoint undo log may consume significant amounts of server memory, especially if you update the same records in the same transaction multiple times. Use the RELEASE SAVEPOINT statement to release system resources consumed by savepoint maintenance.

RELEASE SAVEPOINT <identifier> [ONLY];

RELEASE SAVEPOINT statement erases the savepoint <identifer> from the transaction context. Unless you specify the ONLY keyword, all savepoints established since the savepoint <identifier> are erased too.

Example using Savepoints

create table test (id integer); commit;

insert into test values (1); commit;

insert into test values (2); savepoint y;

delete from test;

select * from test; -- returns no rows

28

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