下面为您介绍的SQL Server删除方法和一般的SQL Server删除方法有所不同,该方法实现的是在SQL Server存储过程通过传送数组字符串参数SQL Server删除多条记录(如多选或全选表单中的多选框所获取的一组数值删除 )。
- 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
- --删除最后一个,因为最后一个后面没有逗号,所以在循环中跳出,需另外再删除
- set @TId=cast(SUBSTRING(@ID,@PointerPrev,LEN(@ID)-@PointerPrev+1) as int)
- Delete from News where ID=@TID
- GO
- set ANSI_NULLS ON
- set QUOTED_IDENTIFIER ON
- GO
- CREATE PROCEDURE AddInter
- @userID nvarchar(max),
- @ProjecID int
- as
- begin
- DECLARE @PointerPrev int
- DECLARE @PointerCurr int
- DECLARE @TId int
- Set @PointerPrev=1
- begin transaction
- while (@PointerPrev < LEN(@userID))
- Begin
- Set @PointerCurr=CharIndex(',',@userID,@PointerPrev)
- if(@PointerCurr>0)
- Begin
- set @TId=cast(SUBSTRING(@userID,@PointerPrev,@PointerCurr-@PointerPrev) as int)
- exec AddIntention @TID,@ProjecID,0,2
- SET @PointerPrev = @PointerCurr+1
- End
- else
- Break
- End
- --删除最后一个,因为最后一个后面没有逗号,所以在循环中跳出,需另外再删除
- set @TId=cast(SUBSTRING(@userID,@PointerPrev,LEN(@userID)-@PointerPrev+1) as int)
- exec AddIntention @TID,@ProjecID,0,2
- IF (@@error <> 0)
- begin
- ROLLBACK TRANSACTION
- end
- COMMIT TRANSACTION
- Return
- end
- GO
【编辑推荐】