Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql “&”运算符中的数据类型varchar(max)和varchar(max)不兼容_Sql_Sql Server_Sql Server 2012 - Fatal编程技术网

Sql “&”运算符中的数据类型varchar(max)和varchar(max)不兼容

Sql “&”运算符中的数据类型varchar(max)和varchar(max)不兼容,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,我正在尝试创建一个sp_send_dbmail,它使用HTML格式,将在一封电子邮件中发送两个不同的表。但我一直得到: “&”运算符中的数据类型varcharmax和varcharmax不兼容 此脚本从填充2个不同表的2个不同视图中提取信息 我正在使用SQL Server 2012 DECLARE @MTDResults varchar(max) SET @MTDResults = N'<style type="text/css"> #box-table { font-famil

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

“&”运算符中的数据类型varcharmax和varcharmax不兼容

此脚本从填充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'
将set@Mergedtable=@MTDResults&@Top10CustMTD更改为set@Mergedtable=@MTDResults+@Top10CustMTD.&对于字符串连接无效。

&与Visual Basic不同,TSQL中的用于布尔运算,而不是varchar连接。对于它,您应该使用+,前提是两个条目都是varchar:

set @Mergedtable = @MTDResults + @Top10CustMTD
如果其中一个不是varchar,您首先必须将其转换为varchar,而不是您的案例,仅供将来参考:

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

有关如何使用和的更多信息,请参见谢谢托马斯。那帮我修好了

设置@Mergedtable=@MTDResults+@top10customtd