Comments on temporary table and Table Variables

Source: Internet
Author: User
Tags variable scope
-- Comment on temporary table and Table Variables

Note
You can declare functions and variables as table types. Table variables can be used in functions, stored procedures, and batch processing.

Try to use table variables instead of temporary tables. Table variables have the following advantages:

The behavior of table variables is similar to that of local variables and has a clearly defined scope. This scope is the function, stored procedure, or batch processing that declares the variable.
In its scope, table variables can be used as regular tables. This variable can be applied to tables or table expressions in select, insert, update, and delete statements. However, table cannot be used in the following statements:

Insert into table_variable exec stored procedure.

Select select_list into table_variable statement.

When the function, stored procedure, or batch processing of the table variable is defined, the table variable is automatically cleared.

Using table variables in a stored procedure reduces the amount of recompilation in a stored procedure compared to using a temporary table.


Transactions involving table variables only exist during table variable update. This reduces the need for table variables to lock and record resources.
Table variables cannot be assigned values. In addition, because the table variable scope is limited and is not part of the persistent database, it is not affected by transaction rollback.

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.