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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Syntax:<br />

Example:<br />

Notes:<br />

::= <br />

<br />

DML statements<br />

::= WITH [RECURSIVE] [, ...]<br />

::= name [()] AS ()<br />

::= column-alias [, column-alias ...]<br />

::= any SELECT statement or UNION<br />

::= the main SELECT statement, which can refer to the<br />

CTEs defined in the preamble<br />

with dept_year_budget as (<br />

select fiscal_year,<br />

dept_no,<br />

sum(projected_budget) as budget<br />

from proj_dept_budget<br />

group by fiscal_year, dept_no<br />

)<br />

select d.dept_no,<br />

d.department,<br />

dyb_2008.budget as budget_08,<br />

dyb_2009.budget as budget_09<br />

from department d<br />

left join dept_year_budget dyb_2008<br />

on d.dept_no = dyb_2008.dept_no<br />

and dyb_2008.fiscal_year = 2008<br />

left join dept_year_budget dyb_2009<br />

on d.dept_no = dyb_2009.dept_no<br />

and dyb_2009.fiscal_year = 2009<br />

where exists (<br />

select * from proj_dept_budget b<br />

where d.dept_no = b.dept_no<br />

)<br />

• A CTE definition can contain any legal SELECT statement, as long as it doesn't have a “WITH...” preamble<br />

of its own (no nesting).<br />

• CTEs defined for the same main query can reference each other, but care should be taken to avoid loops.<br />

• CTEs can be referenced from anywhere in the main query.<br />

• Each CTE can be referenced multiple times in the main query, possibly with different aliases.<br />

• When enclosed in parentheses, CTE constructs can be used as subqueries in SELECT statements, but also in<br />

UPDATEs, MERGEs etc.<br />

• In PSQL, CTEs are also supported in FOR loop headers:<br />

69

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

Saved successfully!

Ooh no, something went wrong!