时间:2021-05-02
删除一条留言信息会级联删除回复信息,这时我们需要用到事务,如下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
复制代码 代码如下:
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
复制代码 代码如下:
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
复制代码 代码如下:
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
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
1,不带参数的存储过程2,带输入参数的存储过程3,带输入和输出参数的存储过程4,带返回值的存储过程不带参数的存储过程例如,以下存储过程返回Employees表中
1.首先我们要建立一张表(UserInfo)。主要字段Id、uname、upass。2.创建带返回值的存储过程复制代码代码如下:createprocproc_a
废话不多说,直接给大家贴C#代码了。//////执行存储过程,返回"返回值"//////存储过程名///存储过程参数///执行存储过程的返回值publicsta
过程1:带返回值:dropprocedureifexistsproc_addNum;createprocedureproc_addNum(inxint,in
本文实例讲述了PHP调用存储过程返回值不一致问题的解决方法。分享给大家供大家参考,具体如下:今天遇一个同学聊存储过程返回值经常得到意外的值为null,因为白天有