Solved

SQL Server database mail problem

Posted on 2010-09-15
6
1,306 Views
Last Modified: 2012-05-10
I am using the below code in SQL Server 2008 to query the database and send an email to user(s) based on the criteria in my select statement.

Problem is, that the variable @Emails is not getting anything. It should get the email address or multiple email address of the query to send using database mail.

I know that the query is returning rows, because if I comment out the "USE msdb" and below, I get a table of results. When I uncomment that code, I get an error which says, "At least one of the following parameters must be specified. "@recipients, @copy_recipients, @blind_copy_recipients"." So I know that it is not getting the @reciepents from the query.

Please help.
Thank you!!
USE BurgoyneMusic

GO
Declare @Emails varchar(50)

Select @Emails=@Emails + (a.Email) From Student As a Where exists (SELECT * From CreditCard As b WHERE a.StudentID = b.StudentID And b.CCMonth = month(DATEADD("m",-1,GETDATE())) And b.CCYear = DATEPART(year,GETDATE()))

USE msdb
EXEC sp_send_dbmail @profile_name='MyProfile',
@recipients= @Emails,
@subject='subject',
@body='message body'

Open in new window

0
Comment
Question by:mlg101
  • 2
  • 2
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Larissa T
ID: 33687209
null +'something' produce null
Initialize @Emails to empty string
----------------------
select @Emails=''
Select @Emails=@Emails + (a.Email) From Student As a Where exists (SELECT * From CreditCard As b WHERE a.StudentID = b.StudentID And b.CCMonth = month(DATEADD("m",-1,GETDATE())) And b.CCYear = DATEPART(year,GETDATE()))
0
 
LVL 4

Expert Comment

by:pbarry1
ID: 33687232
Hi,

could you please do a "PRINT @Emails" just before the "USE msdb" to see what is the result of the query.  It looks like you're not putting a separator between the email addresses.  So you're probably getting something like: "email1@abc.comemail2@abc.comemail3@abc.com ..." which is not a valid address.  

If you want to see the detailed error, you can look in the logging tables.  Here's one:
   SELECT * FROM msdb.dbo.sysmail_faileditems

P.S. You don't need to change the database context with "USE msdb".  You can prefix the call to sp_send_dbmail like this:
EXEC msdb..sp_send_dbmail @profile_name='MyProfile',
@recipients= @Emails,
@subject='subject',
@body='message body'
0
 
LVL 1

Author Comment

by:mlg101
ID: 33687314
Tro- I will try that when I get back to computer.

Pbarry- I designed my data to only return one row to start testing but I will try that too. And if I don't say "use msdb" then send dbmail is not declared.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 4

Accepted Solution

by:
pbarry1 earned 250 total points
ID: 33687616
One more thing,  unless you're using the "SET CONCAT_NULL_YIELDS_NULL OFF;" setting, if your query returns a record with a NULL email address, you'll end up with a NULL string.  You should check for NULL email addresses and replace it with something you want:  For instance:

Select @Emails=@Emails + ISNULL(a.Email, '') From Student As a Where...

As for the "USE msdb", I'm not sure why you get an error message, but this one will work (adding the "dbo" schema):
EXEC msdb.dbo.sp_send_dbmail @profile_name='MyProfile',
@recipients= @Emails,
@subject='subject',
@body='message body'
0
 
LVL 1

Author Comment

by:mlg101
ID: 33688242
pbarry - I tried the ISNULL and it gave me the error, "the isnull property requires 2 arguments". But I did change the EXEC msdb.dbo.sp_....to what you said and it was fine. Still not getting anything for @Emails. I tried "print @Emails" and it did nothing. Then I checked the error log like you suggested and it did not have an error for my recent tries with the way it all is.

Tro- I tried your new select @Emails = "Select....etc. and with the quotes around the 2nd Select, it says my argument is too long. It has to be 128 charachters or less. If I do it without the quotes around the 2nd select, then it says incorrect syntax near Select.
0
 
LVL 11

Assisted Solution

by:Larissa T
Larissa T earned 250 total points
ID: 33688469
1) emails  must be separated by ","
2) isnull does need 2 arguments, make sure you copy all code as is.
3) "print did nothing" - confirms that @Emails value is null after your select. Some of a.emails are nulls
4) what it means "with the quotes around the 2nd Select..." Can you post statement that gives you error?

try this
-----------------------
Declare @Emails varchar(526)
Select @Emails=isnull(@Emails+',','') + a.Email From Student As a
Where a.Email>''
and exists (SELECT * From CreditCard As b WHERE a.StudentID = b.StudentID And b.CCMonth = month(DATEADD("m",-1,GETDATE())) And b.CCYear = DATEPART(year,GETDATE()))

print '@Emails='+ isnull(@Emails, 'empty')
--USE msdb
EXEC msdb..sp_send_dbmail @profile_name='MyProfile',
@recipients= @Emails,
@subject='subject',
@body='message body'
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert formula to max at a specific date by month 18 22
Query Syntax 17 36
sql select record as one long string 21 25
SQL - Update field defined as Text 6 17
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question