我需要使用游标从 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