我有一个以html格式写生日电子邮件的SQL ...它工作正常,但是如果我想用变量填充字段,它就不起作用。
例如ma p tag:
<p style="margin: 0; color: #f07d00; font-family: Open Sans, arial, sans-serif; font-size: 12px; font-weight: bold; text-transform: uppercase;">
@DISPONAME
</p>
因此,在显示@DISPONAME的电子邮件中,一个在sql顶部设置的变量,即html开始。 现在我想激活@DISPONAME,比如&#39; + @DISPONAME +&#39;并且它不起作用,电子邮件将被创建,直到此,其余为空白,@ DISPONAME行也不会显示...
有什么问题?如何正确插入?
谢谢!
埃里克
编辑: html启动前的SQL命令......
While (select top 1 id from dbo.Geburtstag_aktiv where send_date is NULL) is not NULL Begin DECLARE @ID int, @DISPONENT nvarchar(100), @DISPONAME nvarchar(100), @EMAILMA nvarchar(100), @EMAILDISPO nvarchar(50), @copy nvarchar(50) DECLARE @MOBIL as nvarchar(100), @DURCHWAHL as nvarchar(100), @BRIEFANREDE as nvarchar(100), @MABRIEFANREDE as nvarchar(100) DECLARE @sub nvarchar(255), @tableHTML nvarchar(MAX), @HomeServerUrl nvarchar(100), @BEDIENERTEXT as nvarchar(100), @BEDIENERFAX as nvarchar(100), @BEDIENERBERUF as nvarchar(100) SET @ID = (select top 1 id from dbo.Geburtstag_aktiv where send_date is NULL) SET @DISPONENT = (select LTRIM(RTRIM(DISPONENT)) from dbo.Geburtstag_aktiv where ID = @ID) SET @DISPONAME = (select LTRIM(RTRIM(DISPONAME)) from dbo.Geburtstag_aktiv where ID = @ID) SET @EMAILMA = (select LTRIM(RTRIM(EMAILMA)) from dbo.Geburtstag_aktiv where ID = @ID) SET @EMAILDISPO = (select LTRIM(RTRIM(EMAILDISPO)) from dbo.Geburtstag_aktiv where ID = @ID) SET @BRIEFANREDE = (select LTRIM(RTRIM(BRIEFANREDE)) from dbo.Geburtstag_aktiv where ID = @ID) SET @copy = copy SET @MOBIL = (select LTRIM(RTRIM(MOBIL)) from dbo.Geburtstag_aktiv where ID = @ID) SET @DURCHWAHL = (select LTRIM(RTRIM(DURCHWAHL)) from dbo.Geburtstag_aktiv where ID = @ID) SET @sub = 'Herzlichen Glückwunsch zum Geburtstag!' SET @HomeServerUrl = (select single_value from [dbo].[tb_admin_single_value] where single_value_desc = 'HomeServerUrl') SET @BEDIENERTEXT = (select LTRIM(RTRIM(BEDIENERTEXT2)) from dbo.Geburtstag_aktiv where ID = @ID) SET @BEDIENERFAX = (select LTRIM(RTRIM(FAX)) from dbo.Geburtstag_aktiv where ID = @ID) SET @BEDIENERBERUF = (select LTRIM(RTRIM(DISPOBERUF)) from dbo.Geburtstag_aktiv where ID = @ID) SET @MABRIEFANREDE = (select LTRIM(RTRIM(BRIEFANREDE)) from dbo.Geburtstag_aktiv where ID = @ID)
编辑: 发送电子邮件的代码:
SET @tableHTML =
'<html>'
Html code
</html>';
EXEC msdb.dbo.sp_send_dbmail
@recipients = @EMAILMA,
@profile_name = 'SERVER2',
@subject = @sub,
@body = @tableHTML,
@body_format = 'HTML'
编辑:这里有一些HTML代码,它不适用于@variable
<tr>
<td>
<a href="callto:035100000@DURCHWAHL" style="margin: 0; color: #000; font-family: Open Sans, arial, sans-serif; font-size: 10px; text-decoration: none;">
Tel.: 0351 / 00000-@DURCHWAHL
</a>
</td>
</tr>
答案 0 :(得分:2)
首先:您应该永远不要在字符串级别创建(X)HTML或XML !!
想象一下像
这样的东西SELECT '<p>' + 'This is bad & dangerous --> don''t do it!' + '</p>';
由于&
和>
,这会导致无效的XML / HTML!
尝试这样
SELECT 'margin: 0; color: #f07d00; font-family: Open Sans, arial, sans-serif; font-size: 12px; font-weight: bold; text-transform: uppercase;' AS [@style]
,'Some Content'
FOR XML PATH('p');
第二点是:
将创建电子邮件,其余为空白
如果因任何原因变量为NULL
,则连接将失败。
SELECT 'Something' + NULL; --returns NULL...
我在上面展示的方法在这里更宽容。只需使用NULL
进行试用即可获得有效答案:
SELECT 'margin: 0; color: #f07d00; font-family: Open Sans, arial, sans-serif; font-size: 12px; font-weight: bold; text-transform: uppercase;' AS [@style]
,NULL
FOR XML PATH('p');
Btw:SchönenGrußnachDresden,ich war da gerade :-D
您的查询一遍又一遍地调用相同的表值函数。这样的事情应该会更容易
SELECT @DISPONENT = LTRIM(RTRIM(DISPONENT))
,@DISPONAME = LTRIM(RTRIM(DISPONAME))
,@EMAILMA = LTRIM(RTRIM(EMAILMA))
--more variables
FROM dbo.Geburtstag_aktiv where ID = @ID;