{"id":26491,"date":"2023-06-06T06:36:00","date_gmt":"2023-06-06T04:36:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=26491"},"modified":"2023-08-08T16:42:34","modified_gmt":"2023-08-08T14:42:34","slug":"sql-server-variables","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/","title":{"rendered":"How to use SQL Server Variables?"},"content":{"rendered":"\n<p>Managing data efficiently is at the core of any well-structured database system. When working with Microsoft SQL Server, an integral part of this management is the effective use of SQL Server variables. These variables allow for the temporary storage of data that can be manipulated or used throughout your session. In this tutorial, we will explore how to declare, assign, and manipulate SQL Server variables using a practical example. Before diving into variables, we&#8217;ll start by creating and populating a sample table that we&#8217;ll use throughout this tutorial.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#1-create-and-populate-the-sample-table\" >1. Create and Populate the Sample Table<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#2-declare-sql-server-variables\" >2. Declare SQL Server Variables<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#3-assign-a-direct-value-to-a-sql-variable\" >3. Assign a Direct Value to a SQL variable<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#4-assign-one-value-from-a-select-statement\" >4. Assign one value from a SELECT statement<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#5-assign-multiple-values-from-a-select\" >5. Assign multiple values from a SELECT<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#6-assign-values-using-select-into\" >6. Assign values using SELECT INTO<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#7-using-variables-in-t-sql-queries\" >7. Using Variables in T-SQL Queries<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-create-and-populate-the-sample-table\"><span class=\"ez-toc-section\" id=\"1-create-and-populate-the-sample-table\"><\/span>1. Create and Populate the Sample Table<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Begin by creating a new table named &#8216;Employees&#8217;. This table will have three columns: &#8216;EmployeeID&#8217;, &#8216;FirstName&#8217;, and &#8216;LastName&#8217;. To create this table, execute the following SQL statement:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">CREATE TABLE Employees (\n    EmployeeID INT PRIMARY KEY,\n    FirstName NVARCHAR(50),\n    LastName NVARCHAR(50)\n);\n<\/pre>\n\n\n\n<p>After the creation of the &#8216;Employees&#8217; table, you can populate it with some sample data. This data will help illustrate how variables can be used in SQL Server.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">INSERT INTO Employees (EmployeeID, FirstName, LastName) \nVALUES \n(1, 'John', 'Doe'),\n(2, 'Jane', 'Doe'),\n(3, 'James', 'Smith');\n<\/pre>\n\n\n\n<p>You now have a table filled with data, ready for manipulation with SQL Server variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-2-declare-sql-server-variables\"><span class=\"ez-toc-section\" id=\"2-declare-sql-server-variables\"><\/span>2. Declare SQL Server Variables<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To declare a variable in SQL Server, use the DECLARE statement. Variables start with an &#8216;@&#8217; symbol. For instance, to declare a variable that can hold an integer value, you could write:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @EmployeeCount INT;\n<\/pre>\n\n\n\n<p>This statement declares a variable named &#8216;@EmployeeCount&#8217; that can hold an integer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-assign-a-direct-value-to-a-sql-variable\"><\/span>3. Assign a Direct Value to a SQL variable<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In the previous section, we assigned a simple value to a SQL variable using the SET statement and a SELECT query. MS SQL, however, offers a variety of ways to assign values to variables. After declaring a variable, assign a value to it using the SET statement.<\/p>\n\n\n\n<p>You can assign a direct value to a variable. This is useful when the value you want to store doesn&#8217;t need to be derived from a SQL query.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @Greeting NVARCHAR(50);\nSET @Greeting = 'Hello, SQL Server!';\nPRINT @Greeting;\n<\/pre>\n\n\n\n<p>In this example, the string &#8216;Hello, SQL Server!&#8217; is directly assigned to the @Greeting object.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-assign-one-value-from-a-select-statement\"><\/span>4. Assign one value from a SELECT statement<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>As demonstrated earlier, you can assign a value to a variable based on a SQL query&#8217;s result. The value from the query is set as the variable&#8217;s value.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @OldestEmployeeID INT;\nSET @OldestEmployeeID = (SELECT TOP 1 EmployeeID FROM Employees ORDER BY DateOfBirth ASC);\nPRINT @OldestEmployeeID;\n<\/pre>\n\n\n\n<p>Here, the variable @OldestEmployeeID is set to the ID of the oldest employee in the &#8216;Employees&#8217; table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-assign-multiple-values-from-a-select\"><\/span>5. Assign multiple values from a SELECT<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>SQL Server allows you to assign values to multiple variables using a single SELECT statement.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @MinSalary DECIMAL(18, 2), @MaxSalary DECIMAL(18, 2);\nSELECT @MinSalary = MIN(Salary), @MaxSalary = MAX(Salary) FROM Salaries;\nPRINT 'Minimum salary: ' + CAST(@MinSalary AS NVARCHAR(20));\nPRINT 'Maximum salary: ' + CAST(@MaxSalary AS NVARCHAR(20));\n<\/pre>\n\n\n\n<p>This assigns the minimum and maximum salary from the &#8216;Salaries&#8217; table to the @MinSalary and @MaxSalary variables, respectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6-assign-values-using-select-into\"><\/span>6. Assign values using SELECT INTO<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>While SELECT INTO is often used to create a new table from the result set of a SELECT statement, you can also use it to assign a value to a variable.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">DECLARE @AverageSalary DECIMAL(18, 2);\nSELECT @AverageSalary = AVG(Salary) FROM Salaries INTO @AverageSalary;\nPRINT 'Average salary: ' + CAST(@AverageSalary AS NVARCHAR(20));\n<\/pre>\n\n\n\n<p>In this case, the average salary from the &#8216;Salaries&#8217; table is assigned to the @AverageSalary variable.<\/p>\n\n\n\n<p>By exploring these various ways to assign values, you can gain flexibility in how you manipulate and use data with SQL Server variables.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-7-using-variables-in-t-sql-queries\"><span class=\"ez-toc-section\" id=\"7-using-variables-in-t-sql-queries\"><\/span>7. Using Variables in T-SQL Queries<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>SQL Server variables can be used in any <a href=\"https:\/\/en.wikipedia.org\/wiki\/SQL\" target=\"_blank\" rel=\"noreferrer noopener\">SQL statement<\/a> or stored procedure. For example, to display the total number of employees, use the PRINT statement:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">PRINT 'The total number of employees is: ' + CAST(@EmployeeCount AS NVARCHAR(10));\n<\/pre>\n\n\n\n<p>This statement converts the @EmployeeCount variable to a string and concatenates it with a message, resulting in a string that provides the total number of employees.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-conclusion\">Conclusion<\/h3>\n\n\n\n<p>In this tutorial, you learned how to create and populate a sample table in SQL Server. More importantly, you learned how to declare, assign, and use SQL Server variables. This basic understanding of variables enables you to write more dynamic and efficient SQL scripts.<\/p>\n\n\n\n<p>Remember, variables offer a powerful way to temporarily store and manipulate data within a session. Combined with <a href=\"https:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-temporary-tables\/\">SQL temporary tables<\/a> and <a href=\"https:\/\/expert-only.com\/en\/t-sql\/script-create-view-sql-server\/\">views<\/a>, they offer a lot of flexibility to manipulate and represent the data. As you become more comfortable with them, you will find they are invaluable tools in your SQL Server toolkit.<\/p>\n\n\n\n<figure class=\"wp-block-embed aligncenter is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"nZ74qdCESf\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-temporary-tables\/\">Manage SQL Server temporary tables using T-SQL code<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Manage SQL Server temporary tables using T-SQL code&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/manage-sql-server-temporary-tables\/embed\/#?secret=1eQm5TacRi#?secret=nZ74qdCESf\" data-secret=\"nZ74qdCESf\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Managing data efficiently is at the core of any well-structured database system. When working with Microsoft SQL Server, an integral part of this management is the effective use of SQL Server variables. These variables allow for the temporary storage <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\" title=\"How to use SQL Server Variables?\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10619,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-26491","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use SQL Server Variables? T-SQL<\/title>\n<meta name=\"description\" content=\"Master SQL Server Variables with our tutorial to create, populate tables, declare, assign and manipulate variables with practical examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use SQL Server Variables?\" \/>\n<meta property=\"og:description\" content=\"Master SQL Server Variables with our tutorial to create, populate tables, declare, assign and manipulate variables with practical examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-06-06T04:36:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-08-08T14:42:34+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"How to use SQL Server Variables?\",\"datePublished\":\"2023-06-06T04:36:00+00:00\",\"dateModified\":\"2023-08-08T14:42:34+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\"},\"wordCount\":647,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg\",\"articleSection\":[\"T-SQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\",\"url\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\",\"name\":\"How to use SQL Server Variables? T-SQL\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg\",\"datePublished\":\"2023-06-06T04:36:00+00:00\",\"dateModified\":\"2023-08-08T14:42:34+00:00\",\"description\":\"Master SQL Server Variables with our tutorial to create, populate tables, declare, assign and manipulate variables with practical examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use SQL Server Variables?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to use SQL Server Variables? T-SQL","description":"Master SQL Server Variables with our tutorial to create, populate tables, declare, assign and manipulate variables with practical examples.","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:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/","og_locale":"en_US","og_type":"article","og_title":"How to use SQL Server Variables?","og_description":"Master SQL Server Variables with our tutorial to create, populate tables, declare, assign and manipulate variables with practical examples.","og_url":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2023-06-06T04:36:00+00:00","article_modified_time":"2023-08-08T14:42:34+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"How to use SQL Server Variables?","datePublished":"2023-06-06T04:36:00+00:00","dateModified":"2023-08-08T14:42:34+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/"},"wordCount":647,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg","articleSection":["T-SQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/","url":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/","name":"How to use SQL Server Variables? T-SQL","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg","datePublished":"2023-06-06T04:36:00+00:00","dateModified":"2023-08-08T14:42:34+00:00","description":"Master SQL Server Variables with our tutorial to create, populate tables, declare, assign and manipulate variables with practical examples.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/digitization-FD44BBF7CF9_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-variables\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"How to use SQL Server Variables?"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/26491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=26491"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/26491\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10619"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=26491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=26491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=26491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}