To create a temporary table in an Oracle stored procedure

Source: Internet
Author: User
Tags rollback

DDL statements cannot be used directly in Oracle's stored procedures, such as CREATE, alter, DROP, truncate, and so on.

That assumes that we want to create a temporary table in a stored procedure that can only use dynamic SQL statements:

Create or replace procedure Pro as  str_sql varchar2 (+); Begin  -Creates a temporary table  str_sql: = ' Create global temporary TA Ble temp_table (       col1 varchar2 (Ten),       col2 number    ) on commit preserve rows ';  Execute immediate str_sql;  --Use temporary table  str_sql: = ' insert into temp_table (col1, col2) VALUES (' A ', ' 1 ') ';  Execute immediate str_sql;  --Delete temporary table  str_sql: = ' drop table temp_table ';  Execute immediate str_sql;end;

In Oracle. The temporary table is divided into two types: Session level and transaction level (transaction).

The session-level temporal table exists throughout the session until the session ends, and the transaction-level transient table data disappears after the transaction is finished. That is, Commit/rollback or ending the session,

The temporary table data is cleared.

On commit Preserve rows-session level temporary table (exit login ends session)

On commit Delete rows-transaction level temporary table (commit or rollback will end transaction)


Temporary table pros and Cons:

1. It is recommended to use cursors when querying data only.

2. The temporary table does not establish an index. Therefore, it is not recommended to assume a larger data volume or multiple queries.

To create a temporary table in an Oracle stored procedure

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.