SQL服务器将表中的数据插入到XML变量中

时间:2009-11-20 11:29:44

标签: sql-server-2005 sqlxml

如何在不使用游标的情况下将一大堆行插入XML变量? 我知道我可以做到

SET @errors.modify('insert <error>{ sql:variable("@text") }</error> as last into /errors[1]')

插入变量的值,但我想基本上做

SET @errors.modify(SELECT 'insert <error>{ sql:column("text") }</error>' FROM table)

当然,这不是合法的语法。

编辑:显然我的问题不明确。我想要的是能够这样做:

CREATE TABLE my_table(text nvarchar(50))
INSERT INTO my_table VALUES('Message 2')
INSERT INTO my_table VALUES('Message 3')

DECLARE @errors xml
SET @errors = '<errors><error>Message 1</error></errors>'

SET @errors.modify('INSERT EVERYTHING FROM my_table MAGIC STATEMENT')

运行此代码后,@ error应包含

<errors>
  <error>Message 1</error>
  <error>Message 2</error>
  <error>Message 3</error>
</errors>

3 个答案:

答案 0 :(得分:2)

这不是更简单吗?

set ErrorXML=(SELECT * from #MyTable FOR XML AUTO)

答案 1 :(得分:1)

最后更新:

好的,既然问题更清楚了,他就是解决方案 - 希望!!

DECLARE @errors xml
SET @errors = '<errors><error>Message 1</error></errors>'

DECLARE @newErrors XML 

SELECT @newErrors = (SELECT text AS 'error'
FROM dbo.my_table 
FOR XML PATH(''), ELEMENTS)

SELECT @errors, @newErrors

SET @errors.modify('insert sql:variable("@newErrors") as last into (/errors)[1]')

SELECT @errors

这给了我

开头的@errors

<errors><error>Message 1</error></errors>   

“魔术”SELECT后的@newError:

<error>Message 2</error><error>Message 3</error>

更新后的@errors:

<errors>
    <error>Message 1</error>
    <error>Message 2</error>
    <error>Message 3</error>
</errors>

那是你在寻找什么? :-)


(旧答案 - 不是OP正在寻找的......)

您需要查看SQL XQuery中的.nodes()函数 - 这将分解XML 变量到一个XML节点列表,基于一个XPath表达式(它引用了XML中你可能有相同结构的节点枚举的某些点),并为它们提供了一个“虚拟”表和列名。

基于“Table.Column”元素,您可以从该XML节点中选择单个值 - 属性或子元素 - 然后将这些值作为“原子”值返回,例如作为INT,VARCHAR(x),无论你需要什么。这些值可以插入表中:

INSERT dbo.YourTable(col1, col2, col3, ..., colN)
  SELECT
     Error.Column.value('@attr1[1]', 'varchar(20)'),
     Error.Column.value('subitem[1]', 'int'),
     .....
     Error.Column.value('subitemN[1]', 'DateTime')
  FROM
     @xmldata.nodes('/error') AS Error(Column)

更新:确定,所以你想做相反的事情 - 将关系数据转换成XML - 这更容易: - )

DECLARE @NewXmlContent XML

SELECT @NewXmlContent = 
       (SELECT
          col1 as '@ID',
          col2 as 'SomeElement',
          .....
          colN as 'LastElement'
       FROM 
          dbo.YourTable
       WHERE
           ....
       FOR XML PATH('element'), ROOT('root')
       )

UPDATE YourOtherTable
SET XmlField.modify('insert sql:variable("@NewXmlContent") 
                     as last into (/XPath)[1]')
WHERE (some condition)

这将在@NewXmlContent中为您提供类似的内容:

<root>
   <element ID="(value of col1)">
      <SomeElement>(value of col2)</SomeElement>
      .....
      <LastElement>(value of colN)</LastElement>
   </element>
</root>

和带有.modify()调用的UPDATE语句实际上会将该内容插入数据库中的现有XML字段。这是将XML内容放入现有XML列的唯一方法 - 无法直接引用正在插入的XML片段中的另一个XML列....

新的“FOR XML PATH”语法非常强大且灵活,允许您执行任何操作。

当然,您可以轻松地将其存储到XML变量中。

马克

答案 2 :(得分:0)

根据marc的回答,这是一个适用于SQL Server 2005的解决方案:

CREATE TABLE #my_table(text nvarchar(50))
INSERT INTO #my_table VALUES('Message 2')
INSERT INTO #my_table VALUES('Message 3')

DECLARE @errors xml
SET @errors = '<errors><error>Message 1</error></errors>'

SELECT @errors = CAST(@errors AS nvarchar(max)) + '<new>' + (SELECT text AS 'error' FROM #my_table FOR XML PATH(''), ELEMENTS) + '</new>'

SET @errors = CAST(@errors AS nvarchar(max)) + '<new>' + @newErrors + '</new>'
SET @errors.modify('insert (/new/*) as last into (/errors)[1]')
SET @errors.modify('delete (/new)')

SELECT @errors

DROP TABLE #my_table

将返回

<errors>
  <error>Message 1</error>
  <error>Message 2</error>
  <error>Message 3</error>
</errors>