Summary of temporary tables and cursors

Source: Internet
Author: User

RelatedArticleNavigation
  1. SQL server2005 Transact-SQL new weapon learning Summary-Summary
  2. Index of flex and fms3 articles
  3. Flexair open-source edition-global free multi-person video chat room, free network remote multi-person Video Conferencing System (jointly developed by flex and fms3) <video chat, conference Development Instance 8>

Recently usedDBThere are many opportunities,I have made some summary:

I. Temporary table

Temporary tables are similar to permanent tables, but temporary tables are stored inTempdbIs automatically deleted when it is no longer in use.

Temporary tables can be local or global.

 

2By comparison:

The name of a local temporary table is symbolic.(#)Headers

Only visible to the current user connection

The instance is automatically deleted when it is disconnected.

 

The name of the global temporary table is symbolic.(##)Headers

All users are visible

It is automatically deleted when all users that reference the table are disconnected.

 

 

In fact, the local temporary table isTempdbWith a unique name

For example, we useSALog on to a query analyzer and use it again.SALog on to another query Analyzer

 

In2Query Analyzer:

Use pubs

Go

Select * into # TEM from jobs

 

Are2Users created2Local temporary tables

We can see from the following query statement

Select *

From [tempdb]. [DBO]. [sysobjects]

Where xtype = 'U'

Determine the existence of a temporary table: If Object_id ( ' Tempdb .. # TEM ' ) Is   Not   Null
Begin
Print   ' Exists '
End
Else
Begin
Print   ' Not exists '
End

Note:

1. In the dynamicSQLThe local temporary table created in the statement is automatically deleted after the statement is run.

Therefore, the following statements cannot obtain the result set.

Exec ('select * into # tems from job ')

Select * from # tems

 

2. Temporary tables used in the stored procedure are automatically deleted after the process is completed.

However, explicit deletion is recommended, which is beneficial to the system.

II. Cursor
The cursor can also be local or global.

Local cursor: used only in the declaration stage
Global cursors: they can be declared and used outside the trigger.


Judgment existence:If   Cursor_status ( ' Global ' , ' Cursor name ' ) =- 3   And   Cursor_status ( ' Local ' , ' Cursor name ' ) =- 3
Begin
Print   ' Not exists '
End

 

 

 

 

 

 

Favorites and sharing

Add QQ bookmarks to Baidu souzang {
Function onclick ()
{
Window. Open ('HTTP: // myweb.cn.yahoo.com/popadd.html? Url = '+ encodeuricomponent (document. location. href) + '& Title =' + encodeuricomponent (document. title), 'yahoo ', 'scrollbars = Yes, width = 440, Height = 440, Left = 80, Top = 80, status = Yes, resizable = Yes ');
}
} "> Add to Yahoo favorites

RSS subscribe to me What is RSS?




Dongguan. Net Club

Welcome to join

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.