{"id":370,"date":"2019-08-02T13:33:32","date_gmt":"2019-08-02T08:03:32","guid":{"rendered":"https:\/\/techieshouts.com\/?p=370"},"modified":"2022-08-09T19:06:56","modified_gmt":"2022-08-09T13:36:56","slug":"table-variables-in-ms-sql-server","status":"publish","type":"post","link":"https:\/\/techieshouts.com\/home\/table-variables-in-ms-sql-server\/","title":{"rendered":"Table Variables in MS SQL Server"},"content":{"rendered":"\n<p>Table variables in SQL Server are variables that store a set of records in a table like structure for a particular period of time. It is another way of storing data temporarily similar to <a href=\"https:\/\/techieshouts.com\/temporary-tables-in-ms-sql-server\/\">temporary tables<\/a>. In this article, we will see what is table variable with an example.<\/p>\n\n\n\n<p>Generally, the name of the table variable should start with &#8216;@&#8217; and followed by a meaningful name. Declaring the table variable just like declaring the local variables in SQL Server.<\/p>\n\n\n\n<h2>Table Variable Syntax<\/h2>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">DECLARE @TableVariableName TABLE \n(\n    column1 datatype, \n    column2 datatype\n);<\/pre>\n\n\n\n<p>For example, let&#8217;s populate\u00a0the list of Indian players from the table &#8216;tblPlayer&#8217;. <\/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\">DECLARE @PlayerDetails TABLE \r\n(playerID INT NOT NULL, playerName VARCHAR(50) NOT NULL);\r\n\r\nINSERT INTO @PlayerDetails\r\nSELECT playerID, FirstName\r\nFROM tblPlayer\r\nWHERE countryID = 1;\r\n\r\nSELECT * FROM @PlayerDetails;<\/pre>\n\n\n\n<p>Queries in the above code snippet, declares the table variable &#8216;@PlayerDetails&#8217; with two columns, inserts the data into it and retrieves the data from it.<\/p>\n\n\n\n<h2>Scope<\/h2>\n\n\n\n<p>The scope of the table variable ends as soon as it exits the batch of SQL statements. A function or stored procedure which contains table variable ends the scope once the execution is completed. <\/p>\n\n\n\n<h2>Limitations<\/h2>\n\n\n\n<ul><li>Define the structure of the table variable during the declaration. It is not possible to alter the structure of it after the declaration. <\/li><li>Unlike other data types in SQL server, you cannot use table variable as an input or output parameter.<\/li><li>It is also important to note that the table variables do not participate in transactions. Although we use rollback command in the below code snippet, the select query returns two records.<\/li><\/ul>\n\n\n\n<pre data-mode=\"sqlserver\" data-theme=\"dreamweaver\" data-fontsize=\"14\" data-lines=\"Infinity\" class=\"wp-block-simple-code-block-ace\">DECLARE @PlayerDetails TABLE \r\n(playerID INT NOT NULL, playerName VARCHAR(50) NOT NULL);\r\n\r\nBEGIN TRAN T1\r\nINSERT INTO @PlayerDetails VALUES (1, 'Sachin'), (2, 'Shewag')\r\nROLLBACK TRAN T1\r\n\r\nSELECT * FROM @PlayerDetails;<\/pre>\n\n\n\n<h2><strong>Difference between table variable and\ntemp table<\/strong><\/h2>\n\n\n\n<table class=\"wp-block-table aligncenter\"><tbody><tr><td>\n  <strong>Table Variable<\/strong>\n  <\/td><td>\n  <strong>Temp Table<\/strong>\n  <\/td><\/tr><tr><td>Declare the table variable with TABLE keyword which stores records.   \u00a0   \u00a0   <\/td><td>Create the temp table using CREATE TABLE or SELECT INTO statement with a prefix # or ## as a table name.   <\/td><\/tr><tr><td>It is not possible to alter table variable after declaration.   <\/td><td>It is possible to alter temp tables structure after creating it.   <\/td><\/tr><tr><td>Scope of table variable ends after the execution of batch.   <\/td><td>Scope of the temp table is based on the type and it ends when the connection is closed.   <\/td><\/tr><tr><td>Table variable can\u2019t be dropped.   <\/td><td>Temp table can be dropped automatically or manually.   <\/td><\/tr><tr><td> Table variable can be used in functions.   <\/td><td>Temp table can\u2019t be used in functions.   <\/td><\/tr><tr><td>Table variable does not participate in transactions.   <\/td><td>Temp table can participate in transactions.   <\/td><\/tr><\/tbody><\/table>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table variables in SQL Server are variables that store a set of records in a table like structure for a particular period of time. It is another way of storing data temporarily similar to temporary tables. In this article, we will see what is table variable with an example. Generally, the name of the table\u2026 <span class=\"read-more\"><a href=\"https:\/\/techieshouts.com\/home\/table-variables-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,56],"_links":{"self":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/370"}],"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=370"}],"version-history":[{"count":2,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/370\/revisions"}],"predecessor-version":[{"id":605,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/posts\/370\/revisions\/605"}],"wp:attachment":[{"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/media?parent=370"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/categories?post=370"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techieshouts.com\/home\/wp-json\/wp\/v2\/tags?post=370"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}