sqlserver 存储过程带事务 拼接id 返回值_MsSql

删除一条留言信息会级联删除回复信息,这时我们需要用到事务,如下SQL

复制代码 代码如下:

ALTER PROCEDURE [dbo].[proc_tb_leaveword_delete]
 (
     @leavewordID INT,
     @record TINYINT OUTPUT
 )   
 AS
 BEGIN
     BEGIN TRY
         BEGIN TRANSACTION
             DELETE FROM tb_leavewordID WHERE leavewordID=@leavewordID
             DELETE FROM tb_reply WHERE leavewordID=@leavewordID
             SET @record=0 --成功
             COMMIT TRANSACTION
     END TRY
     BEGIN CATCH
         ROLLBACK TRANSACTION
         SET @record=-1 --失败
     END CATCH
     RETURN @record
 END

删除一条新闻,一条新闻可能有多条留言,每条留言可能有回复信息,这时我们删除一条新闻的SQL如下

复制代码 代码如下:

ALTER PROCEDURE [dbo].[proc_tb_news_delete]
 (
     @newsID INT,
     @record TINYINT OUTPUT
 )   
 AS
 BEGIN
     DECLARE @leavewordCount INT --留言个数
     DECLARE @delete_where VARCHAR(4000) --留言id字符,类似1,2,4,5,6
     SET @leavewordCount=(SELECT ISNULL(COUNT(1),0) FROM tb_leaveword WHERE newsID=@newsID)
     SET @delete_where=''
     IF(@leavewordCount=0) --此条新闻无留言时
         BEGIN TRY
             DELETE FROM tb_news WHERE newsID=@newsID
             SET @record=0 --成功
         END TRY
         BEGIN CATCH
             SET @record=-1 --失败
         END CATCH
     ELSE IF(@leavewordCount>0) --此条新闻有留言时
        ----获取删除条件(start)----
        DECLARE MY_CURSOR CURSOR
        FOR SELECT leavewordID FROM tb_news WHERE newsID=@newsID
        BEGIN
            DECLARE @leavewordID INT
            OPEN MY_CURSOR
            FETCH NEXT FROM MY_CURSOR INTO @leavewordID
            IF(@leavewordID IS NOT NULL)
                SET @delete_where=@delete_where+CAST(@leavewordID AS VARCHAR(10))+','
                WHILE(@@FETCH_STATUS<>-1)
                    BEGIN
                        SET @leavewordID=NULL
                        FETCH NEXT FROM MY_CURSOR INTO @leavewordID
                        IF(@leavewordID IS NOT NULL)
                            SET @delete_where=@delete_where+CAST(@leavewordID AS VARCHAR(10))+','
                    END
         END
         CLOSE MY_CURSOR
         DEALLOCATE MY_CURSOR
         SET @delete_where=SUBSTRING(@delete_where,1,LEN(@delete_where)-1)
         ----获取删除条件(end)----
         BEGIN
             BEGIN TRY
                 BEGIN TRANSACTION
                     DELETE FROM tb_news WHERE newsID=@newsID
                     EXECUTE('DELETE FROM tb_leaveword WHERE leavewordID IN('+@delete_where+')')
                     EXECUTE('DELETE FROM tb_reply WHERE leavewordID IN('+@delete_where+')')
                     SET @record=0 --成功
                     COMMIT TRANSACTION
             END TRY
             BEGIN CATCH
                 ROLLBACK TRANSACTION
                 SET @record=-1 --失败
             END CATCH
         END
      RETURN @record
 END

删除一新闻类型时,可能此类型下有多条新闻,此条新闻下又有多条留言,留言下又有多条回复,依次级联删除,如下存储过程

复制代码 代码如下:

