数据类型varchar(max)和varchar(max)在'&'中不兼容操作者

时间:2014-04-26 12:23:38

标签: sql sql-server sql-server-2012

我正在尝试创建一个使用HTML格式的sp_send_dbmail,它将在1封电子邮件中发送两个不同的表格。但我一直在说:

  

数据类型varchar(max)和varchar(max)在'&'中不兼容。操作

此脚本从2个不同的视图中提取信息,这些视图填充了2个不同的表。

我正在使用SQL Server 2012。

DECLARE @MTDResults varchar(max)

SET @MTDResults = 
N'<style type="text/css">
#box-table
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
text-align: center;
border-collapse: collapse;
border-top: 7px solid #9baff1;
border-bottom: 7px solid #9baff1;
}
#box-table th
{
font-size: 13px;
font-weight: Bold;
background: #b9c9fe;
border-right: 2px solid #9baff1;
border-left: 2px solid #9baff1;
border-bottom: 2px solid #9baff1;
color: #039;
}
#box-table td
{
border-right: 1px solid #aabcfe;
border-left: 1px solid #aabcfe;
border-bottom: 1px solid #aabcfe;
color: #669;
}
tr:nth-child(odd)    { background-color:#eee; }
tr:nth-child(even)   { background-color:#fff; } 
</style>'+  
N'<H1><font color="Black">MTD Results</H1>'+
N'<table id="box-table">'+
N'<tr><font color = "Black">
<th> Type </th>
<th> Sales </th>
<th> Cost </th>
<th> Margin </th>
<th> Percentage </th>
<th> Tons </th>
</tr>'
+ CAST ( (
Select 
TD = Type,'', 
TD = Sales,'', 
TD = Cost,'', 
TD = Marg,'', 
TD = Tons
from [dbo].[Daily_Sales_MTD_HTML] 
FOR XML PATH ('tr')
) as varchar(max))
+ '</table>'

DECLARE @Top10CustMTD varchar(max)

SET @Top10CustMTD = 
N'<style type="text/css">
#box-table
{
font-family: "Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size: 12px;
text-align: center;
border-collapse: collapse;
border-top: 7px solid #9baff1;
border-bottom: 7px solid #9baff1;
}
#box-table th
{
font-size: 13px;
font-weight: Bold;
background: #b9c9fe;
border-right: 2px solid #9baff1;
border-left: 2px solid #9baff1;
border-bottom: 2px solid #9baff1;
color: #039;
}
#box-table td
{
border-right: 1px solid #aabcfe;
border-left: 1px solid #aabcfe;
border-bottom: 1px solid #aabcfe;
color: #669;
}
tr:nth-child(odd)    { background-color:#eee; }
tr:nth-child(even)   { background-color:#fff; } 
</style>'+  
N'<H1><font color="Black">Top 10 Customers For the Month</H1>'+
N'<table id="box-table">'+
N'<tr><font color = "Black">
<th> Customer Name </th>
<th> Sales </th>
<th> Margin </th>
<th> Percentage </th>
</tr>'
+ CAST ( (
Select 
TD = Custname,'',
TD = Sales,'',
TD = Margin,'',
TD = Perc,''
from [dbo].[Daily_Sales_10CustMTD_HTML]
FOR XML PATH ('tr')
) as varchar(max))
+ '</table>'

DECLARE @Mergedtable NVARCHAR(max)
set @Mergedtable = @MTDResults & @Top10CustMTD

EXEC msdb.dbo.sp_send_dbmail 
@recipients = 'JSmith@email.com',
@copy_recipients = '', 
@subject = 'Daily Sales',
@body = @Mergedtable,
@body_format = 'html'

3 个答案:

答案 0 :(得分:1)

set @Mergedtable = @MTDResults & @Top10CustMTD更改为set @Mergedtable = @MTDResults + @Top10CustMTD&对字符串连接无效。

答案 1 :(得分:1)

与Visual Basic不同,TSQL中的

&用于布尔操作,而不是用于varchar连接。对于它,您应该使用+,前提是两个条目都是varchars:

set @Mergedtable = @MTDResults + @Top10CustMTD

如果其中一个不是varchar,您首先必须将其转换为varchar(不是您的情况,仅供将来参考):

set @Mergedtable = @MTDResults + convert(varchar(max),@Top10CustMTD)

有关如何使用&的详细信息,请参阅Ampersand (&) operator in a SQL Server WHERE Clause

答案 2 :(得分:0)

谢谢托马斯。这为我解决了这个问题。

设置@Mergedtable = @MTDResults + @Top10CustMTD