Assign a list of integers to an @var

前端 未结 2 1583
一个人的身影
一个人的身影 2021-01-13 17:18

I can:

declare @idOrder int
set @idOrder = 21319

I want:

declare @idOrder int
set @idOrder = (21319, 21320)
2条回答
  •  清酒与你
    2021-01-13 17:52

    In SQL Server you can also

    CREATE FUNCTION [dbo].[fn_ado_param_int] (@ado nvarchar(4000))
    RETURNS @VALUES TABLE (ado int)AS
       BEGIN
       declare @Delim char(1)
       set @Delim = ','
       DECLARE @chrind INT
       DECLARE @Piece nvarchar(4000)
       SELECT @chrind = 1
       WHILE @chrind > 0
          BEGIN
             SELECT @chrind = CHARINDEX(@Delim,@ado)
             IF @chrind > 0
                SELECT @Piece = LEFT(@ado,@chrind - 1)
             ELSE
                SELECT @Piece = @ado
             INSERT @VALUES(ado) VALUES(@Piece)
             SELECT @ado = RIGHT(@ado,LEN(@ado) - @chrind)
             IF LEN(@ado) = 0 BREAK
          END
       RETURN
    END
    
    declare @idOrder varchar(500);
    set @inOrder = "21319,2138,2138";
    delete from Orders where id in (select ado from dbo.fn_ado_param_int(@idOrder));
    

提交回复
热议问题