我需要使用游标从sql脚本生成XML。基本上我需要<Table><tr><td></td></tr></Table>
!我不知道如何开始,因为光标主题对我来说听起来很混乱。
DECLARE
@CustomerID int = 8,
@UtilityCompanyID int = 1
SELECT MD.SERV_ACCT,
AD.ACCOUNT,
AD.SERV_ADDRESS,
MAXINVDATE,
AD.SERV_ACCT + ', ' + AD.ACCOUNT + ', ' + AD.SERV_ADDRESS AS ADB
FROM
(SELECT DISTINCT SERV_ACCT, MAX(INV_DATE) as MAXINVDATE
FROM tblAPSData
WHERE SERV_ACCT NOT IN (SELECT ServiceAccount
FROM tblMEP_Meters
JOIN tblMEP_Sites
ON tblMEP_Meters.SiteID = tblMEP_Sites.ID
JOIN tblMEP_Projects
ON tblMEP_Projects.ID = tblMEP_Sites.ProjectID
WHERE CustomerID = 8
AND Type = 1
)
AND ACCOUNT IN (SELECT AccountNumber
FROM tblMEP_CustomerAccounts
WHERE CustomerID = @CustomerID
AND UtilityCompanyID = @UtilityCompanyID)
AND INV_DATE > DATEADD(month, -6, getdate())
GROUP BY SERV_ACCT) MD
join tblAPSdata AD
on MD.SERV_ACCT = AD.SERV_ACCT
and MD.MAXINVDATE = AD.INV_DATE
我希望XML中的结果是那样的
SERV_ACCT ACCOUNT SERV_ADDRESS ADB
0289S61288 117512280 4324E MONT 0289S61288, 117512280, 4324E MONT
1435S01282 117512280 432E SE 1435S01282, 117512280, 432E SE
2498S21288 117512280 reN 64 ST 2498S21288, 117512280, reN 64 ST
5538S21284 117512280 reE DOV 5538S21284, 117512280, reE DOV
答案 0 :(得分:2)
假设您的表格ServerAcct
包含您要显示的数据,我建议您使用FOR XML PATH
( 绝对不 一个游标!)来产生XML输出。
像这样的查询:
SELECT *
FROM dbo.ServerAcct
FOR XML PATH('ServerAccount'),ROOT('AllAccounts')
将产生此输出:
<AllAccounts>
<ServerAccount>
<Serv_Acct>0289S61288</Serv_Acct>
<Account>117512280</Account>
<Serv_Address>4324E MONT</Serv_Address>
<ADB>0289S61288, 117512280, 4324E MONT</ADB>
</ServerAccount>
<ServerAccount>
<Serv_Acct>1435S01282</Serv_Acct>
<Account>117512280</Account>
<Serv_Address>432E SE</Serv_Address>
<ADB>1435S01282, 117512280, 432E SE</ADB>
</ServerAccount>
<ServerAccount>
<Serv_Acct>2498S21288</Serv_Acct>
<Account>117512280</Account>
<Serv_Address>reN 64 ST </Serv_Address>
<ADB>2498S21288, 117512280, reN 64 ST</ADB>
</ServerAccount>
<ServerAccount>
<Serv_Acct>5538S21284</Serv_Acct>
<Account>117512280</Account>
<Serv_Address>reE DOV</Serv_Address>
<ADB>5538S21284, 117512280, reE DOV</ADB>
</ServerAccount>
</AllAccounts>
有关FOR XML PATH
以及如何使用它的详细信息,请参阅relevant MSDN SQL Server Books Online documentation - 它非常灵活且可高度自定义 - 您可以使用FOR XML PATH
执行任何操作!
那你的问题究竟是什么?
答案 1 :(得分:0)
您不需要游标来创建XML,有一个FOR XML
命令可以将查询结果转换为XML,请尝试使用它。有很多不同的选项,我相信你可以获得所需的XML。
这是最简单的示例,FOR XML AUTO
:
DECLARE
@CustomerID int = 8,
@UtilityCompanyID int = 1
WITH YourQuery AS
(
SELECT MD.SERV_ACCT,
AD.ACCOUNT,
AD.SERV_ADDRESS,
MAXINVDATE,
AD.SERV_ACCT + ', ' + AD.ACCOUNT + ', ' + AD.SERV_ADDRESS AS ADB
FROM
(SELECT DISTINCT SERV_ACCT, MAX(INV_DATE) as MAXINVDATE
FROM tblAPSData
WHERE SERV_ACCT NOT IN (SELECT ServiceAccount
FROM tblMEP_Meters
JOIN tblMEP_Sites
ON tblMEP_Meters.SiteID = tblMEP_Sites.ID
JOIN tblMEP_Projects
ON tblMEP_Projects.ID = tblMEP_Sites.ProjectID
WHERE CustomerID = 8
AND Type = 1
)
AND ACCOUNT IN (SELECT AccountNumber
FROM tblMEP_CustomerAccounts
WHERE CustomerID = @CustomerID
AND UtilityCompanyID = @UtilityCompanyID)
AND INV_DATE > DATEADD(month, -6, getdate())
GROUP BY SERV_ACCT) MD
join tblAPSdata AD
on MD.SERV_ACCT = AD.SERV_ACCT
and MD.MAXINVDATE = AD.INV_DATE
)
SELECT * FROM YourQuery FOR XML AUTO, ELEMENTS
<强> Simplified DEMO 强>
编辑:
如果你想将每一行作为一个单独的XML,可以这样做(我不是从上面重复整个查询,只是最后一部分):
WITH YourQuery AS
(
....
)
SELECT
(
SELECT
qXml.*
FROM YourQuery qXml
WHERE qXml.SERV_ACCT = q.SERV_ACCT --asuming this is your PK
FOR XML PATH ('')
)
from YourQuery q
<强> SQLFiddle DEMO 强>