如何在从 SQL Server 中的 XML 数据中提取值时修改值
这是一个关于 SQL 中的 XQuerying 的问题(SQL Server 2008)
最初,我有一个 XML,我需要以不同的方式输出,以供参考:
declare @XMLNODE table (id int identity, doc xml)
insert @XMLNODE (doc) values (
'
<Root>
<Elements>
<Items>
<OldItem>
<ID>1</ID>
<Show Pointer="yes" />
<Display Pointer="Display">
<Detail1>some Details</Detail1>
</DisplayDetails>
</OldItem>
</Items>
</Elements>
</Root>'
)
SELECT a.value('(ID)[1]','int') as ID,
a.value('(Show/@Pointer)[1]', 'varchar(5)') AS ShowItem,
a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
FROM @XMLNODE t
cross apply
t.doc.nodes('//OldItem') x(a)
FOR XML PATH ('Items'),
ROOT('Elements')
所以,现在我有一个像这样的 XML:
<Elements>
<Items>
<ID>1</ID>
<ShowItem>yes</ShowItem>
<DisplayDetails>true</DisplayDetails>
<Detail1>some Details</Detail1>
</Items>
</Elements>
Want to add some edit this to 1或没有值,基于是否为 true/yes 等。
即所需:
<Elements>
<Items>
<ID>1</ID>
<ShowItem>1</ShowItem>
<DisplayDetails>1</DisplayDetails>
<Detail1>some Details</Detail1>
</Items>
</Elements>
另外,如果
我只想将其更新为
。
有没有办法直接比较查询中的输入(例如是或否)并将其设置为 1 或 0 而不是 true 或 false?我正在使用 .query 来使用这些值并根据需要添加其他信息。
Here is a question on XQuerying in SQL (SQL Server 2008)
Originally, I have an XML that I need to need to output somewhat differently, for reference:
declare @XMLNODE table (id int identity, doc xml)
insert @XMLNODE (doc) values (
'
<Root>
<Elements>
<Items>
<OldItem>
<ID>1</ID>
<Show Pointer="yes" />
<Display Pointer="Display">
<Detail1>some Details</Detail1>
</DisplayDetails>
</OldItem>
</Items>
</Elements>
</Root>'
)
SELECT a.value('(ID)[1]','int') as ID,
a.value('(Show/@Pointer)[1]', 'varchar(5)') AS ShowItem,
a.value('Display[1]/@Pointer[1]="Display"', 'varchar(10)') as DisplayDetails, -- Set to 'true' or 'false'
a.value('DisplayDetails[1][@Pointer[1]="Display"]/Detail1[1]', 'varchar(max)') as Detail1
FROM @XMLNODE t
cross apply
t.doc.nodes('//OldItem') x(a)
FOR XML PATH ('Items'),
ROOT('Elements')
So, now I have an XML like this:
<Elements>
<Items>
<ID>1</ID>
<ShowItem>yes</ShowItem>
<DisplayDetails>true</DisplayDetails>
<Detail1>some Details</Detail1>
</Items>
</Elements>
Want to add some modify this to 1 or no value, based on if it was true/yes etc.
i.e. desired:
<Elements>
<Items>
<ID>1</ID>
<ShowItem>1</ShowItem>
<DisplayDetails>1</DisplayDetails>
<Detail1>some Details</Detail1>
</Items>
</Elements>
Also, if <DisplayDetails>false</DisplayDetails>
I just want to update it to <DisplayDetails />
.
Is there a way to directly compare the inputs in the query (for example, yes or no) and set it to 1 or 0 instead of true or false? I am using a .query to use these values and add additional information as needed..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太从 SQL 查询 XML,但如果我对任何非 XML 的内容执行此操作,我会在 SELECT 语句中执行类似的操作:
注意:尚未尝试过此操作,而且我不在数据库客户端上以确保我的语法完全正确。然而,检查数据库中的值并根据该值显示其他内容相对简单。
I don't query XML from SQL very much, but if I were doing this for anything that wasn't XML, I'd do something like this in the SELECT statement:
Note: this hasn't been tried, and I'm not on a DB client to make sure my syntax is exactly correct. However, checking a value from the DB and showing something else based on that value is relatively straight-forward.