How to use temporary SQL Server tables and table Variables

Source: Internet
Author: User
SQL Server Temporary tables of are divided into local temporary tables and global temporary tables, which are described as follows in books online:

There are two types of temporary tables: local and global, both of which have different names, visibility, and availability. The name of the local temporary table starts with a single numeric sign (#). They are only visible to the current user connection and are deleted when the user is disconnected from the Microsoft SQL Server 2000 instance. The name of the global temporary table starts with a mathematical sign (#). After being created, it is visible to all users. All users who reference this table are deleted when they are disconnected from SQL Server.

The local temporary table created in the stored procedure is equivalent to the stored procedure variable. It is visible only in the stored procedure, andProgramThe created local temporary table is the same as that written in the Microsoft online series and is visible to the current user connection. With this understanding, you can use different methods to create a local temporary table under different situations and requirements. This will make the temporary table the most useful. The SQL code:CodeRun later! ">

Normal browsing Copy code Print Code
  1. /***************** Table variables ********************/
  2. Declare@ TTable(
  3. CataidInt
  4. )
  5. Insert Into@ TSelectA. cataidFromF_getchlid (27,'123')
  6. Select Top10 favorites. *, multicata. cataname
  7. FromFavoritesLeft Outer JoinMulticataOnFavorites. cataid = multicata. cataid
  8. WhereFavorites. userid ='123' AndFavorites. cataidIn(
  9. Select*From@ T)
  10. /**************** Temporary table ********************* ***/
  11. Method 1,
  12. SelectA. cataidInto# TFromF_getchlid (27,'123')
  13. Select Top10 favorites. *, multicata. cataname
  14. FromFavoritesLeft Outer JoinMulticataOnFavorites. cataid = multicata. cataid
  15. WhereFavorites. userid ='123' AndFavorites. cataidIn(
  16. Select*From# T)
  17. Drop Table# T
  18. Method 2,
  19. Create Table# T (
  20. CataidInt
  21. )
  22. Insert Into# TSelectA. cataidFromZhaoguoguang. f_getchlid (27,'123')
  23. Select Top10 favorites. *, multicata. cataname
  24. FromFavoritesLeft Outer JoinMulticataOnFavorites. cataid = multicata. cataid
  25. WhereFavorites. userid ='123' AndFavorites. cataidIn(
  26. Select*From# T)
  27. Drop Table# T
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.