SQL存儲過程通過傳送數(shù)組字符串參數(shù)刪除多條記錄
作者:佚名
SQL刪除記錄有多種不同的方法,下面為您介紹在SQL Server存儲過程通過傳送數(shù)組字符串參數(shù)刪除多條記錄,供您參考。
SQL刪除記錄有多種不同的方法,下面為您介紹在SQL Server存儲過程通過傳送數(shù)組字符串參數(shù)刪除多條記錄(如多選或全選表單中的多選框所獲取的一組數(shù)值刪除 )。
代碼如下:
CREATE PROCEDURE DeleteNews
@ID nvarchar(500)
as
DECLARE @PointerPrev int
DECLARE @PointerCurr int
DECLARE @TId int
Set @PointerPrev=1
while (@PointerPrev < LEN(@ID))
Begin
Set @PointerCurr=CharIndex(',',@ID,@PointerPrev)
if(@PointerCurr>0)
Begin
set @TId=cast(SUBSTRING(@ID,@PointerPrev,@PointerCurr-@PointerPrev) as int)
Delete from News where ID=@TID
SET @PointerPrev = @PointerCurr+1
End
else
Break
End
--刪除最后一個,因為最后一個后面沒有逗號,所以在循環(huán)中跳出,需另外再刪除
set @TId=cast(SUBSTRING(@ID,@PointerPrev,LEN(@ID)-@PointerPrev+1) as int)
Delete from News where ID=@TID
GO
@ID nvarchar(500)
as
DECLARE @PointerPrev int
DECLARE @PointerCurr int
DECLARE @TId int
Set @PointerPrev=1
while (@PointerPrev < LEN(@ID))
Begin
Set @PointerCurr=CharIndex(',',@ID,@PointerPrev)
if(@PointerCurr>0)
Begin
set @TId=cast(SUBSTRING(@ID,@PointerPrev,@PointerCurr-@PointerPrev) as int)
Delete from News where ID=@TID
SET @PointerPrev = @PointerCurr+1
End
else
Break
End
--刪除最后一個,因為最后一個后面沒有逗號,所以在循環(huán)中跳出,需另外再刪除
set @TId=cast(SUBSTRING(@ID,@PointerPrev,LEN(@ID)-@PointerPrev+1) as int)
Delete from News where ID=@TID
GO
責(zé)任編輯:段燃
來源:
互聯(lián)網(wǎng)