{"id":29032,"date":"2023-10-09T06:38:00","date_gmt":"2023-10-09T04:38:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=29032"},"modified":"2023-11-13T19:54:29","modified_gmt":"2023-11-13T18:54:29","slug":"table-valued-parameter-stored-procedure","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/t-sql\/table-valued-parameter-stored-procedure\/","title":{"rendered":"Use a table parameter in a Stored Procedure in SQL Server"},"content":{"rendered":"\n<p>In this T-SQL guide, we&#8217;ll explore how to use table-valued parameters in a SQL Server stored procedure. It  enables the passing of a table as a parameter and mot only its values like by using a select statement for example. Thus, this method is particularly useful for scenarios where you need to pass multiple rows of data to a stored procedure without having to pass too many variables.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-differences-between-output-parameters-and-table-valued-parameters\">Differences Between OUTPUT Parameters and Table-Valued Parameters<\/h4>\n\n\n\n<p>This previous tutorial discussed <a href=\"https:\/\/expert-only.com\/en\/t-sql\/return-values-sql-server-stored-procedure\/\">output parameters in SQL procedures<\/a>, which return single values or single line values if you extend the concept. Table-valued parameters, on the other hand, allow you to pass an entire set of data, like a table, to a stored procedure.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"h-set-up-the-customers-table\">Set Up the Customers Table<\/h4>\n\n\n\n<p>First, let&#8217;s create a sample table to store <em>Customers<\/em>:<\/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=\"\">-- Create the customers table with 3 sample columns\nCREATE TABLE [dbo].[CUSTOMERS] (\n   [CustomerNumber] int IDENTITY(1,1),\n   [Name]           nvarchar(20) UNIQUE,\n   [City]           nvarchar(20)\n)\nGO\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-step-1-create-a-type-for-the-table-valued-parameter\">Step 1: Create a Type for the Table-Valued Parameter<\/h2>\n\n\n\n<p>Before we create the stored procedure, we need to define a type that represents the structure of the table we want to pass as a parameter:<\/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=\"\">-- Create a type that represents our table structure\nCREATE TYPE CustomerTableType AS TABLE (\n    [Name] nvarchar(20),\n    [City] nvarchar(20)\n)\nGO\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-step-2-create-a-stored-procedure-with-a-table-valued-parameter-input\">Step 2: Create a Stored Procedure with a Table-Valued Parameter Input<\/h2>\n\n\n\n<p>Now, let&#8217;s create a stored procedure that accepts table-valued <a href=\"https:\/\/en.wikipedia.org\/wiki\/Parameter_(computer_programming)\" target=\"_blank\" rel=\"noopener\">parameter<\/a>, using the table TYPE named <em>CustomerTableType<\/em> , defined in the paragraph above. In this particular use case, the stored procedure with a very explicit name performs these actions: <\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Receive the Table as an input table parameter (variable).<\/li>\n\n\n\n<li>Insert all of its content into the classical Customers table.<\/li>\n<\/ol>\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=\"\">-- Create a stored procedure that takes a table-valued parameter\nCREATE PROCEDURE dbo.uspAddMultipleCustomers\n    @Customers CustomerTableType READONLY\nAS\n    INSERT INTO dbo.CUSTOMERS ([Name], [City])\n    SELECT [Name], [City] FROM @Customers\nGO\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-step-3-use-the-sql-server-stored-procedure\">Step 3: Use the SQL Server Stored Procedure<\/h2>\n\n\n\n<p>To use this stored procedure, follow these 3 steps: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First declare a variable of our table type.<\/li>\n\n\n\n<li>Populate it with one or multiple lines of data here we insert 5 lines.<\/li>\n\n\n\n<li>And then pass it to the procedure.<\/li>\n<\/ul>\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 a variable of our table type\nDECLARE @NewCustomers CustomerTableType\n\n-- Insert data into the table-valued variable\nINSERT INTO @NewCustomers ([Name], [City])\nVALUES \n    ('Alice', 'New York'),      -- North America\n    ('Jorge', 'Buenos Aires'),  -- South America\n    ('Chen', 'Beijing'),        -- Asia\n    ('Fatima', 'Cairo'),        -- Africa\n    ('Anya', 'Moscow');         -- Europe\n\n-- Execute the stored procedure with the table-valued parameter\nEXEC dbo.uspAddMultipleCustomers @NewCustomers\n<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-unlike-xml-table-typed-valued-parameters-allows-flexibility\">Unlike XML,  table typed valued parameters allows flexibility<\/h3>\n\n\n\n<p>To conclude, table-valued parameters offer a flexible way to pass complex data structures like tables to stored procedures in SQL Server. This method is highly efficient for bulk data operations and complex business logic implementations. <\/p>\n\n\n\n<p>You can extend this concept with even a larger scope of flexibly by <a href=\"https:\/\/expert-only.com\/en\/t-sql\/xml-data-in-sql-server\/\">passing XML tables to stored procedures<\/a>. Then the variable is completely independent of the data structure. <\/p>\n\n\n\n<p>For more examples about usage of stored procedures like modification, visit our <a href=\"https:\/\/expert-only.com\/en\/t-sql\/alter-sql-server-stored-procedure\/\">SQL Server stored procedure modification guide<\/a>. And also this similar tutorial, but this time using functions instead of procedures.<\/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=\"iePheVcKzp\"><a href=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-table-valued-functions\/\">How to use SQL Server Table-Valued Functions?<\/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;How to use SQL Server Table-Valued Functions?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/t-sql\/sql-server-table-valued-functions\/embed\/#?secret=hCH6k0JTfO#?secret=iePheVcKzp\" data-secret=\"iePheVcKzp\" 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>In this T-SQL guide, we&#8217;ll explore how to use table-valued parameters in a SQL Server stored procedure. It enables the passing of a table as a parameter and mot only its values like by using a select statement for <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/t-sql\/table-valued-parameter-stored-procedure\/\" title=\"Use a table parameter in a Stored Procedure in SQL Server\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10839,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[454],"tags":[],"class_list":{"0":"post-29032","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\/29032","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=29032"}],"version-history":[{"count":9,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29032\/revisions"}],"predecessor-version":[{"id":29044,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/29032\/revisions\/29044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10839"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=29032"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=29032"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=29032"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}