How to pass a list as a parameter in a stored procedure?

Looking at passing a list of user IDs to return list names. I have a plan for handling issued names (with COALESCE something or the other), but trying to find a better way to pass a list of user IDs. The feelings of my sproc will look something like this:

create procedure [dbo].[get_user_names]
@user_id_list, --which would equal a list of incoming ID numbers like (5,44,72,81,126)
@username varchar (30) output
as
select last_name+', '+first_name 
from user_mstr
where user_id in @user_id_list

Passing values ​​for @user_id_list is my main problem here.

+25
source share
7 answers

The preferred method to pass an array of values ​​to a stored procedure in an SQL server is to use table parameters.

First you define the type as follows:

CREATE TYPE UserList AS TABLE ( UserID INT );

Then you use this type in the stored procedure:

create procedure [dbo].[get_user_names]
@user_id_list UserList READONLY,
@username varchar (30) output
as
select last_name+', '+first_name 
from user_mstr
where user_id in (SELECT UserID FROM @user_id_list)

, , :

DECLARE @UL UserList;
INSERT @UL VALUES (5),(44),(72),(81),(126)

, , SP:

EXEC dbo.get_user_names @UL, @username OUTPUT;
+33

, : , JSON.

2016 STRING_SPLIT, : https://docs.microsoft.com/en-us/sql/t-sql/functions/string-split-transact-sql

, / / .

2016 JSON nvarchar OPENJSON: https://docs.microsoft.com/en-us/sql/t-sql/functions/openjson-transact-sql

, , , , .

, TVP , , , /. . 2016+, , , /.

, , , , 2016+, / , -.

+9

:

  • # String.

string userId = "";

  1. . ",".

: #

userId= "5,44,72,81,126";

SQL-

 SqlParameter param = cmd.Parameters.AddWithValue("@user_id_list",userId);
  1. SQL-. ( NVARCHAR(Max)) .
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  
  );
  1. , , .

SELECT user_id = FROM dbo.SplitInts(@user_id_list, ',');

+4

:

create procedure [dbo].[get_user_names]
    @user_id_list varchar(2000), -- You can use any max length

    @username varchar (30) output
    as
    select last_name+', '+first_name 
    from user_mstr
    where user_id in (Select ID from dbo.SplitString( @user_id_list, ',') )

SplitString:

Create FUNCTION [dbo].[SplitString]
(    
      @Input NVARCHAR(MAX),
      @Character CHAR(1)
)
RETURNS @Output TABLE (
      Item NVARCHAR(1000)
)
AS
BEGIN
      DECLARE @StartIndex INT, @EndIndex INT

      SET @StartIndex = 1
      IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
      BEGIN
            SET @Input = @Input + @Character
      END

      WHILE CHARINDEX(@Character, @Input) > 0
      BEGIN
            SET @EndIndex = CHARINDEX(@Character, @Input)

            INSERT INTO @Output(Item)
            SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1)

            SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
      END

      RETURN
END
+1

Azure DB, Azure Data WH SQL Server 2016, STRING_SPLIT , @sparrow.

@sparrow

where user_id in (Select value from STRING_SPLIT( @user_id_list, ',')

+1
source

Perhaps you could use:

select last_name+', '+first_name 
from user_mstr
where ',' + @user_id_list + ',' like '%,' + convert(nvarchar, user_id) + ',%'
0
source

You can use this simple inline method to build the string_list_type parameter (works in SQL Server 2014):

declare @p1 dbo.string_list_type
insert into @p1 values(N'myFirstString')
insert into @p1 values(N'mySecondString')

An example of using a stored procedure:

exec MyStoredProc @MyParam=@p1
0
source

Source: https://habr.com/ru/post/1015257/


All Articles