如何将@@rowcount 添加到此 SQL 查询

Posted

技术标签:

【中文标题】如何将@@rowcount 添加到此 SQL 查询【英文标题】:How can I add @@rowcount to this SQL query 【发布时间】:2020-10-22 00:40:19 【问题描述】:
SET @tablehtml =
N'<H1>Report Heading</H1>' +
N'<table border="1">' +
N'<th>Check Number</th>' +
N'<th>Last Operator Date</th>' +
N'<th>Last Timestamp</th>' +
N'<th>Run Date</th>' +
N'<th>Issued Check Number</th>' +
N'<th>Error Description</th>' +
'<tr>' +
CAST ( ( SELECT td = S.CHK_NUM,       '',
                td = S.LAST_OPER_ID, '',
                td = S.LAST_TIMESTMP, '',
                td = S.RUN_DT, '',
                td = ISNULL(S.RE_ISSUE_CHK_NUM,-1), '',
                td = ISNULL(S.ERR_DESC,'<null>'), ''
          FROM STAGNG_CDDP_ERR_RCD S
          FOR XML PATH('tr'), TYPE 
) AS NVARCHAR(MAX) ) +
N'</table>' ;

PRINT @tableHTML

我想计算结果行并将其分配给一个变量。如何实现?

【问题讨论】:

【参考方案1】:

只需添加一个计数查询:

DECLARE @tableHTML NVARCHAR(MAX), @Count int;

SELECT @tableHTML =
    N'<H1>Report Heading</H1>' +
    N'<table border="1">' +
    N'<th>Check Number</th>' +
    N'<th>Last Operator Date</th>' +
    N'<th>Last Timestamp</th>' +
    N'<th>Run Date</th>' +
    N'<th>Issued Check Number</th>' +
    N'<th>Error Description</th>' +
    '<tr>' +
    CAST ( ( SELECT td = S.CHK_NUM,       '',
                    td = S.LAST_OPER_ID, '',
                    td = S.LAST_TIMESTMP, '',
                    td = S.RUN_DT, '',
                    td = ISNULL(S.RE_ISSUE_CHK_NUM,-1), '',
                    td = ISNULL(S.ERR_DESC,'<null>'), ''
              FROM STAGNG_CDDP_ERR_RCD S
              FOR XML PATH('tr'), TYPE 
    ) AS NVARCHAR(MAX) ) +
    N'</table>'
    , @Count = (SELECT COUNT(*) FROM STAGNG_CDDP_ERR_RCD);

PRINT @Count;
PRINT @tableHTML;

【讨论】:

以上是关于如何将@@rowcount 添加到此 SQL 查询的主要内容,如果未能解决你的问题,请参考以下文章

SQL Server中Rowcount与@@Rowcount的用法

SQL Server中Rowcount与@@Rowcount的用法

是否有可能从AJAX请求中获取rowCount()以在分页中使用它?

@@ROWCOUNT (Transact-SQL)

将递增值合并到此 sql

将@@ rowcount分配给SQL Server存储过程中的变量