正确格式化 sql tp 产生预期的 xml 输出
Formatting sql correctly tp produce expected xml output
感谢一位 SO 用户,我在 FOR XML 方面取得了进展,但我显然没有完全正确地使用 sql 语法。
假设我有以下 sql
DECLARE @Uname VARCHAR(15) = 'Dom',
@Pword VARCHAR(15) = 'Monty'
SELECT
RTRIM(@Uname) AS '@uname',
RTRIM( @Pword) AS '@pword',
(SELECT COALESCE(PortOfLanding,'') AS portOfLanding
FROM Landings.LandingHeaders
WHERE Posted = 0
FOR XML PATH('Sale'))
FOR XML PATH('abc')
当运行时,它产生以下内容作为输出
<abc uname="Dom" pword="Monty"><Sale><portOfLanding>GBHTG</portOfLanding></Sale><Sale><portOfLanding>GBHTG</portOfLanding></Sale></abc>
我真正希望的是以下内容
<abc uname="Dom" pword="Python">
<Sale portOfLanding= "GBHTG" />
<Sale portOfLanding= "GBHTG"/>
</abc>
事实上,我想在 SQL 中添加第三部分,这样最终可能会像这样 xml
<abc uname="Dom" pword="Python">
<Sale portOfLanding= "GBHTG">
<saleline detail="some value here" />
<saleline detail="some value here" />
<Sale/>
<Salesnote portOfLanding= "GBHTG"/>
</abc>
有人可以指出我在原始 SQL 查询中哪里出错了吗?
谢谢
试试这个:
DECLARE @Uname VARCHAR(15) = 'Dom',
@Pword VARCHAR(15) = 'Monty';
SELECT RTRIM(@Uname) AS '@uname',
RTRIM( @Pword) AS '@pword',
(SELECT COALESCE(PortOfLanding,'') AS '@portOfLanding'
FROM Landings.LandingHeaders
WHERE Posted = 0
FOR XML PATH('Sale'), TYPE)
FOR XML PATH('abc')
您可以更深入地了解:
SELECT RTRIM(@Uname) AS '@uname',
RTRIM( @Pword) AS '@pword',
(SELECT COALESCE(PortOfLanding,'') AS '@portOfLanding',
(SELECT COALESCE(PortOfLanding,'') AS '@detail'
FROM Landings.LandingHeaders
FOR XML PATH('SaleLine'), TYPE)
FROM Landings.LandingHeaders
WHERE Posted = 0
FOR XML PATH('Sale'), TYPE)
FOR XML PATH('abc')
感谢一位 SO 用户,我在 FOR XML 方面取得了进展,但我显然没有完全正确地使用 sql 语法。
假设我有以下 sql
DECLARE @Uname VARCHAR(15) = 'Dom',
@Pword VARCHAR(15) = 'Monty'
SELECT
RTRIM(@Uname) AS '@uname',
RTRIM( @Pword) AS '@pword',
(SELECT COALESCE(PortOfLanding,'') AS portOfLanding
FROM Landings.LandingHeaders
WHERE Posted = 0
FOR XML PATH('Sale'))
FOR XML PATH('abc')
当运行时,它产生以下内容作为输出
<abc uname="Dom" pword="Monty"><Sale><portOfLanding>GBHTG</portOfLanding></Sale><Sale><portOfLanding>GBHTG</portOfLanding></Sale></abc>
我真正希望的是以下内容
<abc uname="Dom" pword="Python">
<Sale portOfLanding= "GBHTG" />
<Sale portOfLanding= "GBHTG"/>
</abc>
事实上,我想在 SQL 中添加第三部分,这样最终可能会像这样 xml
<abc uname="Dom" pword="Python">
<Sale portOfLanding= "GBHTG">
<saleline detail="some value here" />
<saleline detail="some value here" />
<Sale/>
<Salesnote portOfLanding= "GBHTG"/>
</abc>
有人可以指出我在原始 SQL 查询中哪里出错了吗?
谢谢
试试这个:
DECLARE @Uname VARCHAR(15) = 'Dom',
@Pword VARCHAR(15) = 'Monty';
SELECT RTRIM(@Uname) AS '@uname',
RTRIM( @Pword) AS '@pword',
(SELECT COALESCE(PortOfLanding,'') AS '@portOfLanding'
FROM Landings.LandingHeaders
WHERE Posted = 0
FOR XML PATH('Sale'), TYPE)
FOR XML PATH('abc')
您可以更深入地了解:
SELECT RTRIM(@Uname) AS '@uname',
RTRIM( @Pword) AS '@pword',
(SELECT COALESCE(PortOfLanding,'') AS '@portOfLanding',
(SELECT COALESCE(PortOfLanding,'') AS '@detail'
FROM Landings.LandingHeaders
FOR XML PATH('SaleLine'), TYPE)
FROM Landings.LandingHeaders
WHERE Posted = 0
FOR XML PATH('Sale'), TYPE)
FOR XML PATH('abc')