Delete the temp table
Temporary Tables
The simple answer is yes you can. Let look at a simple CREATE TABLE statement:
CREATE TABLE #Yaks (
YakID int,
YakName char(30) )
You'll notice I prefixed the table with a pound sign (#). This tells SQL Server that this table is a local temporary table. This table is only visible to this session of SQL Server. When I close this session, the table will be automatically dropped. You can treat this table just like any other table with a few exceptions. The only real major one is that you can't have foreign key constraints on a temporary table.
Temporary tables are created in TempDB. If you run this query:
CREATE TABLE #Yaks ( YakID int, YakName char(30) ) select name from tempdb..sysobjects where name like '#yak%' drop table #yaks
You'll get something like this:
name ------------------------------------------------------------------------------------ #Yaks_________________________ . . . ___________________________________00000000001D (1 row(s) affected)
except that I took about fifty underscores out to make it readable. SQL Server stores the object with a some type of unique number appended on the end of the name. It does all this for you automatically. You just have to refer to #Yaks in your code.
If two different users both create a #Yaks table each will have their own copy of it. The exact same code will run properly on both connections. Any temporary table created inside a stored procedure is automatically dropped when the stored procedure finishes executing. If stored procedure A creates a temporary table and calls stored procedure B, then B will be able to use the temporary table that A created. It's generally considered good coding practice to explicitly drop every temporary table you create. If you are running scripts through SQL Server Management Studio or Query Analyzer the temporary tables are kept until you explicitly drop them or until you close the session.
Now let's get back to your question. The best way to use a temporary table is to create it and then fill it with data. This goes something like this:
CREATE TABLE #TibetanYaks( YakID int, YakName char(30) ) INSERT INTO #TibetanYaks (YakID, YakName) SELECT YakID, YakName FROM dbo.Yaks WHERE YakType = 'Tibetan' -- Do some stuff with the table drop table #TibetanYaks
And you can use the SQL as bellow when you create a temp table.
if object_id('TempDB..#TibetanYaks') is not null
drop table #TibetanYaks
CREATE TABLE #TibetanYaks( YakID int, YakName char(30) )
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/14321372/viewspace-610632/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Oracle Temp Table ConceptOracle
- DELETE_TABLE_STATS Proceduredelete
- the procedure:delete the data of one tabledelete
- recover database delete archivelogs skip tablespace temp;報錯DatabasedeleteHive
- 【sql調優之執行計劃】temp table transformationSQLORM
- delete table 和 truncate table - 型別安全的心 - 部落格園delete型別
- Truncate table 詳解及與delete,drop 的區別delete
- Truncate table詳解及與delete,drop的區別delete
- temp
- DELETE TABLE資料後,查詢變慢,問題處理delete
- TEMP表空間不足解決 - temp group
- create table進階學習(二)_dba_free_temp_space_v$tempseg_usage_v$sort_segment
- Oracle - ORA-01652: unable to extend temp segment by 128 in tablespace TEMPOracle
- delete input 與 delete all inputdelete
- delete OBSOLETE 與 delete expirieddelete
- oracle temp 表空間Oracle
- deletedelete
- temp資料夾可以刪除嗎 temp資料夾幹啥的
- 聊聊Data Guard環境下Temp表空間和Temp檔案管理
- delete 和 delete [] 的真正區別delete
- [Oracle Script] check temp tablespace usageOracle
- ORACLE temp表的簡介Oracle
- temp表學習筆記筆記
- delete與delete[]需要注意的地方delete
- FormData delete()ORMdelete
- URLSearchParams delete()delete
- JavaScript deleteJavaScriptdelete
- DELETE STATISTICSdelete
- rman 中delete 與delete force 的區別delete
- 檢視單個SQL消耗TEMP表空間以及TEMP表空間使用率SQL
- Oracle Temp 表空間切換Oracle
- tablespace 大檔案,undo,temp tablespace
- temp檔案空間的分配
- Oracle Temp 臨時表空間Oracle
- MySQL Delete PHPMySqldeletePHP
- WeakMap delete() 方法delete
- WeakSet delete() 方法delete
- Map delete() 方法delete