{"id":853,"date":"2018-10-06T17:54:18","date_gmt":"2018-10-06T10:54:18","guid":{"rendered":"http:\/\/www.sqlservertutorial.net\/?page_id=853"},"modified":"2020-04-11T20:13:09","modified_gmt":"2020-04-11T13:13:09","slug":"sql-server-table-variables","status":"publish","type":"page","link":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/","title":{"rendered":"SQL Server Table Variables"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn about the SQL Server table variables that hold rows of data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='what-are-table-variables'>What are table variables <a href=\"#what-are-table-variables\" class=\"anchor\" id=\"what-are-table-variables\" title=\"Anchor for What are table variables\">#<\/a><\/h2>\n\n\n\n<p>Table variables are kinds of variables that allow you to hold rows of data, which are similar to <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-temporary-tables\/\">temporary tables<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='how-to-declare-table-variables'>How to declare table variables <a href=\"#how-to-declare-table-variables\" class=\"anchor\" id=\"how-to-declare-table-variables\" title=\"Anchor for How to declare table variables\">#<\/a><\/h2>\n\n\n\n<p>To declare a table variable, you use the <code>DECLARE<\/code> statement as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @table_variable_name <span class=\"hljs-keyword\">TABLE<\/span> (\n    column_list\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, you specify the name of the table variable between the <code>DECLARE<\/code> and <code>TABLE<\/code> keywords. The name of the table variables must start with the <code>@<\/code> symbol.<\/p>\n\n\n\n<p>Following the <code>TABLE<\/code> keyword, you define the structure of the table variable which is similar to the structure of a regular table that includes column definitions, data type, size, optional constraint, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='the-scope-of-table-variables'>The scope of table variables <a href=\"#the-scope-of-table-variables\" class=\"anchor\" id=\"the-scope-of-table-variables\" title=\"Anchor for The scope of table variables\">#<\/a><\/h2>\n\n\n\n<p>Similar to local <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/variables\/\">variables<\/a>, table variables are out of scope at the end of the batch.<\/p>\n\n\n\n<p>If you define a table variable in a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/\">stored procedure<\/a> or <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/\">user-defined function<\/a>, the table variable will no longer exist after the stored procedure or user-defined function exits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='table-variable-example'>Table variable example <a href=\"#table-variable-example\" class=\"anchor\" id=\"table-variable-example\" title=\"Anchor for Table variable example\">#<\/a><\/h2>\n\n\n\n<p>For example, the following statement declares a table variable named <code>@product_table<\/code> which consists of three columns: <code>product_name<\/code>, <code>brand_id<\/code>, and <code>list_price<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @product_table <span class=\"hljs-keyword\">TABLE<\/span> (\n    product_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    brand_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    list_price <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">11<\/span>,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='inserting-data-into-the-table-variables'>Inserting data into the table variables <a href=\"#inserting-data-into-the-table-variables\" class=\"anchor\" id=\"inserting-data-into-the-table-variables\" title=\"Anchor for Inserting data into the table variables\">#<\/a><\/h3>\n\n\n\n<p>Once declared, the table variable is empty. You can insert rows into the table variables using the <code><a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-insert\/\">INSERT<\/a><\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> @product_table\n<span class=\"hljs-keyword\">SELECT<\/span>\n    product_name,\n    brand_id,\n    list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products\n<span class=\"hljs-keyword\">WHERE<\/span>\n    category_id = <span class=\"hljs-number\">1<\/span>;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='querying-data-from-the-table-variables'>Querying data from the table variables <a href=\"#querying-data-from-the-table-variables\" class=\"anchor\" id=\"querying-data-from-the-table-variables\" title=\"Anchor for Querying data from the table variables\">#<\/a><\/h3>\n\n\n\n<p>Similar to a temporary table, you can query data from the table variables using the <code>SELECT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    *\n<span class=\"hljs-keyword\">FROM<\/span>\n    @product_table;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Note that you need to execute the whole batch or you will get an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DECLARE<\/span> @product_table <span class=\"hljs-keyword\">TABLE<\/span> (\n    product_name <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    brand_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n    list_price <span class=\"hljs-built_in\">DEC<\/span>(<span class=\"hljs-number\">11<\/span>,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>\n);\n\n<span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> @product_table\n<span class=\"hljs-keyword\">SELECT<\/span>\n    product_name,\n    brand_id,\n    list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    production.products\n<span class=\"hljs-keyword\">WHERE<\/span>\n    category_id = <span class=\"hljs-number\">1<\/span>;\n\n<span class=\"hljs-keyword\">SELECT<\/span>\n    *\n<span class=\"hljs-keyword\">FROM<\/span>\n    @product_table;\nGO<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following picture shows the partial output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"340\" height=\"225\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example.png\" alt=\"SQL Server Table Variables Example\" class=\"wp-image-854\" srcset=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example.png 340w, https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example-300x199.png 300w\" sizes=\"auto, (max-width: 340px) 100vw, 340px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id='restrictions-on-table-variables'>Restrictions on table variables <a href=\"#restrictions-on-table-variables\" class=\"anchor\" id=\"restrictions-on-table-variables\" title=\"Anchor for Restrictions on table variables\">#<\/a><\/h2>\n\n\n\n<p>First, you have to define the structure of the table variable during the declaration. Unlike a regular or temporary table, you cannot <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-alter-table-alter-column\/\">alter<\/a> the structure of the table variables after they are declared.<\/p>\n\n\n\n<p>Second, statistics help the query optimizer to come up with a good query&#8217;s execution plan. Unfortunately, table variables do not contain statistics. Therefore, you should use table variables to hold a small number of rows.<\/p>\n\n\n\n<p>Third, you cannot use the table variable as an <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/stored-procedure-output-parameters\/\">input<\/a> or <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/stored-procedure-output-parameters\/\">output parameter<\/a> like other <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-data-types\/\">data types<\/a>. However, you can return a table variable from a user-defined function<\/p>\n\n\n\n<p>Fourth, you cannot create non-clustered indexes for table variables. However, starting with SQL Server 2014, memory-optimized table variables are available with the introduction of the new In-Memory OLTP that allows you to add non-clustered indexes as part of table variable&#8217;s declaration.<\/p>\n\n\n\n<p>Fifth, if you are using a table variable with a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-inner-join\/\">join<\/a>, you need to <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-alias\/\">alias<\/a> the table in order to execute the query. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span>\n    brand_name,\n    product_name,\n    list_price\n<span class=\"hljs-keyword\">FROM<\/span>\n    brands b\n<span class=\"hljs-keyword\">INNER<\/span> <span class=\"hljs-keyword\">JOIN<\/span> @product_table pt \n    <span class=\"hljs-keyword\">ON<\/span> p.brand_id = pt.brand_id;\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='performance-of-table-variables'>Performance of table variables <a href=\"#performance-of-table-variables\" class=\"anchor\" id=\"performance-of-table-variables\" title=\"Anchor for Performance of table variables\">#<\/a><\/h2>\n\n\n\n<p>Using table variables in a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-stored-procedures\/\">stored procedure<\/a> results in fewer recompilations than using a <a href=\"https:\/\/www.sqlservertutorial.net\/sql-server-basics\/sql-server-temporary-tables\/\">temporary table<\/a>.<\/p>\n\n\n\n<p>In addition, a table variable use fewer resources than a temporary table with less locking and logging overhead.<\/p>\n\n\n\n<p>Similar to the temporary table, the table variables do live in the <code>tempdb<\/code> database, not in the memory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='using-table-variables-in-user-defined-functions'>Using table variables in user-defined functions <a href=\"#using-table-variables-in-user-defined-functions\" class=\"anchor\" id=\"using-table-variables-in-user-defined-functions\" title=\"Anchor for Using table variables in user-defined functions\">#<\/a><\/h2>\n\n\n\n<p>The following user-defined function named <code>ufnSplit()<\/code>&nbsp;that returns a table variable.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">OR<\/span> <span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">FUNCTION<\/span> udfSplit(\n    @<span class=\"hljs-keyword\">string<\/span> <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>), \n    @delimiter <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">50<\/span>) = <span class=\"hljs-string\">' '<\/span>)\n<span class=\"hljs-keyword\">RETURNS<\/span> @parts <span class=\"hljs-keyword\">TABLE<\/span>\n(    \nidx <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\nval <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-keyword\">MAX<\/span>)   \n)\n<span class=\"hljs-keyword\">AS<\/span>\n<span class=\"hljs-keyword\">BEGIN<\/span>\n\n<span class=\"hljs-keyword\">DECLARE<\/span> @<span class=\"hljs-keyword\">index<\/span> <span class=\"hljs-built_in\">INT<\/span> = <span class=\"hljs-number\">-1<\/span>;\n\nWHILE (LEN(@string) &gt; 0) \n<span class=\"hljs-keyword\">BEGIN<\/span> \n    <span class=\"hljs-keyword\">SET<\/span> @<span class=\"hljs-keyword\">index<\/span> = <span class=\"hljs-keyword\">CHARINDEX<\/span>(@delimiter , @<span class=\"hljs-keyword\">string<\/span>)  ;\n    \n    IF (@index = 0) AND (LEN(@string) &gt; 0)  \n    <span class=\"hljs-keyword\">BEGIN<\/span>  \n        <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> @parts \n        <span class=\"hljs-keyword\">VALUES<\/span> (@<span class=\"hljs-keyword\">string<\/span>);\n        BREAK  \n    <span class=\"hljs-keyword\">END<\/span> \n\n    <span class=\"hljs-keyword\">IF<\/span> (@<span class=\"hljs-keyword\">index<\/span> &gt; <span class=\"hljs-number\">1<\/span>)  \n    <span class=\"hljs-keyword\">BEGIN<\/span>  \n        <span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> @parts \n        <span class=\"hljs-keyword\">VALUES<\/span> (<span class=\"hljs-keyword\">LEFT<\/span>(@<span class=\"hljs-keyword\">string<\/span>, @<span class=\"hljs-keyword\">index<\/span> - <span class=\"hljs-number\">1<\/span>));\n        \n        <span class=\"hljs-keyword\">SET<\/span> @<span class=\"hljs-keyword\">string<\/span> = <span class=\"hljs-keyword\">RIGHT<\/span>(@<span class=\"hljs-keyword\">string<\/span>, (<span class=\"hljs-keyword\">LEN<\/span>(@<span class=\"hljs-keyword\">string<\/span>) - @<span class=\"hljs-keyword\">index<\/span>));  \n    <span class=\"hljs-keyword\">END<\/span> \n    <span class=\"hljs-keyword\">ELSE<\/span>\n    <span class=\"hljs-keyword\">SET<\/span> @<span class=\"hljs-keyword\">string<\/span> = <span class=\"hljs-keyword\">RIGHT<\/span>(@<span class=\"hljs-keyword\">string<\/span>, (<span class=\"hljs-keyword\">LEN<\/span>(@<span class=\"hljs-keyword\">string<\/span>) - @<span class=\"hljs-keyword\">index<\/span>)); \n    <span class=\"hljs-keyword\">END<\/span>\n<span class=\"hljs-keyword\">RETURN<\/span>\n<span class=\"hljs-keyword\">END<\/span>\n<span class=\"hljs-keyword\">GO<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following statement calls the <code>udfSplit()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SELECT<\/span> \n    * \n<span class=\"hljs-keyword\">FROM<\/span> \n    udfSplit(<span class=\"hljs-string\">'foo,bar,baz'<\/span>,<span class=\"hljs-string\">','<\/span>);\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"71\" height=\"74\" src=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-user-defined-function-example.png\" alt=\"SQL Server Table Variables - user-defined function example\" class=\"wp-image-855\"\/><\/figure>\n\n\n\n<p>In this tutorial, you will learn how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.<\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"853\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/\"\n\t\t\t\tdata-post-title=\"SQL Server Table Variables\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"853\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/\"\n\t\t\t\tdata-post-title=\"SQL Server Table Variables\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial shows you how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":851,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-853","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>An Introduction to SQL Server Table Variables By Examples<\/title>\n<meta name=\"description\" content=\"This tutorial shows you how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"An Introduction to SQL Server Table Variables By Examples\" \/>\n<meta property=\"og:description\" content=\"This tutorial shows you how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Server Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-11T13:13:09+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/\",\"name\":\"An Introduction to SQL Server Table Variables By Examples\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-Table-Variables-Example.png\",\"datePublished\":\"2018-10-06T10:54:18+00:00\",\"dateModified\":\"2020-04-11T13:13:09+00:00\",\"description\":\"This tutorial shows you how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-Table-Variables-Example.png\",\"contentUrl\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/wp-content\\\/uploads\\\/SQL-Server-Table-Variables-Example.png\",\"width\":340,\"height\":225,\"caption\":\"SQL Server Table Variables Example\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/sql-server-table-variables\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server User-defined Functions\",\"item\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/sql-server-user-defined-functions\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"SQL Server Table Variables\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/#website\",\"url\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/\",\"name\":\"SQL Server Tutorial\",\"description\":\"The Practical SQL Server Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.sqlservertutorial.net\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"An Introduction to SQL Server Table Variables By Examples","description":"This tutorial shows you how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/","og_locale":"en_US","og_type":"article","og_title":"An Introduction to SQL Server Table Variables By Examples","og_description":"This tutorial shows you how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.","og_url":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/","og_site_name":"SQL Server Tutorial","article_modified_time":"2020-04-11T13:13:09+00:00","og_image":[{"url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example.png","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/","url":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/","name":"An Introduction to SQL Server Table Variables By Examples","isPartOf":{"@id":"https:\/\/www.sqlservertutorial.net\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/#primaryimage"},"image":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example.png","datePublished":"2018-10-06T10:54:18+00:00","dateModified":"2020-04-11T13:13:09+00:00","description":"This tutorial shows you how to use the SQL Server table variables which offer some performance benefits and flexibility in comparison with temporary tables.","breadcrumb":{"@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/#primaryimage","url":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example.png","contentUrl":"https:\/\/www.sqlservertutorial.net\/wp-content\/uploads\/SQL-Server-Table-Variables-Example.png","width":340,"height":225,"caption":"SQL Server Table Variables Example"},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/sql-server-table-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlservertutorial.net\/"},{"@type":"ListItem","position":2,"name":"SQL Server User-defined Functions","item":"https:\/\/www.sqlservertutorial.net\/sql-server-user-defined-functions\/"},{"@type":"ListItem","position":3,"name":"SQL Server Table Variables"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlservertutorial.net\/#website","url":"https:\/\/www.sqlservertutorial.net\/","name":"SQL Server Tutorial","description":"The Practical SQL Server Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlservertutorial.net\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/853","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/comments?post=853"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/853\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/pages\/851"}],"wp:attachment":[{"href":"https:\/\/www.sqlservertutorial.net\/wp-json\/wp\/v2\/media?parent=853"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}