sqlserver 預存程序帶事務 拼接id 傳回值

來源:互聯網
上載者:User

刪除一條留言資訊會串聯刪除回複資訊,這時我們需要用到事務,如下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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.