Use of temporary tables for SQL Learning Series (ii)

Source: Internet
Author: User

1. Notes on the use of temporary tables:

(1). The temporary table is actually a user table placed in the database tempdb.

(2). Temptablename must have "#" and "#" can be one or two tables that start with # (local) or # # (global), which exists during the session and is automatically deleted when the session ends.

(3). If you do not #开头 with # or # when you create it, use tempdb instead. TempTable to name it, the table can persist until the database is restarted.

2. Delete temporary tables

drop table Temptablename

(1). When the stored procedure finishes, the local temporary table created in the stored procedure is automatically dropped. This table can be referenced by all nested stored procedures that are executed by the stored procedure that created the table. However, the process that called the stored procedure that created this table cannot reference this table.
(2). All other local temporary tables are automatically dropped at the end of the current session.
(3). The Global temporary table is automatically dropped when the session that created the table ends and other tasks stop referencing them. The association between a task and a table is persisted only within the lifetime of a single Transact-SQL statement. In other words, when the session that created the global temporary table ends, the last Transact-SQL statement referencing this table is finished and the table is automatically dropped.

3. Using the Code

--Delete temporary table drop table #Student--creates a temporary table #Student the CREATE TABLE (name nvarchar ( -) notNULL, Ageint)--EnquirySelect* from#Student--add insert into #Student values ('Zhang San', -)--Remove Delete #StudentwhereName='Zhang San'--Modify the update #StudentSetAge= - whereName='Zhang San'
View Code

Use of temporary tables for SQL Learning Series (ii)

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.