{"id":18878,"date":"2022-06-08T07:12:00","date_gmt":"2022-06-08T05:12:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=18878"},"modified":"2022-11-30T12:01:51","modified_gmt":"2022-11-30T11:01:51","slug":"sql-server-insert-into-with-variables","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/sql-server-insert-into-with-variables\/","title":{"rendered":"SQL Server INSERT INTO with variables to manage dynamic inserts"},"content":{"rendered":"\n<h4 class=\"has-text-align-center wp-block-heading\" id=\"h-use-the-sql-server-insert-into-query-with-variables-to-insert-data-dynamically\"><strong><em>Use the SQL Server Insert Into query with variables to insert data dynamically.<\/em><\/strong><\/h4>\n\n\n\n<p>How to build a SQL Server INSERT INTO statement with variables to make the code dynamic? This tutorial is like the first in the series for inserting data with SQL Server from a select query. To do this, modify the code slightly to create the table and use variables in the query instead of hard coding the values to be inserted.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. SQL INSERT INTO query with T-SQL variables<\/h2>\n\n\n\n<p>This tutorial shows how to use variables in a query to insert data into a SQL Server table, reuse the code in this Insert Into example from a SELECT query, and add T-SQL variables to it.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Create the SQL table to insert the data<\/h2>\n\n\n\n<p>First, create the Customers table with the NAME column declared as unique so that the same customer is not inserted twice. The customer number NOCLIENT is auto incremented to have a continuous list of customer numbers.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">IF EXISTS(\n  SELECT 1 FROM sys.objects\n  WHERE object_id = object_id(N'[dbo].[CLIENTS]')\n  AND type in (N'U')\n)\nDROP TABLE [dbo].[CLIENTS];\nGO\n\nCREATE TABLE [dbo].[CLIENTS] (\n  [NOCLIENT] int IDENTITY(1,1),\n  [NAME] nvarchar(20) UNIQUE,\n  [CITY] nvarchar(20)\n);\nGO<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">3. INSERT INTO query to insert data with variables<\/h2>\n\n\n\n<p>Indeed, in a block of T-SQL code, follow these two simple steps. Declare a variable for each column. Each of the data types declared in the variables corresponds to the data types of the columns. Then insert the data with an <a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-insert-into-from-a-select\/\">INSERT INTO query<\/a> that uses the two variables declared at the beginning of the code.<br><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"mssql\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-- Declare the variables\nDECLARE @NAME NVARCHAR(20);\nDECLARE @CITY NVARCHAR(20);\n\n-- First insertion\nSET @NAME = 'MAMMADOU';\nSET @CITY = 'Lyon';\nINSERT INTO dbo.CLIENTS ( NAME, CITY ) VALUES ( @NAME, @CITY );\n\n-- Second insertion\nSET @NAME = 'AHMED';\nSET @CITY = 'Paris';\nINSERT INTO dbo.CLIENTS ( NAME, CITY ) VALUES ( @NAME, @CITY );\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">4. Advantages of variables in T-SQL development<\/h2>\n\n\n\n<p>The use of T-SQL variables is useful to reuse the value of the variable several times. To avoid copying and pasting the same values in a script and therefore to factorise the objects as much as possible. Making T-SQL <a href=\"https:\/\/en.wikipedia.org\/wiki\/Dynamic_programming\" target=\"_blank\" rel=\"noreferrer noopener\">code dynamic<\/a> is a good practice, highly recommended, it is notably the main purpose of SQL Server functions and stored procedures.<\/p>\n\n\n\n<p>This article explains how to build and execute a dynamic SQL Server script with an INSERT INTO command and variables. And thus, insert data in a table via variables instantiated at the beginning of the script, or which evolve during the course of the script. Finally, to go further, here is a tutorial to <a href=\"https:\/\/expert-only.com\/en\/t-sql\/update-same-column-another-line-sql\/\">update the same column of another row in a SQL Server table<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed 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=\"ub8f3d9PcT\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/update-same-column-another-line-sql\/\">Update the same column of another line with SQL Server<\/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;Update the same column of another line with SQL Server&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/update-same-column-another-line-sql\/embed\/#?secret=KOInZeO0kB#?secret=ub8f3d9PcT\" data-secret=\"ub8f3d9PcT\" 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>Use the SQL Server Insert Into query with variables to insert data dynamically. How to build a SQL Server INSERT INTO statement with variables to make the code dynamic? This tutorial is like the first in the series for <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-insert-into-with-variables\/\" title=\"SQL Server INSERT INTO with variables to manage dynamic inserts\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10271,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-18878","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-t-sql"},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/18878","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=18878"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/18878\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10271"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=18878"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=18878"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=18878"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}