Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8107753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:52:43+00:00 2026-06-06T00:52:43+00:00

How to pass an array into a SQL Server stored procedure? For example, I

  • 0

How to pass an array into a SQL Server stored procedure?

For example, I have a list of employees. I want to use this list as a table and join it with another table. But the list of employees should be passed as parameter from C#.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:52:45+00:00Added an answer on June 6, 2026 at 12:52 am

    SQL Server 2016 (or newer)

    You can pass in a delimited list or JSON and use STRING_SPLIT() or OPENJSON().

    STRING_SPLIT():

    CREATE PROCEDURE dbo.DoSomethingWithEmployees
      @List varchar(max)
    AS
    BEGIN
      SET NOCOUNT ON;
    
      SELECT value FROM STRING_SPLIT(@List, ',');
    END
    GO
    EXEC dbo.DoSomethingWithEmployees @List = '1,2,3';
    

    OPENJSON():

    CREATE PROCEDURE dbo.DoSomethingWithEmployees
      @List varchar(max)
    AS
    BEGIN
      SET NOCOUNT ON;
    
      SELECT value FROM OPENJSON(CONCAT('["',
        REPLACE(STRING_ESCAPE(@List, 'JSON'), 
        ',', '","'), '"]')) AS j;
    END
    GO
    EXEC dbo.DoSomethingWithEmployees @List = '1,2,3';
    

    I wrote more about this here:

    • Handling an unknown number of parameters in SQL Server
    • Ordered String Splitting in SQL Server with OPENJSON

    SQL Server 2008 (or newer)

    First, in your database, create the following two objects:

    CREATE TYPE dbo.IDList
    AS TABLE
    (
      ID INT
    );
    GO
    
    CREATE PROCEDURE dbo.DoSomethingWithEmployees
      @List AS dbo.IDList READONLY
    AS
    BEGIN
      SET NOCOUNT ON;
      
      SELECT ID FROM @List; 
    END
    GO
    

    Now in your C# code:

    // Obtain your list of ids to send, this is just an example call to a helper utility function
    int[] employeeIds = GetEmployeeIds();
    
    DataTable tvp = new DataTable();
    tvp.Columns.Add(new DataColumn("ID", typeof(int)));
    
    // populate DataTable from your List here
    foreach(var id in employeeIds)
        tvp.Rows.Add(id);
    
    using (conn)
    {
        SqlCommand cmd = new SqlCommand("dbo.DoSomethingWithEmployees", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter tvparam = cmd.Parameters.AddWithValue("@List", tvp);
        // these next lines are important to map the C# DataTable object to the correct SQL User Defined Type
        tvparam.SqlDbType = SqlDbType.Structured;
        tvparam.TypeName = "dbo.IDList";
        // execute query, consume results, etc. here
    }
    

    SQL Server 2005

    If you are using SQL Server 2005, I would still recommend a split function over XML. First, create a function:

    CREATE FUNCTION dbo.SplitInts
    (
       @List      VARCHAR(MAX),
       @Delimiter VARCHAR(255)
    )
    RETURNS TABLE
    AS
      RETURN ( SELECT Item = CONVERT(INT, Item) FROM
          ( SELECT Item = x.i.value('(./text())[1]', 'varchar(max)')
            FROM ( SELECT [XML] = CONVERT(XML, '<i>'
            + REPLACE(@List, @Delimiter, '</i><i>') + '</i>').query('.')
              ) AS a CROSS APPLY [XML].nodes('i') AS x(i) ) AS y
          WHERE Item IS NOT NULL
      );
    GO
    

    Now your stored procedure can just be:

    CREATE PROCEDURE dbo.DoSomethingWithEmployees
      @List VARCHAR(MAX)
    AS
    BEGIN
      SET NOCOUNT ON;
      
      SELECT EmployeeID = Item FROM dbo.SplitInts(@List, ','); 
    END
    GO
    

    And in your C# code you just have to pass the list as '1,2,3,12'…


    I find the method of passing through table valued parameters simplifies the maintainability of a solution that uses it and often has increased performance compared to other implementations including XML and string splitting.

    The inputs are clearly defined (no one has to guess if the delimiter is a comma or a semi-colon) and we do not have dependencies on other processing functions that are not obvious without inspecting the code for the stored procedure.

    Compared to solutions involving user defined XML schema instead of UDTs, this involves a similar number of steps but in my experience is far simpler code to manage, maintain and read.

    In many solutions you may only need one or a few of these UDTs (User defined Types) that you re-use for many stored procedures. As with this example, the common requirement is to pass through a list of ID pointers, the function name describes what context those Ids should represent, the type name should be generic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list of values I want to insert into a table via
I want to pass a struct array into a function and I keep getting
I need to pass an array of strings to SQL Server 2005, and so
How to pass an array into an SQL statement and submit it to a
Say I want to pass an array of integers into a method that runs
I'm trying to pass an array into a function and then use the information
Can i pass the entire POST array into a function and handle it within
I'm trying to pass a byte array from inside my rails app into another
I've got an array with items, and I want to pass these in to
I am trying to pass array parameter to SQL commnd in C# like below,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.