sql 存储过程批量删除数据的语句

前端之家收集整理的这篇文章主要介绍了sql 存储过程批量删除数据的语句前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="64953" class="copybut" id="copybut64953" onclick="doCopy('code64953')"> 代码如下:

<div class="codebody" id="code64953">
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,LEN(@DelCharIndexID) - @PointerPrev + 1) As NVARCHAR(50))
SET @sql = 'Delete From '+ @TableName +' Where '+ @FieldName + ' = '''+ @TID+''''
Exec(@sql)
Print('======='+@TId+'=======sql'+@sql)
GO

猜你在找的MsSQL相关文章