{"id":362,"date":"2019-08-02T13:21:06","date_gmt":"2019-08-02T07:51:06","guid":{"rendered":"https:\/\/techieshouts.com\/?p=362"},"modified":"2022-08-09T19:07:02","modified_gmt":"2022-08-09T13:37:02","slug":"temporary-tables-in-ms-sql-server","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/temporary-tables-in-ms-sql-server\/","title":{"rendered":"Temporary Tables in MS SQL Server"},"content":{"rendered":"\n<p>A temporary table in SQL Server is a table that exists temporarily on the database server for a certain period of time. Temporary tables are basically stored inside the \u201ctempdb\u201d, which is a system database.\u00a0<\/p>\n\n\n\n<p>Temporary tables are useful to work with a small subset of records in a table that contains a large number of records. Instead of processing a small set of data again and again from a large table, filter and store it once in a temporary table.<\/p>\n\n\n\n<h2><strong>Types of temporary tables<\/strong><\/h2>\n\n\n\n<p>The two types of temporary tables are local and global temporary tables.<\/p>\n\n\n\n<h2><strong>Local Temporary Table<\/strong><\/h2>\n\n\n\n<p>The local temporary table has a pound sign (#) as the first character of the name. Creating a temp table is similar to creating a permanent table with a pound sign as a prefix in the table name. You can create the table using a <a href=\"https:\/\/techieshouts.com\/create-table-in-sql-server\/\">CREATE TABLE<\/a> statement or <a href=\"https:\/\/techieshouts.com\/creating-table-using-select-into\/\">SELECT INTO<\/a> statement. Let&#8217;s see an example to get the list of players belongs to India and store it in a temp table.<\/p>\n\n\n\n<h3>Method 1<\/h3>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">CREATE TABLE #PlayerDetails \n(\n    playerID INT, \n    playerName VARCHAR(50), \n    country VARCHAR(25)\n);\n\nINSERT INTO #PlayerDetails \nSELECT playerID, FirstName, countryID  \nFROM tblPlayer  \nWHERE countryID = 1 --India<\/pre>\n\n\n\n<h3>Method 2 <\/h3>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">SELECT \n    playerID, \n    FirstName \nINTO #PlayerDetails  \nFROM tblPlayer  \nWHERE countryID = 1 --India<\/pre>\n\n\n\n<p>Any one of the above queries creates the local temporary table &#8216;#PlayerDetails&#8217; in &#8216;tempdb&#8217;.\u00a0To verify the created temp table in the server, expand &#8216;tempdb&#8217; under system database in the object explorer window. See the below image.\u00a0\u00a0 <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" width=\"1003\" height=\"336\" src=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/image.png\" alt=\"Local temporary table has a unique identifier\" class=\"wp-image-366\" srcset=\"https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/image.png 1003w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/image-300x100.png 300w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/image-768x257.png 768w, https:\/\/techieshouts.com\/wp-content\/uploads\/2019\/08\/image-660x221.png 660w\" sizes=\"(max-width: 1003px) 100vw, 1003px\" \/><figcaption>Local temporary table<\/figcaption><\/figure>\n\n\n\n<p>In the above screen, notice that the temp table name has some values at the end. It is the unique identifier and SQL server generates it automatically. This is because the multiple database connections can create the same temp table. To differentiate the temp tables created by different connections, the unique identifier is appended at the end of the table name.\u00a0 <\/p>\n\n\n\n<p>The scope of the local temp table is available only for the connection that has created the table. You should perform the DDL\/ DML operations on the temp table in the same connection. When you close the connection that creates the temp table, it drops automatically. You can also drop the temp table manually by using DROP TABLE statement.<\/p>\n\n\n\n<h2><strong>Global Temporary Table<\/strong>&nbsp;<\/h2>\n\n\n\n<p>The global temporary table has two-pound signs (##) as the first character of the name.<\/p>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">SELECT \n    playerID, \n    FirstName \nINTO ##PlayerDetails  \nFROM tblPlayer  \nWHERE countryID = 1 --India <\/pre>\n\n\n\n<p>In contrast to local temporary table, the scope of the global temp table is available to all the open connections. Since it can be accessed by all connections, no unique identifier is added at the end of the table name.<\/p>\n\n\n\n<p>When you close the last connection that references the temp table, it drops automatically. You can also drop the temp table manually by using DROP TABLE statement. <\/p>\n\n\n\n<p>Like permanent tables, temp tables can have constraints and indexes.&nbsp;<\/p>\n\n\n\n<h2><strong>Difference between local and global temp table<\/strong><\/h2>\n\n\n\n<table class=\"wp-block-table\"><tbody><tr><td><strong>Local Temp Table<\/strong>&nbsp;<\/td><td><strong>Global Temp Table<\/strong>&nbsp;<\/td><\/tr><tr><td>The table name has the single pound \u2018#\u2019 sign as the prefix.&nbsp;<\/td><td>The table name has the double pound \u2018##\u2019 sign as the prefix.&nbsp;<\/td><\/tr><tr><td>A unique identifier is appended at the end of the table name.&nbsp;<\/td><td>No unique identifier is appended to the table name.&nbsp;<\/td><\/tr><tr><td>The scope of the table is available only for the connection.&nbsp;<\/td><td>The scope of the table is available for all the open connections.&nbsp;<\/td><\/tr><tr><td>The table is dropped automatically when the connection that created the table is closed.&nbsp;<\/td><td>The table is dropped when the last connection that references it is closed.&nbsp;<\/td><\/tr><\/tbody><\/table>\n\n\n\n<p>\n\nThe limitation of the temp table is that the user-defined function cannot contain the temp tables.\n\n<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A temporary table in SQL Server is a table that exists temporarily on the database server for a certain period of time. Temporary tables are basically stored inside the \u201ctempdb\u201d, which is a system database.\u00a0 Temporary tables are useful to work with a small subset of records in a table that contains a large number\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/temporary-tables-in-ms-sql-server\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10,15],"tags":[39,40,41,57],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/362"}],"collection":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/comments?post=362"}],"version-history":[{"count":5,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/362\/revisions"}],"predecessor-version":[{"id":603,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/362\/revisions\/603"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}