我有一个存储过程,该存储过程将根据传入的ID从ausers表中删除。如果要删除多个用户或ID范围,有没有办法做到这一点?
ID为整数。
答案 0 :(得分:0)
您必须创建一个用于拆分字符串的函数,并且可以使用逗号分隔参数
Function
---------------
CREATE FUNCTION 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
GO
Store Procedure
----------------------
DECLARE @CustomerID VARCHAR(50) = '1,2,3'
DELETE FROM Customer WHERE CustomerID in (SELECT Item
FROM dbo.SplitString(@CustomerID, ','))
希望对您有帮助:)