SQL Server 将表中的数据插入 XML 变量
如何在不使用游标的情况下将一大堆行插入到 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')
运行此代码后,@errors 应该包含
<errors>
<error>Message 1</error>
<error>Message 2</error>
<error>Message 3</error>
</errors>
How can I insert a whole bunch of rows into an XML variable without using a cursor?
I know I can do
SET @errors.modify('insert <error>{ sql:variable("@text") }</error> as last into /errors[1]')
to insert the value of a variable, but I want to basically do
SET @errors.modify(SELECT 'insert <error>{ sql:column("text") }</error>' FROM table)
which, of course, isn't legal syntax.
Edit: Obviously my question wasn't clear. What I want is to be able to do like this:
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')
And after running this code, @errors should contain
<errors>
<error>Message 1</error>
<error>Message 2</error>
<error>Message 3</error>
</errors>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不是更简单吗?
Isn't this simpler?
最后更新:
好的,现在问题更加清楚了,他就是解决方案 - 希望如此!
这给了我
开头
@errors在“神奇”SELECT之后的
@newError:更新之后的@errors:
是那你要找的吗? :-)
(旧答案 - 不是 OP 正在寻找的......)
您需要查看 SQL XQuery 中的
.nodes()
函数 - 这将分解 XML基于 XPath 表达式(引用 XML 中可能具有相同结构的节点枚举的某个点)将变量放入 XML 节点列表中,并为它们提供“虚拟”表和列名称。
基于该“Table.Column”元素,您可以从该 XML 节点中选择单个值(属性或子元素),然后将这些值作为“原子”值返回,例如 INT、VARCHAR(x),无论您需要什么。这些值可以插入到表中:
更新: 好的,所以您想做相反的操作 - 将关系数据转换为 XML - 这甚至更容易:-)
这将为您提供 @NewXmlContent 中类似的内容:
并且带有
.modify()
调用的 UPDATE 语句实际上会将该内容插入到数据库中的现有 XML 字段中。这是将 XML 内容放入现有 XML 列的唯一方法 - 无法直接引用正在插入的 XML 片段中的另一个 XML 列......新的“FOR XML PATH”语法非常强大且灵活,允许您做任何事情。
当然,您可以轻松地将其存储到 XML 变量中。
马克
LAST UPDATE:
OK, now that the question is much clearer, he's the solution - hopefully!!
This gives me
@errors at the beginning
@newError after the "magic" SELECT:
@errors after the UPDATE:
Is THAT what you're looking for?? :-)
(old answers - not what the OP was looking for.....)
You need to look at the
.nodes()
function in SQL XQuery - this will break up an XMLvariable into a list of XML nodes, based on an XPath expression (that references some point in your XML where you are likely to have an enumeration of nodes of the same structure), and it gives them a "virtual" table and column name.
Based on that "Table.Column" element, you can select single values from that XML node - either attributes or sub-elements - and you get these back as "atomic" values, e.g. as INT, VARCHAR(x), whatever you need. These values can be inserted into the table:
UPDATE: ok, so you want to do the opposite - turn relational data into XML - that's even easier :-)
This will give you something like this in @NewXmlContent:
and the UPDATE statement with the
.modify()
call will actually insert that content into an existing XML field in your database. This is the only way to get XML contents into an existing XML column - there's no way of directly referencing another XML column inside a XML fragment being inserted....The new "FOR XML PATH" syntax is very powerful and flexible and allows you to do just about anything.
And of course, you can easily store that into a XML variable.
Marc
根据 marc 的回答,这里有一个适用于 SQL Server 2005 的解决方案:
将返回
Based on marc's answer, here is a solution that works for SQL Server 2005:
Will return