最新活动:电脑PC端+手机端+微网站+自适应网页多模板选择-建站388元起价!!!
当前位置:主页 > 网站建设 > sqlserver 存储过程带事务 拼接id 返回值建站知识

sqlserver 存储过程带事务 拼接id 返回值建站知识

时间:2023-05-23 16:05:23 阅读: 文章分类: 网站建设 作者: 网站编辑员

导读:1建站知识存储过程带事务,拼接id,返回值 以下SQL以防以后还需用到,特此备份网站seo优化课程网站优化seo培训。

网站seo优化课程网站优化seo培训删除一条留言信息会级联删除回复信息,这时我们需要用到事务,如下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].[pr个业网站建设公司oc_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

关键词标签: 存储过程 返回值

声明: 本文由我的SEOUC技术文章主页发布于:2023-05-23 ,文章sqlserver 存储过程带事务 拼接id 返回值建站知识主要讲述存储过程,返回值,sqlserver 存储过程带事务 拼接网站建设源码以及服务器配置搭建相关技术文章。转载请保留链接: https://www.seouc.com/article/web_5930.html

我的IDC 网站建设技术SEOUC.COM
专注网站建设,SEO优化,小程序设计制作搭建开发定制网站等,数千家网站定制开发案例,网站推广技术服务。
  • 5000+合作客服
  • 8年从业经验
  • 150+覆盖行业
  • 最新热门源码技术文章