1

我有这个代码:

DECLARE 

 @CustomerID int = 8,
 @UtilityCompanyID int = 1

SELECT MD.SERV_ACCT, 
       AD.ACCOUNT,
       MD.SERV_ADDRESS
       ----AD.DOING_BUSINESS_AS,
       --MD.SERV_ACCT + ', ' + AD.ACCOUNT + ', ' + MD.SERV_ADDRESS AS ADB
FROM
(SELECT DISTINCT SERV_ACCT, SERV_ADDRESS, 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, SERV_ADDRESS) MD
join tblAPSdata AD on MD.SERV_ACCT = AD.SERV_ACCT 
and MD.MAXINVDATE = AD.INV_DATE

这是输出:

SERV_ACCT   ACCOUNT         SERV_ADDRESS
0289S61288  117512280   921 N dsdijdsi
1435S01282  117512280   238 D dsdijads 
2498S21288  117512280   23814 4fksf 
5538S21284  117512280   4324 fjsdf
6109S12286  117512280   0912  fuehsf
7488S22289  117512280   242 fsjdhfs
8058S12287  117512280   309 fjsnef
9058S00288  117512280   342 fhsfuqq
9645S21281  117512280   556 dajdjawd
1440S13289  312937281   2383 hfsdufh
5384S92284  979437282   324 dhauda
0492S90281  117512280   29349 dhsadfsf
1303S90280  117512280   89e3y djasd 
1548S00286  117512280   3426 iodjad
7358S00281  117512280   39844 jdfasjf
9168S00282  117512280   399 ndfsfjk

我想要以 HTML 格式的输出!那可能吗?有谁知道 SQL Server 是否可以做到这一点?还是我必须手动编码?我读到了字符串分隔符,但我不确定这是否是我真正想做的,因为我什至不知道该怎么做。

4

2 回答 2

1

不可以。您必须将 SQL 与外部工具(编程语言、CMS、批处理文件等)结合起来。

于 2013-07-16T05:42:42.527 回答
0

我讨厌有人过来,看到这不能(技术上)在 SQL 中完成,所以这是解决方案:

DECLARE 
    @CustomerID int = 8,
    @UtilityCompanyID int = 1

    ;
    WITH Html AS (
        SELECT 1 AS SortOrder,'<table>' AS Content
        UNION ALL SELECT 2,'<tr><th>SERV_ACCT</th><th>ACCOUNT</th><th>SERV_ADDRESS</th></tr>'
        UNION ALL SELECT 3,'<tr><td>'+ MD.SERV_ACCT +'</td><td>'+ AD.ACCOUNT +'</td><td>'+ MD.SERV_ADDRESS +'</td></tr>'
        FROM
(SELECT DISTINCT SERV_ACCT, SERV_ADDRESS, 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, SERV_ADDRESS) MD
join tblAPSdata AD on MD.SERV_ACCT = AD.SERV_ACCT 
and MD.MAXINVDATE = AD.INV_DATE

        UNION ALL SELECT 4,'</table>'
    )
    SELECT Content FROM Html ORDER BY SortOrder

但这不是最有效的方法,我不建议将它用于网站或 Web 应用程序。

于 2013-07-16T08:36:43.837 回答