Home > Backend Development > C++ > How to Pass an Array to a SQL Server Stored Procedure?

How to Pass an Array to a SQL Server Stored Procedure?

Barbara Streisand
Release: 2025-01-31 08:36:09
Original
323 people have browsed it

How to Pass an Array to a SQL Server Stored Procedure?

Pass the array to the SQL Server storage procedure

In the scenario that needs to pass data from C# as an array to SQL Server stored procedure, there are many ways. This article will explore several methods, depending on the SQL Server version used.

SQL Server 2016 (or higher version)

Using string_split () or openjson (), you can pass the separation list or JSON to the storage procedure.

String_Split ()

CREATE PROCEDURE dbo.DoSomethingWithEmployees
  @List varchar(max)
AS
BEGIN
  SET NOCOUNT ON;

  SELECT value FROM STRING_SPLIT(@List, ',');
END
GO
Copy after login
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
Copy after login
SQL Server 2008 (or higher version)

Custom data type and table value parameters are used for array transmission.

SQL database object

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
Copy after login
C# code

// 获取员工 ID
int[] employeeIds = GetEmployeeIds();

// 创建 DataTable 并使用员工 ID 填充
DataTable tvp = new DataTable();
tvp.Columns.Add(new DataColumn("ID", typeof(int)));
foreach (int id in employeeIds)
    tvp.Rows.Add(id);

// 将 DataTable 作为参数添加
using (conn)
{
    SqlCommand cmd = new SqlCommand("dbo.DoSomethingWithEmployees", conn);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlParameter tvparam = cmd.Parameters.AddWithValue("@List", tvp);
    tvparam.SqlDbType = SqlDbType.Structured;
    tvparam.TypeName = "dbo.IDList";

    // 执行查询
}
Copy after login
SQL Server 2005

Introduction to the custom disassembly function for array transmission:

SQL 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
Copy after login
Storage procedure <存>

C# code
CREATE PROCEDURE dbo.DoSomethingWithEmployees
  @List VARCHAR(MAX)
AS
BEGIN
  SET NOCOUNT ON;

  SELECT EmployeeID = Item FROM dbo.SplitInts(@List, ','); 
END
GO
Copy after login

Pass the array as a list of commas.

The above is the detailed content of How to Pass an Array to a SQL Server Stored Procedure?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template