Ask a Question
Ask Question Login
Corporate Training
  1. Community
  2. SQL Server
  3. Question
SQL Server

How to Use Table-Valued Parameter as Output parameter for stored procedure ?

Asked by Aswini Lobo Apr 20, 2021 3.3K views 2 answers
Share

About this question

Is it possible to Table-Valued parameter be used as output param for stored procedure ? Here is, what I want to do in code/*First I create MY type */ CREATE TYPE typ_test AS TABLE ( id int not null ,name varchar(50) not null ,value varchar(50) not null PRIMARY KEY (id) ) GO --Now I want to create stored procedure which is going to send output type I created, --But it looks like it is inpossible, at least in SQL2008 create PROCEDURE [dbo].sp_test @od datetime ,@do datetime ,@poruka varchar(Max) output ,@iznos money output ,@racun_stavke dbo.typ_test READONLY --Can I Change READONLY with OUTPUT ? AS BEGIN SET NOCOUNT ON; /*FILL MY OUTPUT PARAMS AS I LIKE */ end

Your answer

2 Answers

Ranjana Admin JanBask Expert Latest answer

Answered on Jun 12, 2024

Using table-valued parameters (TVPs) as output parameters in stored procedures in SQL Server is not directly supported. TVPs are intended for input purposes, allowing you to pass a set of rows into a stored procedure.

However, you can achieve similar functionality by using a temporary table or a table variable within the stored procedure, populating it with the data you want to return, and then selecting from that table. Here's how you can accomplish this:

Step-by-Step Guide

1. Define a Table Type

First, define a table type that can be used as the TVP.

CREATE TYPE MyTableType AS TABLE

  (    Id INT,    Name NVARCHAR(50));

2. Create a Stored Procedure

Create a stored procedure that uses a table variable to store the output data.

  CREATE PROCEDURE MyStoredProcedureASBEGIN    -- Declare a table variable to store the output data    DECLARE @OutputTable MyTableType;    -- Populate the table variable with data    INSERT INTO @OutputTable (Id, Name)    VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Charlie');    -- Return the contents of the table variable    SELECT * FROM @OutputTable;END;

3. Call the Stored Procedure

You can call the stored procedure and capture the output in your application or another SQL context.

Example using SQL:

  -- Declare a variable of the table type to store the resultDECLARE @ResultTable MyTableType;-- Insert the result of the stored procedure into the table variableINSERT INTO @ResultTableEXEC MyStoredProcedure;-- Select from the table variable to see the resultSELECT * FROM @ResultTable;

Example using C#:

If you are calling the stored procedure from a C# application, you can use SqlDataAdapter to fill a DataTable with the result.

  using (SqlConnection connection = new SqlConnection("your_connection_string")){    using (SqlCommand command = new SqlCommand("MyStoredProcedure", connection))    {        command.CommandType = CommandType.StoredProcedure;        SqlDataAdapter adapter = new SqlDataAdapter(command);        DataTable resultTable = new DataTable();        adapter.Fill(resultTable);        // Now resultTable contains the data returned from the stored procedure    }}

Summary

While SQL Server does not support table-valued parameters as output parameters directly, you can use table variables or temporary tables within your stored procedure to simulate this behavior. By inserting data into a table variable and selecting from it at the end of your procedure, you can effectively return a set of rows from a stored procedure.


Was this helpful?

More SQL Server discussions

Learn & Explore

Free tutorials and interview questions from industry experts — learn the skill, then get ready to prove it.

Latest SQL Server Blogs

Guides, tips and career advice on SQL Server from JanBask experts.