存储过程在插入表之前将数据插入XML?

时间:2015-11-24 09:24:49

标签: mysql xml

编辑1:

了解到如果你添加

for xml auto, elements

到SELECT的结尾然后你可以得到这样的元素:

<orderline>
  <orderNr>FO123456</orderNr>
  <line>1000</line>
  <company>HI</company>
</orderline>

但我仍然不知道如何将其插入@XML。

我有多个具有相同结构的数据库,当设置某个标志时,我想执行以下代码。问题是我对创建XML非常陌生,似乎无法找到如何做到这一点的任何好的解释。我希望代码不言自明。

DECLARE @XML xml = 
'<message>
    <header>
        <command>
            LOCK_ORDER
        </command>
    </header>
    <data>
        -- I wanna insert the data from the SELECT here --
    </data>
</message>'

SELECT order_num AS orderNr, line_num AS line, orderline.company 
FROM ORDER_LINE orderline 
INNER JOIN ITEM_OPTION i on orderline.ITEM_NUM = i.ITEM_NUM and orderline.WAREH_NUM = i.WAREH_NUM and orderline.COMPANY = i.COMPANY 
WHERE ORDER_NUM = @orderNr and i.MANAGE_STOCK = 'Y'

INSERT INTO ASSIGNMENT_MESSAGE 
(COM_STATUS, TO_SYSTEM, DATA, TIMESTAMP)
VALUES
(1234, 'TO_SYSTEM', @XML, GETDATE())

1 个答案:

答案 0 :(得分:2)

试试这个伙伴:

 DECLARE @result xml = null;


SET @result = (SELECT  CONCAT('<message><header><command>LOCK_ORDER</command></header><data>',
    CONCAT((
                SELECT order_num AS orderNr, line_num AS line, orderline.company 
        FROM ORDER_LINE orderline 
        INNER JOIN ITEM_OPTION i on orderline.ITEM_NUM = i.ITEM_NUM and orderline.WAREH_NUM = i.WAREH_NUM and orderline.COMPANY = i.COMPANY 
        WHERE ORDER_NUM = @orderNr and i.MANAGE_STOCK = 'Y' for xml auto,elements
    )
,'</data></message>')
))


SELECT @result;