ALTER PROCEDURE [dbo].[proc_tb_news_type_delete]
 (
     @typeID INT,
     @record TINYINT OUTPUT
 )
 AS
 BEGIN
     DECLARE @newsCount INT --此类新闻下的新闻个数
     SET @newsCount=(SELECT ISNULL(COUNT(1),0) FROM tb_news WHERE typeID=@typeID)
     IF(@newsCount=0) --此类型下无新闻
         BEGIN TRY
             DELETE FROM tb_news_type WHERE typeID=@typeID
             SET @record=0 --成功
         END TRY
         BEGIN CATCH
             SET @record=-1 --失败
         END CATCH
     ELSE IF(@newsCount>0) --此类型下有新闻
         BEGIN TRY
             BEGIN TRANSACTION
                 DECLARE MY_CURDOR CURSOR
                 FOR SELECT newsID FROM tb_news WHERE typeID=@typeID
                 BEGIN
                     DECLARE @newsID INT
                     OPEN MY_CURSOR
                     FETCH NEXT FROM MY_CURSOR INTO @newsID
                     IF(@newsID IS NOT NULL)
                         DELETE FROM tb_news_type WHERE typeID=@typeID
                         EXECUTE proc_tb_news_delete @newsID=@newsID --执行存储过程
                         WHILE(@@FETCH_STATUS<>-1)
                             BEGIN
                                 SET @newsID=NULL
                                 FETCH NEXT FROM MY_CURSOR INTO @newsID
                                 IF(@newsID IS NOT NULL)
                                     DELETE FROM tb_news_type WHERE typeID=@typeID
                                     EXECUTE proc_tb_news_delete @newsID=@newsID --执行存储过程
                             END
                 END
                 CLOSE MY_CURSOR
                 DEALLOCATE MY_CURSOR
                 COMMIT TRANSACTION
         END TRY
         BEGIN CATCH
             ROLLBACK TRANSACTION
             SET @record=-1 --失败
         END CATCH
      RETURN @record
 END

当删除多条新闻类型时,我们需要把拼接好的类型id,例如:1,2,4,5,12,34,穿入存储过程,分割字符的SQL语句如下所示:

复制代码 代码如下:

DECLARE @A VARCHAR(5000)
 DECLARE @i INT
 SET @A='A,B,C,D,D,S,X,C,C,C,D,AAAA,DDDDDD,DEF,ERT,'
 SET @i=CHARINDEX(',',@A)
 WHILE @i>=1
 BEGIN
     PRINT LEFT(@A,@i-1)
     SET @A=SUBSTRING(@A,@i+1,LEN(@A)-1)
     SET @i=CHARINDEX(',',@A)
 END

删除多条新闻类型SQL如下:

复制代码 代码如下:

ALTER PROCEDURE [dbo].[proc_tb_news_type_selects_delete]
 (
     @typeID_list VARCHAR(500),
     @record TINYINT OUTPUT
 )
 AS
 BEGIN
     BEGIN TRY
             BEGIN TRANSACTION
                 DECLARE @index INT
                 DECLARE @typeID INT
                 SET @typeID_list=RTRIM(LTRIM(@typeID_list))
                 SET @index=CHARINDEX(',',@typeID_list)
                 WHILE @index>=1
                     BEGIN
                         SET @typeID=CAST(LEFT(@typeID_list,@index-1) AS INT)
                         EXECUTE proc_tb_news_type_delete @typeID=@typeID
                         SET @typeID_list=SUBSTRING(@typeID_list,@index+1,LEN(@typeID_list)-1)
                         SET @index=CHARINDEX(',',@typeID_list)
                     END
             COMMIT TRANSACTION
             SET @record=0 --成功
     END TRY
     BEGIN CATCH
         ROLLBACK TRANSACTION
         SET @record=-1 --失败
     END CATCH
     RETURN @record
 END

作者:cnblogs xu_happy_you

时间: 2024-09-28 08:33:30

sqlserver 存储过程带事务 拼接id 返回值_MsSql的相关文章

sqlserver 存储过程带事务 拼接id 返回值

