{"id":10304,"date":"2019-09-27T00:00:00","date_gmt":"2019-09-27T05:00:00","guid":{"rendered":"https:\/\/threecloud.wpengine.com\/post\/global-temporary-tables-in-azure-sql-data-warehouse-2\/"},"modified":"2022-11-30T09:25:21","modified_gmt":"2022-11-30T15:25:21","slug":"global-temporary-tables-in-azure-sql-data-warehouse","status":"publish","type":"post","link":"https:\/\/3cloudsolutions.com\/resources\/global-temporary-tables-in-azure-sql-data-warehouse\/","title":{"rendered":"Global Temporary Tables in Azure SQL Data Warehouse"},"content":{"rendered":"<p>Azure SQL Data Warehouse (DW) has been improving its capabilities day-by-day. This relational database platform used by Microsoft is known to be faster and cheaper than other major cloud data warehouse solutions.<\/p>\n<\/p>\n<p><strong>In this post, I will discuss a consideration for migrating databases from SQL Server on-prem or Azure SQL DB to Azure SQL Data Warehouse as it relates to global temporary tables.<\/strong><\/p>\n<p><strong>As you may know, with SQL Server on-prem or Azure SQL DB, local temp tables have one pound sign (#) as a part of the table\u2019s name, #Products_az for example.<\/strong>\u00a0 Local temp tables are visible only to the connection used to create it and are deleted after the connection is closed.<\/p>\n<p><strong>A global table will have two pound signs, ##Products_az for example.<\/strong>\u00a0 Global temp tables are visible to any connection.\u00a0 A global temp table is deleted automatically when the last connection that references it has closed.<\/p>\n<p><strong>You may have code in a stored procedure that creates a global temporary table in SQL Server on-prem or Azure SQL DB.\u00a0 The code to create a global temp table will execute in Azure SQL DW without error to ostensibly create a table named ##Products_az, for example.\u00a0 However, any subsequent code that queries ##Products_az will fail. Since global temp tables are not supported in Azure SQL DW, the code to create the table really created a local temp table with a name beginning with a single #.\u00a0 The \u201c##\u201d table is not created.<\/strong><\/p>\n<p><strong>Let\u2019s look at an example in Management Studio:<\/strong><\/p>\n<ul>\n<li>Open a query window on a connection to an Azure SQL DB.<\/li>\n<li>Execute SQL to create a new global temp table loaded with sample data.<\/li>\n<li>Query the global temp table. Verify the global temporary table was created and contains data.<\/li>\n<li>Next, open a new session and check that that table is available in this new session.\u00a0 Successfully querying this global temp table from another connection verifies this functionality on Azure SQL DB.<\/li>\n<\/ul>\n<p>Now here\u2019s where the problem comes in. <strong>At this time, Azure SQL DW only supports local tables. So, what will happen if we try to create a global temporary table in Azure SQL DW?<\/strong><\/p>\n<ul>\n<li>Open a query window on a connection to an Azure SQL DW<\/li>\n<li>Using the same code from earlier, execute SQL to create a new global temp table loaded with sample data<\/li>\n<li>Querying the global temporary table fails!<\/li>\n<\/ul>\n<p><strong>The error is occurring because Microsoft SQL Server is still considering this a local temporary table because it looks for the first pound sign while the second pound sign is ignored and consider part of the name.<\/strong><\/p>\n<p>This is a problem if you are trying to do a migration of hundreds of stored procedures because you will be able to batch, compile and even run without any errors. You will not run into an error until a second stored procedure or process tries to use that table which is quite a big issue.<\/p>\n<p><strong>It\u2019s important to keep in mind that global temporary tables are not supported in Azure SQL Data Warehouse.<\/strong><\/p>\n<div class=\"hs-responsive-embed-wrapper hs-responsive-embed\" style=\"width: 100%; height: auto; position: relative; overflow: hidden; padding: 0; max-width: 560px; max-height: 315px; min-width: 256px; margin: 0px auto; display: block;\">\n<div class=\"hs-responsive-embed-inner-wrapper\" style=\"position: relative; overflow: hidden; max-width: 100%; padding-bottom: 56.25%; margin: 0;\"><iframe loading=\"lazy\" src=\"https:\/\/www.youtube.com\/embed\/lMXxWbv0pGs\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/div>\n<\/div>\n<p>I hope these tips will help you to know what to consider when creating local and global tables in Azure SQL Data Warehouse.<\/p>\n<p><strong>Need further help? Our expert team and solution offerings can help your business with any Azure product or service, including Managed Services offerings. Contact us at 888-8AZURE or\u00a0 <a href=\"mailto:sales@3cloudsolutions.com\">sales@3cloudsolutions.com<\/a>.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Azure SQL Data Warehouse (DW) has been improving its capabilities day-by-day. This relational database platform&mldr;<\/p>\n","protected":false},"author":38,"featured_media":9296,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":""},"categories":[260],"tags":[],"class_list":["post-10304","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-ai","topics-blog"],"acf":[],"_links":{"self":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/10304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/users\/38"}],"replies":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/comments?post=10304"}],"version-history":[{"count":0,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/posts\/10304\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media\/9296"}],"wp:attachment":[{"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/media?parent=10304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/categories?post=10304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/3cloudsolutions.com\/wp-json\/wp\/v2\/tags?post=10304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}