Thursday, December 31, 2009

Sybase Temporary Table

There are seven types of table in Sybase. Aside from regular permanent table and
proxy table, there are 5 types of temporary tables. The most commonly used
temporary tables are DECLARE temporary table and CREATE temporary table.
The big difference comes that DECLARE temporary table is code-block-long, meaning
it exist only within a code block, e.g. a stored procedure; on the other side,
CREATE temporary table lives the whole session or connection.

The syntax
<declare_local_temporary_table>
::= DECLARE LOCAL TEMPORARY TABLE <table_name>
<table_element_list>
[ <commit_action> ]
<table_name> ::= <identifier>
<commit_action> ::= ON COMMIT DELETE ROWS
| ON COMMIT PRESERVE ROWS
| NOT TRANSACTIONAL

<create_local_temporary_table>
::= CREATE LOCAL TEMPORARY TABLE <table_name>
<table_element_list>
[ <commit_action> ]

Syntactically, they are almost the same except the keywords of the commands are different. Interestingly, we can even define behavior for commit and rollback actions of the temporary table. ON COMMIT DELETE ROWS, for example, lets implicitly delete all rows when commit is issued; while ON COMMIT PRESERVE ROWS doesn't delete row, just keeping rows as a normal transaction operation does.

No comments: