Sql Server 2012 存儲過程Demo1

--===================================================================
--Author:wuyang
--CreateDate:2017-1-13
--Desc:CMS轉移
--eg:把人南路社區(011002002002048004)的信息數據遷移至青石橋社區(011002002002048002),然後刪除人南路社區。
--===================================================================

CREATE PROCEDURE [dbo].[CMSGroupChange]
(
@oldid      NVARCHAR(100),--轉移id
@newid  NVARCHAR(100),--新id
@result         INT OUT       --1:成功 0:失敗   
)
AS
BEGIN
    SET XACT_ABORT ON
    BEGIN TRANSACTION
        DECLARE @oldgroupid NVARCHAR(100);
        DECLARE @newgroupid NVARCHAR(100);
        DECLARE @oldusername NVARCHAR(100);
        DECLARE @newusername NVARCHAR(100);
        IF EXISTS(SELECT * FROM gpsp_sitemapping WHERE old_id=@oldid) and  EXISTS(SELECT * FROM gpsp_sitemapping WHERE old_id=@newid)
        BEGIN
            --1.根據id查詢站點名稱和站點id
            SELECT @oldgroupid=new_id,@oldusername=old_name FROM gpsp_sitemapping WHERE old_id=@oldid;
            SELECT @newgroupid=new_id,@newusername=old_name FROM gpsp_sitemapping WHERE old_id=@newid;
            print @oldusername+@oldgroupid+'    中的數據即將轉移到     '+@newusername+@newgroupid+'中';
            --2.開始轉移 update CMSPublishedArticle(更新站點id和plid)
            update CMSPublishedArticle set groupid = @newgroupid,plid = 
            (select l.plid ) from CMSPublishedArticle c,JournalArticle j ,Layout l
            where
            c.groupId = @oldgroupid and 
            c.resourcePrimKey = j.resourcePrimKey and 
            l.uuid_ = j.layoutUuid and
            l.groupId = @newgroupid;
            --3.update JournalArticle(更新站點id)
            update JournalArticle set groupId = @newgroupid where groupId = @oldgroupid;
            --4.update AssetEntry(更新站點di)
            update AssetEntry set groupId = @newgroupid where groupId = @oldgroupid and classNameId = '10109';
            --5.delete gpsp_site(刪除舊站點)
            delete from gpsp_site where siteid = @oldgroupid;
            --6.delete gpsp_sitemapping(刪除舊站點映射關係)
            delete from gpsp_sitemapping where new_id = @oldgroupid
            --7.跟新CMS顯示來源名稱
            update CMSPublishedArticle set userName = @newusername where groupId = @newgroupid
        END
        ELSE
        BEGIN
            SET @result=0;
            print '錯誤,不存在站點';
        END

    IF @@ERROR<>0
    BEGIN
        SET @result=0;
        ROLLBACK TRANSACTION
        RETURN ;
    END
    ELSE
    BEGIN
        SET @result=1;
        COMMIT TRANSACTION
    END


END


GO


發佈了42 篇原創文章 · 獲贊 47 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章