T-SQL 字符串电子邮件格式问题
T-SQL string email format issue
我正在尝试创建电子邮件,但 运行 遇到了问题。当存储过程 运行s 时,我得到以下错误信息。
Msg 14624, Level 16, State 1, Procedure sp_send_dbmail, Line 242
At least one of the following parameters must be specified. "@body,
@query, @file_attachments, @subject".
我的代码在下面,但我正在添加每个请求的项目。我已经缩小了发生故障的范围。如果我拉出连接“+”,一切都会按预期进行。但是我之前已经通过连接完成了这个,所以我不确定有什么不同。
DECLARE @RespPeriod varchar(20)
DECLARE @SubjectLine varchar(100)
DECLARE @ContactEmail varChar(100)
DECLARE @AAEAPVSupplierID int
DECLARE @key varchar(50)
DECLARE @formattedURL varchar(100)
DECLARE @emailBody varchar(max)
DECLARE Curs Cursor
FOR
SELECT theID FROM #temptbl
OPEN Curs
FETCH NEXT FROM Curs INTO @theID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT *
INTO #temptbl
FROM tblmainTbl
WHERE theID = @theID
DECLARE @isComplete Bit = 1
IF EXISTS (SELECT * FROM #temptbl WHERE Complete = 0)
BEGIN
SET @isComplete = 0
END
IF @isComplete = 1
BEGIN
SET @SubjectLine = 'Testing ' + @RespPeriod + ' Testing.'
SET @ContactEmail = (SELECT SalesEmail FROM #temptbl WHERE theID = @theID)
SET @key = (SELECT ResponseKEY FROM #temptbl WHERE theID = @theID)
SET @formattedURL = 'http://www.something.com/something.aspx?rkey=' + @key
SET @emailBody = '<html>Dear BlaBlaBla' + @RespPeriod + ' ' + @formattedURL + '">' + @formattedURL + '</a></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SMTPProfile'
,@recipients = @ContactEmail
,@subject = @SubjectLine
,@body = @emailBody
,@body_format = 'HTML'
END
DROP TABLE #temptbl
FETCH NEXT FROM Curs INTO @theID
END
CLOSE Curs
DEALLOCATE Curs
您的代码示例不完整(您缺少所使用的某些变量的声明)。我的预感是一个或多个变量值(可能是@RespPeriod?)为 NULL,当您对 sp_send_dbmail
调用中使用的变量赋值进行连接时,您传递的是 NULL。
记住,字符串 + NULL = NULL
就在您调用 sp_send_dbmail
之前,插入这些语句...
PRINT '--------------'
PRINT '@SubjectLine = ' + ISNULL(@SubjectLine, 'NULL')
PRINT '@ContactEmail = ' + ISNULL(@ContactEmail, 'NULL')
PRINT '@key = ' + ISNULL(@key, 'NULL')
PRINT '@formattedURL = ' + ISNULL(@formattedURL, 'NULL')
PRINT '@emailBody = ' + ISNULL(@emailBody, 'NULL')
PRINT '--------------'
如果这是您的原因,应该很快就会明白。如果是,追回解析为 NULL 的任何变量的各个部分,直到找到导致整个字符串为 NULL 的部分。如果不是,请提供更多代码,以便我们查找其他地方。
我正在尝试创建电子邮件,但 运行 遇到了问题。当存储过程 运行s 时,我得到以下错误信息。
Msg 14624, Level 16, State 1, Procedure sp_send_dbmail, Line 242
At least one of the following parameters must be specified. "@body, @query, @file_attachments, @subject".
我的代码在下面,但我正在添加每个请求的项目。我已经缩小了发生故障的范围。如果我拉出连接“+”,一切都会按预期进行。但是我之前已经通过连接完成了这个,所以我不确定有什么不同。
DECLARE @RespPeriod varchar(20)
DECLARE @SubjectLine varchar(100)
DECLARE @ContactEmail varChar(100)
DECLARE @AAEAPVSupplierID int
DECLARE @key varchar(50)
DECLARE @formattedURL varchar(100)
DECLARE @emailBody varchar(max)
DECLARE Curs Cursor
FOR
SELECT theID FROM #temptbl
OPEN Curs
FETCH NEXT FROM Curs INTO @theID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT *
INTO #temptbl
FROM tblmainTbl
WHERE theID = @theID
DECLARE @isComplete Bit = 1
IF EXISTS (SELECT * FROM #temptbl WHERE Complete = 0)
BEGIN
SET @isComplete = 0
END
IF @isComplete = 1
BEGIN
SET @SubjectLine = 'Testing ' + @RespPeriod + ' Testing.'
SET @ContactEmail = (SELECT SalesEmail FROM #temptbl WHERE theID = @theID)
SET @key = (SELECT ResponseKEY FROM #temptbl WHERE theID = @theID)
SET @formattedURL = 'http://www.something.com/something.aspx?rkey=' + @key
SET @emailBody = '<html>Dear BlaBlaBla' + @RespPeriod + ' ' + @formattedURL + '">' + @formattedURL + '</a></html>'
EXEC msdb.dbo.sp_send_dbmail
@profile_name = 'SMTPProfile'
,@recipients = @ContactEmail
,@subject = @SubjectLine
,@body = @emailBody
,@body_format = 'HTML'
END
DROP TABLE #temptbl
FETCH NEXT FROM Curs INTO @theID
END
CLOSE Curs
DEALLOCATE Curs
您的代码示例不完整(您缺少所使用的某些变量的声明)。我的预感是一个或多个变量值(可能是@RespPeriod?)为 NULL,当您对 sp_send_dbmail
调用中使用的变量赋值进行连接时,您传递的是 NULL。
记住,字符串 + NULL = NULL
就在您调用 sp_send_dbmail
之前,插入这些语句...
PRINT '--------------'
PRINT '@SubjectLine = ' + ISNULL(@SubjectLine, 'NULL')
PRINT '@ContactEmail = ' + ISNULL(@ContactEmail, 'NULL')
PRINT '@key = ' + ISNULL(@key, 'NULL')
PRINT '@formattedURL = ' + ISNULL(@formattedURL, 'NULL')
PRINT '@emailBody = ' + ISNULL(@emailBody, 'NULL')
PRINT '--------------'
如果这是您的原因,应该很快就会明白。如果是,追回解析为 NULL 的任何变量的各个部分,直到找到导致整个字符串为 NULL 的部分。如果不是,请提供更多代码,以便我们查找其他地方。