sql 存储过程批量删掉数据的语句
发布时间:2023-07-27 14:19:08 所属栏目:MsSql教程 来源:
导读:<div class="codebody" id="code64953">
Create PROCEDURE Batch_Delete
@TableName nvarchar(100),--表名
@FieldName nvarchar(100),--删除字段名
@DelCharIndexID nvarchar(1000)
as
DECL
Create PROCEDURE Batch_Delete
@TableName nvarchar(100),--表名
@FieldName nvarchar(100),--删除字段名
@DelCharIndexID nvarchar(1000)
as
DECL
<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 (编辑:聊城站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