{"id":251,"date":"2023-11-21T17:23:45","date_gmt":"2023-11-21T16:23:45","guid":{"rendered":"https:\/\/bitwise.exposed\/?p=251"},"modified":"2026-03-16T09:38:48","modified_gmt":"2026-03-16T08:38:48","slug":"create-temptable-in-t-sql-with-select-into","status":"publish","type":"post","link":"https:\/\/bitwise.exposed\/index.php\/2023\/11\/21\/create-temptable-in-t-sql-with-select-into\/","title":{"rendered":"Create #TempTable with T-SQL"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Temporary tables in SQL are used to store and process intermediate results within a session. They are particularly useful when you need to store temporary data that you want to manipulate or join with other tables. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also for developing queries in complex and large databases, they could be really useful. For example, in a staging area where views build on each other, queries could be quite slow. Persist views in TempTables during a session could be a practical solution. Also for readability and what ever comes in mind. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There&#8217;s more than one way in SQL to create a temporary table, but Microsoft SQL Server (T-SQL) offers a convenient way with the &#8218;INTO&#8216; statement to create a temporary table and populate it with the results of a query in a single statement. This is often referred to as a &#8217;select into&#8216; operation. <br><br>Here&#8217;s an example using a local temporary table:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">SQL<\/span><span role=\"button\" tabindex=\"0\" data-code=\"-- Create and populate a local temporary table using SELECT INTO\nSELECT Column1, Column2\nINTO #TempTable\nFROM YourSourceTable\nWHERE SomeCondition;\n\n-- Query the temporary table\nSELECT * FROM #TempTable;\n\n-- Drop the temporary table (it will be dropped automatically when the session ends)\nDROP TABLE #TempTable;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">-- Create and populate a local temporary table using SELECT INTO<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> Column1, Column2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">INTO<\/span><span style=\"color: #000000\"> #TempTable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> YourSourceTable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">WHERE<\/span><span style=\"color: #000000\"> SomeCondition;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">-- Query the temporary table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> * <\/span><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> #TempTable;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">-- Drop the temporary table (it will be dropped automatically when the session ends)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">DROP<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">TABLE<\/span><span style=\"color: #000000\"> #TempTable;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>YourSourceTable<\/strong> is the source table from which you want to select data.<\/li>\n\n\n\n<li><strong>SomeCondition<\/strong> is a condition that filters the data you want to insert into the temporary table.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>&#8218;SELECT INTO&#8216; <\/strong>statement creates the <strong>&#8218;#TempTable&#8216;<\/strong> if it doesn&#8217;t exist and inserts the result set of the SELECT query into it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you want to create a global temporary table, you can use the <strong>&#8218;##&#8216;<\/strong> prefix:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">SQL<\/span><span role=\"button\" tabindex=\"0\" data-code=\"-- Create and populate a global temporary table using SELECT INTO\nSELECT Column1, Column2\nINTO ##GlobalTempTable\nFROM YourSourceTable\nWHERE SomeCondition;\n\n-- Query the global temporary table\nSELECT * FROM ##GlobalTempTable;\n\n-- Drop the global temporary table (it will be dropped automatically when the session ends)\nDROP TABLE ##GlobalTempTable;\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\">-- Create and populate a global temporary table using SELECT INTO<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> Column1, Column2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">INTO<\/span><span style=\"color: #000000\"> ##GlobalTempTable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> YourSourceTable<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">WHERE<\/span><span style=\"color: #000000\"> SomeCondition;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">-- Query the global temporary table<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">SELECT<\/span><span style=\"color: #000000\"> * <\/span><span style=\"color: #0000FF\">FROM<\/span><span style=\"color: #000000\"> ##GlobalTempTable;<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\">-- Drop the global temporary table (it will be dropped automatically when the session ends)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #0000FF\">DROP<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000FF\">TABLE<\/span><span style=\"color: #000000\"> ##GlobalTempTable;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong> The examples above refers to T-SQL used in Microsoft SQL Server.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Microsoft SQL Server (T-SQL) offers a convenient way with the &#8218;INTO&#8216; statement to create a temporary table and populate it with the results of a query in a single statement.<\/p>\n","protected":false},"author":1,"featured_media":254,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,17],"tags":[],"class_list":["post-251","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-pattern","category-sql"],"_links":{"self":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/251","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/comments?post=251"}],"version-history":[{"count":6,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/251\/revisions"}],"predecessor-version":[{"id":274,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/251\/revisions\/274"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media\/254"}],"wp:attachment":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media?parent=251"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/categories?post=251"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/tags?post=251"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}