如何在SQL Server中的存储过程中为单个参数传递更多值

时间:2018-10-31 07:45:19

标签: sql sql-server tsql tsqlt

我的文字:-

CREATE proc usp_delete    
        @tranid int           
        as 
        begin 
    delete from customer where tranid in(@tranid)
     end

注意:-我想通过此存储过程删除多于1条记录的记录,例如:-如果我通过1,2,3,4,5,6,那时候所有6条记录都应该删除

2 个答案:

答案 0 :(得分:5)

您可以使用module-info.java来声明

table-valued parameter

对于使用TVP执行,声明一个类型的变量并将其传递给存储过程

CREATE TYPE EntityId AS TABLE  
( Id INT )
 GO

CREATE PROCEDURE usp_delete    
    @tranid EntityId READONLY
AS 
 BEGIN 
     DELETE c
     FROM customer c
     JOIN @tranid t ON t.Id=c.tranid 
 END

答案 1 :(得分:0)

在更高版本的SQL Server中,您可以使用STRING_SPLIT将定界列表转换为单个值,然后使用CROSS APPLY联接到表中。

CREATE PROC usp_delete @tranid VARCHAR(MAX) AS
BEGIN
    DELETE c
    FROM   customer c
    CROSS APPLY STRING_SPLIT(@tranid, ',')
    WHERE c.tranid = value
END
GO
EXEC usp_delete '2,3,4'

在旧版本中,您可以编写自己的表值函数以将定界字符串转换为结果集。

CREATE function [dbo].[delimited_string_to_table] (
     @delimiter char(1)
   , @string    text
)
returns @table table (
     seqnbr  int           not null
   , string  varchar(255)  not null
)
as
begin

   declare @pos      int
   declare @textpos  int
   declare @chunklen smallint
   declare @tmpstr   varchar(8000)
   declare @leftover varchar(8000)
   declare @tmpval   varchar(8000)
   declare @seq int = 1

   set @textpos = 1
   set @leftover = ''

   while @textpos <= datalength(@string)
   begin

      set @chunklen = 8000 - datalength(@leftover) / 2
      set @tmpstr = @leftover + substring(@string, @textpos, @chunklen)
      set @textpos = @textpos + @chunklen

      set @pos = charindex(@delimiter, @tmpstr)

      while @pos > 0
      begin
         set @tmpval = ltrim(rtrim(left(@tmpstr, @pos - 1)))
         insert @table (seqnbr, string) values(@seq, @tmpval)
         set @tmpstr = substring(@tmpstr, @pos + 1, len(@tmpstr))
         set @pos = charindex(@delimiter, @tmpstr)
         set @seq = @seq + 1
      end

      set @leftover = @tmpstr

   end

   if len(rtrim(ltrim(@leftover))) != 0
      insert @table(seqnbr, string) values (@seq, ltrim(rtrim(@leftover)))

   return

end
GO

将示例更改为以下形式:

CREATE PROC usp_delete @tranid VARCHAR(MAX) AS
BEGIN
    DELETE c
    FROM   customer c
    JOIN   dbo.delimited_string_to_table(',', @tranid)
           ON c.tranid = string
END
GO