21.10.2013 Views

Firebird 2.1 Language Reference Update

Firebird 2.1 Language Reference Update

Firebird 2.1 Language Reference Update

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

DML statements<br />

Description: A SELECT query used in an INSERT statement may now be a UNION.<br />

Example:<br />

insert into Members (number, name)<br />

select number, name from NewMembers where Accepted = 1<br />

union<br />

select number, name from SuspendedMembers where Vindicated = 1<br />

Available in: DSQL, PSQL<br />

Added in: <strong>2.1</strong><br />

MERGE<br />

Description: Merges data into a table or view. The source may a table, view or derived table (i.e. a parenthesized<br />

SELECT statement or CTE). Each source record will be used to update one or more target records, insert a new<br />

record in the target table, or neither. The action taken depends on the provided condition and the WHEN clause(s).<br />

The condition will typically contain a comparison of fields in the source and target relations.<br />

Syntax:<br />

MERGE INTO {tablename | viewname} [[AS] alias]<br />

USING {tablename | viewname | (select_stmt)} [[AS] alias]<br />

ON condition<br />

WHEN MATCHED THEN UPDATE SET colname = value [, colname = value ...]<br />

WHEN NOT MATCHED THEN INSERT [()] VALUES ()<br />

::= colname [, colname ...]<br />

::= value [, value ...]<br />

Examples:<br />

Note: It is allowed to provide only one of the WHEN clauses<br />

merge into books b<br />

using purchases p<br />

on p.title = b.title and p.type = 'bk'<br />

when matched then<br />

update set b.desc = b.desc || '; ' || p.desc<br />

when not matched then<br />

insert (title, desc, bought) values (p.title, p.desc, p.bought)<br />

merge into customers c<br />

using (select * from customers_delta where id > 10) cd<br />

on (c.id = cd.id)<br />

when matched then update set name = cd.name<br />

when not matched then insert (id, name) values (cd.id, cd.name)<br />

Note<br />

WHEN NOT MATCHED should be interpreted from the point of view of the source (the relation in the USING<br />

clause). That is: if a source record doesn't have a match in the target table, the INSERT clause is executed.<br />

Conversely, records in the target table without a matching source record don't trigger any action.<br />

65

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!