DECLARE @tableHTML NVARCHAR(MAX) ;
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
如果我尝试用 varchar 文字“(Null)”替换 -1,则会收到错误消息
Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the varchar value 'Null' to data type int.
如果 RE_ISSUE_CHK_NUM 为空,我更愿意显示,但该字段是 int,我相信“TYPE”参数会强制返回值与原始列的类型匹配。
如何修改输出,使所有列都是文本,因此是可格式化的?
编辑
当我尝试这个...
DECLARE @tableHTML NVARCHAR(MAX) ;
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 = CONVERT(NVARCHAR(16), ISNULL(S.RE_ISSUE_CHK_NUM,'(Null)')), '',
td = ISNULL(S.ERR_DESC,'<null>'), ''
FROM STAGNG_CDDP_ERR_RCD S
FOR XML PATH('tr'), TYPE
) AS NVARCHAR(MAX) ) +
N'</table>' ;
PRINT @tableHTML
我明白了:
Msg 245, Level 16, State 1, Line 3
Conversion failed when converting the varchar value '(Null)' to data type int.
这是我想在 HTML 中显示字符串“Null”的 Null 的 Check Number 字段。
这是TYPE
问题的参数吗?