sql 存儲進程批量刪除數據的語句。本站提示廣大學習愛好者:(sql 存儲進程批量刪除數據的語句)文章只能為提供參考,不一定能成為您想要的結果。以下是sql 存儲進程批量刪除數據的語句正文
Create PROCEDURE Batch_Delete
@TableName nvarchar(100), --表名
@FieldName nvarchar(100), --刪除字段名
@DelCharIndexID nvarchar(1000)
as
DECLARE @PointerPrev int
DECLARE @PointerCurr int
DECLARE @TId NVARCHAR(50), @sql NVARCHAR(1000)
Set @PointerPrev = 1
while (@PointerPrev < LEN(@DelCharIndexID))
Begin
Set @PointerCurr = CharIndex(',',@DelCharIndexID,@PointerPrev)
if(@PointerCurr>0)
Begin
SET @TId = cast(SUBSTRING(@DelCharIndexID, @PointerPrev, @PointerCurr - @PointerPrev) As NVARCHAR(50))
SET @sql = 'Delete From '+ @TableName +' Where '+ @FieldName + ' = '''+ @TID+''''
Exec(@Sql)
Print('======='+@TId+'=======sql'+@Sql)
SET @PointerPrev = @PointerCurr + 1
Print(@PointerPrev)
End
else
Begin
Print('break')
Break
End
End
--刪除最初一個,由於最初一個前面沒有逗號,所以在輪回中跳出,需別的再刪除
SET @TId = cast(SUBSTRING(@DelCharIndexID, @PointerPrev, LEN(@DelCharIndexID) - @PointerPrev + 1) As NVARCHAR(50))
SET @sql = 'Delete From '+ @TableName +' Where '+ @FieldName + ' = '''+ @TID+''''
Exec(@Sql)
Print('======='+@TId+'=======sql'+@Sql)
GO