如何在SQL Server 2008中使用XML插入数据

时间:2017-05-20 03:16:18

标签: sql-server xml sql-server-2008

整个"使用XML"插入多个记录;几乎已被SQL Server 2008中的表参数所取代。

CREATE PROCEDURE [Booking].[PlannerQuotation] 
    (@PlannerQuoatetionXML XML,  
     @ResponseXML XML OUTPUT)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @PlannerID INT
    ,@CustomerName NVARCHAR(100)
    ,@MobileNumber NVARCHAR(20)
    ,@ToAddress NVARCHAR(256)
    ,@Subject NVARCHAR(100)
    ,@Message NVARCHAR(100)
    ,@QuotationEmailBody NVARCHAR(100)
    ,@QuotationStatus NVARCHAR(100)
    ,@UserID INT    --Comment : 1
    ,@EntityID INT  --Comment : 1
    ,@ID INT

    BEGIN TRY
    BEGIN TRANSACTION
        --select details
        SELECT 
            @CustomerName = Plannerxml.nref.value('./CustomerName[1]', 'NVARCHAR(100)')
            ,@PlannerID = Plannerxml.nref.value('./PlannerID[1]', 'INT')    --Comment : 1
            ,@UserID = Plannerxml.nref.value('./UserID[1]', 'INT')  --Comment : 1
            ,@MobileNumber = Plannerxml.nref.value('./MobileNumber[1]', 'NVARCHAR(20)')
            ,@ToAddress = Plannerxml.nref.value('./ToAddress[1]', 'NVARCHAR(256)')
            ,@Subject = Plannerxml.nref.value('(./Subject[1])',  'VARCHAR(100)')
            ,@Message = Plannerxml.nref.value('./Message[1]', 'NVARCHAR(100)')
            ,@QuotationEmailBody = Plannerxml.nref.value('(./QuotationEmailBody[1])', 'VARCHAR(100)')
            ,@QuotationStatus = Plannerxml.nref.value('./QuotationStatus[1]', 'NVARCHAR(100)')
            --,@IsSystemPlan = Plannerxml.nref.value('./IsSystemPlan[1]',  'TINYINT')
            --,@ExpireDate = CAST(REPLACE(REPLACE(Plannerxml.nref.value('(./ExpireDate[1])', 'varchar(22)'), 'T', ' '), 'TZ', ' ') AS DATETIME)
            --,@StartDate = CAST(REPLACE(REPLACE(Plannerxml.nref.value('(./StartDate[1])', 'varchar(22)'), 'T', ' '), 'TZ', ' ') AS DATETIME)
            -- Mode of Operation will contain 3 Options - Copy,Move,Rename
        FROM 
            @PlannerQuoatetionXML.nodes('//PlannerQuoatetionRQ') AS Plannerxml(nref)
        --select Planner id
        --SELECT @plannerID = tblPlanner.colPlannerID.value('@PlannerID', 'int')
        --FROM @PlannerQuoatetionXML.nodes('PlannerQuoatetionRQ/Planners/Planner') tblPlanner(colPlannerID)

        SELECT 
            @EntityID = P.EntityID 
        FROM  
            Booking.Planner 
        WHERE 
            PlannerID = @PlannerID 
            AND IsActive = 1 
            AND ISNULL(IsDeleted, 0) = 0

        -- If Operation is Copy and new planner
        -- create new planner
        IF (UPPER(@ModeofOperation) = 'COPY'AND Isnull(@CopytoPlannerID, 0) = 0)
        BEGIN
            -- Insert New planner name into Planner table
            IF NOT EXISTS (SELECT 1
                           FROM Booking.Planner
                           WHERE PlannerName = @PlannerName
                             AND EntityID = @EntityID
                             AND IsActive = 1
                             AND ISNULL(IsDeleted, 0) = 0)
            BEGIN
                --insert into planner
                INSERT INTO Booking.PlannerQuotation (PlannerID, CustomerName,
                                                      MobileNumber, ToAddress,
                                                      Subject, Message,
                                                      QuotationEmailBody,
                                                      QuotationStatus,
                                                      IsActive,
                                                      CreatedBy, CreatedDate)
               VALUES (@PlannerID, @CustomerName,
                       @MobileNumber, @ToAddress, @Subject, @Message,
                       @QuotationEmailBody, @QuotationStatus, 1,
                       @UserID, [Entity].[GetEntityDateTime](@EntityID))

               SELECT @ID = SCOPE_IDENTITY()
           END
           ELSE
           BEGIN
               EXEC Logging.HandleError 60208, 'en-US'
           END
        END
        ELSE
        BEGIN
            SET @ID = @PlannerID
        END
        SET @ResponseXML = '<Response><PlannerQuotationID>'+ CAST(@ID AS VARCHAR(MAX))+'</PlannerQuotationID></Response>'
        --end update planner and details record
        COMMIT TRANSACTION
        END TRY
        BEGIN CATCH
        IF (@@TRANCOUNT > 0)
        BEGIN
        ROLLBACK TRANSACTION
        END
        IF (ERROR_NUMBER() < 60000)
        BEGIN
        EXEC Logging.InsertFailedTransactionLog 'PlannerQuotationInsert'
        ,@PlannerQuotationXML
        END
        EXEC Logging.InsertLogError
    END CATCH
END

我在我的Web应用程序后端使用SQL Server 2008。显然,只要有多个插入方案,我就会迭代C#代码中的所有记录。我从来没有尝试过使用XML的多重插入。我想在阅读了很多关于使用SQL Server 2008进行XML操作的博客之后,这个过程非常繁琐。

所以我的问题是:通过XML插入比传统插入更有效吗?

0 个答案:

没有答案