删除一条留言信息会级联删除回复信息,这时我们需要用到事务,如下SQL 复制代码 代码如下: ALTER PROCEDURE [dbo].[proc_tb_leaveword_delete]  (      @leavewordID INT,      @record TINYINT OUTPUT  )     AS  BEGIN      BEGIN TRY          BEGIN TRANSACTION              DELETE FROM tb_leavewordID WH

xsd-asp.net中数据集(XSD)使用带OUTPUT值的存储过程时,如何获取返回值?

问题描述 asp.net中数据集(XSD)使用带OUTPUT值的存储过程时,如何获取返回值? 在数据集(XSD)中通过创建TableAdatper使用了带OUTPUT值的现有存储过程,在预览数据时能正确获得返回值的列表显示,但在CS页面中如何通过GetData函数正确获得这些返回值? 也试过创建Query使用了带OUTPUT值的现有存储过程,同样可以正确预览数据但CS中仍然取不到返回值,同样提示"最匹配的重载方法具有一些无效参数": ChargeStdTableAdapters.Que

SqlServer存储过程实现及拼接sql的注意点

这里我昨天碰到的问题就是执行一段根据变量tableName对不同的表进行字段状态的更改.由于服务器原因,我不能直接在数据访问层写SQL,所以只好抽离出来放到存储过程里面. 这里就出现了一个问题,我花费了好久才弄通! 其实就是很简单的一个SQL语句: update table1 set field1=value1,field2 = value2 where id = id 我写成什么样子了呢?大家且看: declare @tableName nvarchar(50), @field1 int, @

SqlServer存储过程实现及拼接sql的注意点_MsSql

这里我昨天碰到的问题就是执行一段根据变量tableName对不同的表进行字段状态的更改.由于服务器原因,我不能直接在数据访问层写SQL,所以只好抽离出来放到存储过程里面. 这里就出现了一个问题,我花费了好久才弄通! 其实就是很简单的一个SQL语句: update table1 set field1=value1,field2 = value2 where id = id 我写成什么样子了呢?大家且看: declare @tableName nvarchar(50), @field1 int, @

存储过程的输出参数,返回值与结果集_MsSql

每个存储过程都有默认的返回值,默认值为0.下面我们分别看看在management studio中如何查看输出参数,返回值以及结果集,然后我们再在ASP.NET调用存储过程中如何获得输出参数,返回值以及结果集. 首先:在sql server management studio中查看输出参数,返回值以及结果集.本示例以Northwind数据库为例. 复制代码 代码如下: create proc Employee @Rowcount int=0 output as begin SELECT * FRO

SQLServer存储过程中事务的使用方法_MsSql

本文为大家分享了SQLServer存储过程中事务的使用方法,具体代码如下 create proc usp_Stock @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId int, @StockUnit varchar(50), @StockDate datetime, @TotalMoney money , @ActMoney money , @baseId int, @Description nvarcha

SQLServer存储过程中事务的使用方法

本文为大家分享了SQLServer存储过程中事务的使用方法,具体代码如下 create proc usp_Stock @GoodsId int, @Number int, @StockPrice money, @SupplierId int, @EmpId int, @StockUnit varchar(50), @StockDate datetime, @TotalMoney money , @ActMoney money , @baseId int, @Description nvarcha

存储过程的输出参数,返回值与结果集

每个存储过程都有默认的返回值,默认值为0.下面我们分别看看在management studio中如何查看输出参数,返回值以及结果集,然后我们再在ASP.NET调用存储过程中如何获得输出参数,返回值以及结果集. 首先:在sql server management studio中查看输出参数,返回值以及结果集.本示例以Northwind数据库为例. 复制代码 代码如下: create proc Employee @Rowcount int=0 output as begin SELECT * FRO

.Net 调用存储过程取到return的返回值_实用技巧

1. 存储过程 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: <Author,,Name> -- Create date: <Create Date,,> -- Description: <Description,,> -- ======================================