I have a stored procedure that will delete from ausers table based on an ID I pass in. If I was to delete multiple users or a range of the IDs, is there a way i can do this?
The IDs are Ints.
I have a stored procedure that will delete from ausers table based on an ID I pass in. If I was to delete multiple users or a range of the IDs, is there a way i can do this?
The IDs are Ints.
You have to create one function which is used to split string and you can pass parameter with separated comma
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, ','))
Hope this help you :)