OUTPUT显示的不仅仅是ch3794,col 3794,Ln 35,

时间:2017-03-25 06:38:14

标签: sql-server

我想获得该表的所有记录,但它不会显示超过ch3794,col 3794,Ln 35

如何以HTML格式获取查询输出以通过电子邮件发送报告?

    DECLARE @xmlthree NVARCHAR(MAX)
DECLARE @bodythree NVARCHAR(MAX)

SET @xmlthree = CAST(( SELECT 
[SerName] AS 'td','',
[IPAddress] AS 'td','',
[SName] AS 'td','', 
[Status] AS 'td','',
[TTaken] AS 'td','',
[MName] AS 'td','',
[TTime] AS 'td'
FROM  [dbo].[Error-details]
ORDER BY TTaken DESC  
FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX))


SET @bodythree ='<html><body><H3>Web Report</H3>
<table border="1">
<tr>
<th> SerName </th> 
<th> IPAddress </th> 
<th> SName </th> 
<th> Status </th> 
<th> TTaken </th> 
<th> MName </th> 
<th> TTime </th> </tr>'    

SET @bodythree = @bodythree + @xmlthree +'</table></body></html>'
Print @bodythree
Go

我得到以下输出,OUTPUT中缺少一些记录: - the red mark in the image at the end of the line in my problme

2 个答案:

答案 0 :(得分:0)

这可能仅限于print的限制,print仅返回varchar的前8,000个字符和nvarchar的4,000个字符。

要打印短于16,000个字符的nvarchar个字符串,您可以使用:

print cast(@bodythree as ntext);

对于长于此的字符串,您可以使用:

declare @String nvarchar(max)=@bodythree;
declare @CurrentEnd bigint, @offset int;
while len(@String) > 1
begin
  if charindex(char(10), @String) between 1 and 4000
  begin
    set @CurrentEnd =  charindex(char(10), @String) -1;
    set @offset = 2;
  end
  else
  begin
    set @CurrentEnd = 4000;
    set @offset = 1;
  end   
  print substring(@String, 1, @CurrentEnd);
  set @string = substring(@String, @CurrentEnd+@offset, len(@String));
end;

此处答案的组合:How to print VARCHAR(MAX) using Print Statement?

对于第二种处理您情况的方法,您需要在身体中添加新行,如下所示:

declare @xmlthree nvarchar(max), @bodythree nvarchar(max);
set @xmlthree = replace(cast((
      select
          SerName as 'td',''
        , ipaddress as 'td',''
        , sname as 'td',''
        , [Status] as 'td',''
        , ttaken as 'td',''
        , mname as 'td',''
        , ttime as 'td'
      from  dbo.Error-details
      order by ttaken desc
      for xml path('tr'), elements ) as nvarchar(max))
    ,'</tr>','</tr>'+char(10))
set @bodythree ='<html><body><H3>Web Report</H3>
<table border="1">
<tr>
<th> SerName </th>
<th> ipaddress </th>
<th> sname </th>
<th> Status </th>
<th> ttaken </th>
<th> mname </th>
<th> ttime </th>
</tr>
'
set @bodythree = @bodythree + @xmlthree +'</table></body></html>';
declare @String nvarchar(max)=@bodythree;
declare @CurrentEnd bigint, @offset int;
while len(@String) > 1
begin
  if charindex(char(10), @String) between 1 and 4000
  begin
    set @CurrentEnd =  charindex(char(10), @String) -1;
    set @offset = 2;
  end
  else
  begin
    set @CurrentEnd = 4000;
    set @offset = 1;
  end
  print substring(@String, 1, @CurrentEnd);
  set @string = substring(@String, @CurrentEnd+@offset, len(@String));
end;

答案 1 :(得分:0)

不是向变量声明和赋值,而是尝试直接选择语句。像这样的东西

SELECT
    '<html><body><H3>Web Report</H3>
    <table border="1">
    <tr>
    <th> SerName </th> 
    <th> IPAddress </th> 
    <th> SName </th> 
    <th> Status </th> 
    <th> TTaken </th> 
    <th> MName </th> 
    <th> TTime </th> </tr>'+ISNULL(
    CAST(( SELECT 
    [SerName] AS 'td','',
    [IPAddress] AS 'td','',
    [SName] AS 'td','', 
    [Status] AS 'td','',
    [TTaken] AS 'td','',
    [MName] AS 'td','',
    [TTime] AS 'td'
    FROM  [dbo].[Error-details]
    ORDER BY TTaken DESC  
    FOR XML PATH('tr'), ELEMENTS ) AS NVARCHAR(MAX)),'')
    +'</table></body></html>'