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
相关推荐
明月清风精进不止 2020-01-14
liuyang000 2019-12-27
dreamhua 2019-12-23
herohope 2019-12-17
zhaojp0 2019-12-14
明月清风精进不止 2019-12-01
li00lee 2016-10-21
dropkai 2010-04-24
zjyzz 2007-08-26
姚强 2011-09-06
dropkai 2012-08-07
yierxiansheng 2012-08-27
Omega 2015-01-11
huanging 2014-11-23
sflsgfs 2014-04-30
seegroung 2013-04-04