Global Temp Tables - SQL Server vs Oracle

后端 未结 4 1528
离开以前
离开以前 2021-01-13 08:22

I am using Oracle 11g Global Temporary Tables as I need a solution where I can add rows to a temporary table for a join, and I want only the rows added to the temp table for

4条回答
  •  醉酒成梦
    2021-01-13 09:00

    Temporary tables on SQL Server are local by default. The table will be dropped after the session ends. If you execute a script like:

    create table #Foo (
           FooID  int
          ,FooCode1  varchar (20)
    )
    
    insert table #Foo (FooID, FooCode1)
    values (1001, 'X')
    
    insert table #Foo (FooID, FooCode1)
    values (1002, 'Y')
    
    select f.FooID
          ,f.FooCode1
          ,b.BarID
          ,b.BarCode1
      from #foo f
      join bar b
        on bar.FooID = f.FooID -- (or whatever predicate)
    

    The query will only return rows that join on what you inserted into #Foo in this session. #Foo is local to the session; you can have multiple sessions with their own #Foo temporary table with no worries about namespace collisions. When the session is closed the temporary table will be discarded. You can also explicitly drop #Foo after you've finished with it if you're using a persistent database connection (e.g. a client-server desktop app).

提交回复
热议问题