zoukankan      html  css  js  c++  java
  • Local vs Global Temporary Table

    1. Local temporary table

    Syntax:

    -- local temporary
    select EnglishProductName, ListPrice - DealerPrice as Discount 
    into #NewDimProduct
    from DimProduct
    where ListPrice - DealerPrice is not null
    order by EnglishProductName
    -- test
    select * from #NewDimProduct
    -- drop
    drop table #NewDimProduct
    select * from #NewDimProduct

    Analysis:

    The # prefix to the new table nameresults in the creation of a new temporary table. A single # prefix means a local temporary table. Before you run the DROP TABLE statement, it’s worth considering a few things. As the table is temporary, it will automatically disappear if you close your query editor window. Also, as it’s a temporary table, it lives in your tempdb database. You can view it in Object Explorer --- open the System Database folder, then tempdb, and expand Temporary Tables (you may need to right-click and choose Refresh). It’s a local temporary table; it can only be seen from the current query editor window. If you were to open a new window, a normal Select on the table would fail.

       Temporary tables are often used as work tables or staging tables or test tables. Maybe you want to experiment with some SQL, but prefer not to do so on a production table.

    2. Global Temporary Table

    Syntax:

    -- global temporary
    select EnglishProductName, ListPrice - DealerPrice as Discount 
    into ##NewDimProduct
    from DimProduct
    where ListPrice - DealerPrice is not null
    order by EnglishProductName
    -- test
    select * from ##NewDimProduct
    -- drop
    drop table ##NewDimProduct
    select * from ##NewDimProduct

    Analysis:

    A double ## prefix means a global temporary table. It will reside in your tempdb database until you close the query editor window or issue a Drop Table. In those respects, it’s the same as a local temporary table. The difference lies in its scope. A local temporary table is only accessible from the current query editor window(called a session). On the other hand, a global temporary table is visible (during its lifetime) from other query editor windows or sessions. Global temporary tables can be used from testing or as staging tables when you want to start a new SQL query in a new window.

    3. Semipermanent Temporary Table

    Take a careful look at the first query here. It’s another Select Into but the table name is qualified with a schema and a database name. The database in question is the system database, tempdb.

    Syntax:

    -- semi-permanent temporary
    select EnglishProductName, ListPrice - DealerPrice as Discount 
    into tempdb.dbo.NewDimProduct
    from DimProduct
    where ListPrice - DealerPrice is not null
    order by EnglishProductName
    -- test
    select * from tempdb.dbo.NewDimProduct
    -- drop
    drop table tempdb.dbo.NewDimProduct
    select * from tempdb.dbo.NewDimProduct

    Analysis:

    You can explicitly create the new table in tempdb. But you won’t see it in the Object Explorer underneath the tempdb Temporary Tables folder. Rather, it’s under the tempdb Tables folder. It’s a permanent table in tempdb(your temporary database). They are not truly temporary --- if you close the query editor windows, the tables are still there for other query editor windows and users. They act just like permanent tables in normal production databases.

    But then, they are not truly permanent --- if you stop and restart SQL Server, they are erased from tempdb. The tempdb database is always re-created and cleared as a result when SQL Server starts up. Again, these tables can be used as test or staging tables.

    4. Appendix:

    Query the temporary tables exist?

    if object_id('tempdb..#TC_TableRelation') is null
    begin
        CREATE TABLE [#TC_TableRelation](
            [ID] [nvarchar](255) NOT NULL,
            [ViewName] [nvarchar](50) NOT NULL,
            [MasterTableName] [nvarchar](255) NULL,
            [PrimaryKey] [nvarchar](50) NOT NULL,
            [PrimaryValue] [nvarchar](255) NULL,
            [Status] [nvarchar](255) NULL
        ) ON [PRIMARY]
    end

    Or

    if exists (select * from tempdb.dbo.sysobjects where id = object_id(N'tempdb..#TC_TableRelation') and type='U')
       drop table #TC_TableRelation
  • 相关阅读:
    《算法导论》第十章----基本数据结构
    《算法导论》第九章----中位数和顺序统计学
    《算法导论》第八章----线性时间排序(决策树+计数排序+基数排序)
    C++实现快速排序
    C++实现斐波那契第N项非递归与递归实现的时间比较
    C++实现用两个栈实现队列
    C++实现从尾到头打印链表(不改变链表结构)
    C++实现二叉树(建树,前序,中序,后序)递归和非递归实现
    Spark 大数据文本统计
    逻辑回归--参数解释+数据特征不独热编码+训练数据分布可视话
  • 原文地址:https://www.cnblogs.com/aot/p/3148227.html
Copyright © 2011-2022 走看看