从xml中提取数据并同时更新3个表

时间:2013-05-07 12:08:22

标签: sql sql-server tsql stored-procedures

尝试从xml中提取数据并更新3个表

ALTER PROCEDURE [dbo].[usp_UpdateOrderDetail]
    @Request XML = '<Request/>'--, 
    --@Response XML = '<Response/>' OUTPUT
As
BEGIN
    DECLARE @OrderID BIGINT
    DECLARE @AddressID BIGINT

    SET @OrderID = @Request.value('(Order/OrderID)[1]', 'BIGINT') 
    IF @OrderID > 0 
        BEGIN
            -- this is where I am confused
            UPDATE  Orders O join Customers C on O.OrderID=C.OrderID  SET 
                    C.ContactName = T.C.value('ContactName[1]',  'varchar(50)'),
                    Phone = T.C.value('Phone[1]', 'varchar(50)'),
                    OrderDate = T.C.value('OrderDate[1]', 'varchar(50)')
            FROM @Request.nodes('//Orders') as T(C)
            WHERE OrderID = T.C.value('OrderID[1]', 'BIGINT')
        END
    ELSE
        BEGIN
            INSERT INTO Orders
            (
                ContactName
            ,   Phone
            ,   OrderDate 
            )
            SELECT
                T.C.value('ContactName[1]', 'varchar(50)')
            ,   T.C.value('Phone[1]', 'varchar(50)')
            ,   T.C.value('OrderDate[1]', 'varchar(50)')
            FROM
                @Request.nodes('//Order') AS T (C)
        END
    DECLARE @Counter INT
    SET @Counter = 1

    -- possibly more code here
END

第一条评论中的部分引发错误,说错误的语法......!

我将从所有3个表的几乎所有列中接收数据(为方便起见,此处未包含此内容)。如何连接这3个表并更新每个表中的相应列?

Table diagram

0 个答案:

没有答案