我的 sql server 数据库的 xml 输出有问题.我的桌子:
I have a problem with my xml output from sql server database. My table:
CREATE TABLE [dbo].[test_table](
[id] [int] IDENTITY(1,1) NOT NULL,
[firstname] [nvarchar](255) NULL,
[lastname] [nvarchar](255) NULL,
[city] [nvarchar](255) NULL,
[street] [nvarchar](255) NULL,
[streetno] [int] NULL
)
我想要输出,其中 Address 嵌套在每个 Person 中,如下所示:
I want the output, where Address is nested inside each Person, like this:
<Root>
<Person id="1">
<firstname>Stefanie</firstname>
<lastname>Buckley</lastname>
<Address>
<city>Oklahoma</city>
<street> Cowley Road</street>
<streetno>34</streetno>
</Address>
</Person>
<Person id="2">
<firstname>Sandy</firstname>
<lastname>Mc Gee</lastname>
<Address>
<city>Montgomery</city>
<street> Hague Parkway</street>
<streetno>27</streetno>
</Address>
</Person>
</Root>
我尝试过嵌套选择,例如:
I've tried with nested select like:
select tbl1.id '@id', tbl1.firstname, tbl1.lastname,
(
select city,street,streetno from test_table as tbl2
where tbl2.id = tbl1.id
for xml path('Address')
)
from test_table as tbl1
for xml path('Person'), Root('Root')
但输出如下:
<Root>
<Person id="1">
<firstname>Stefanie</firstname>
<lastname>Buckley</lastname><Address><city>Oklahoma</city><street> Cowley Road</street><streetno>34</streetno></Address></Person>
<Person id="2">
<firstname>Sandy</firstname>
<lastname>Mc Gee</lastname><Address><city>Anchorage</city><street> North Green Clarendon Road</street><streetno>29</streetno></Address></Person>
<Person id="3">
我做错了什么?
你忘记了 ,输入,你不需要 wxtra 从表中读取.
You forgot , type and you don't need wxtra reading from table.
select tbl1.id '@id'
, tbl1.firstname
, tbl1.lastname
, (
select city
, street
, streetno
for xml path('Address'), type
)
from test_table as tbl1
for xml path('Person'), type, Root('Root')
这篇关于获取嵌套的 XML 输出 sql server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
获取所有产品、类别和元数据的 SQL 查询 woocommSQL query to get all products, categories and meta data woocommerce/wordpress(获取所有产品、类别和元数据的 SQL 查询 woocommerce/wordpre
我可以在不编写 SQL 查询的情况下找出数据库列表Can I figure out a list of databases and the space used by SQL Server instances without writing SQL queries?(我可以在不编写 SQL 查询的情况下
如何创建对 SQL Server 实例的登录?How to create a login to a SQL Server instance?(如何创建对 SQL Server 实例的登录?)
如何通过注册表搜索知道SQL Server的版本和版本How to know the version and edition of SQL Server through registry search(如何通过注册表搜索知道SQL Server的版本和版本)
为什么会出现“数据类型转换错误"?使用 ExWhy do I get a quot;data type conversion errorquot; with ExecuteNonQuery()?(为什么会出现“数据类型转换错误?使用 ExecuteNonQuery()?)
如何将 DataGridView 中的图像显示到 PictureBox?How to show an image from a DataGridView to a PictureBox?(如何将 DataGridView 中的图像显示到 PictureBox?)