2012/03/23

Save XML by stored procedure In SQL Server, also see the use of transaction

ALTER PROCEDURE [dbo].[AdminSectionFactLinkSave]
        @RegXML AS XML
AS
BEGIN    

    BEGIN TRY
        
    BEGIN TRANSACTION    

    DECLARE @SectionFact AS INT
        

            DELETE FROM SectionFactKnowFactLink WHERE SFKF_SFID IN (
            SELECT SectionFactLink.ITEM.query('./SFKF_SFID').value('.','INT')
            FROM @RegXML.nodes('/DocumentElement/SectionFactLink') AS SectionFactLink(ITEM))


            INSERT INTO SectionFactKnowFactLink(SFKF_SFID,SFKF_KFMasterID)
            SELECT SectionFactLink.ITEM.query('./SFKF_SFID').value('.','INT'),
            SectionFactLink.ITEM.query('./SFKF_KFMasterID').value('.','INT')
            FROM @RegXML.nodes('/DocumentElement/SectionFactLink') AS SectionFactLink(ITEM)            
            WHERE SectionFactLink.ITEM.query('./SFKF_KFMasterID').value('.','INT') > 0
    
        COMMIT
        RETURN 0

    END TRY

    BEGIN CATCH
        EXEC CatchError
        IF @@TRANCOUNT > 0 ROLLBACK 
        RETURN -1
    END CATCH

END


****************************************************************************
Catch Error:


CREATE PROCEDURE [dbo].[CatchError]    
AS

BEGIN
    
    --To keep the error message
    Declare @ErrMsg as varchar(1000)
    
    BEGIN TRY  
        
        INSERT INTO dbo.Error_Log(Error_Date,Error_Msg,Error_Number,Error_Source )
        SELECT DATEADD(HOUR, 20, GETDATE()), ERROR_MESSAGE() ,ERROR_NUMBER(), ERROR_PROCEDURE()


    END TRY  
  
    BEGIN CATCH  
     
    END CATCH
    
    set @ErrMsg = ERROR_MESSAGE()
    
    Raiserror(@ErrMsg -- Error Message
        , 16 --Severity Level
        , 1 --State
        )

END

No comments:

Post a Comment

 What is AI? Artificial Intelligence (AI) is the ability of machines or computer systems to perform tasks that normally require human intell...