Solved

sql Concantenating not working properly

Posted on 2013-02-01
5
235 Views
Last Modified: 2013-02-01
So the objective is not to send out 1200 emails form my sql server but only send out 20 by concantenating the emails together, but when I run the proc below it does not concantonate them it is blank, but if I change to individual send out it works just fine.  This should work but its not working one bit, what amd I doing wrong here.  So what I am doing is counting 50 emails, concantonating them and then sending it out, but no emails get concatenated.


Select @ii = COUNT(RowId) from #TempMail;
      set @i = 1;
      Set @Send = 1
            While @i <= @ii
              BEGIN
             Select @Name = Name, @Email = Mail from #TempMail where Rowid = @i;
                  IF @Send < 50
                                                     BEGIN
                          set @Receipt = @Receipt + @Email + ';'
                                                     END
                  ELSE
                        BEGIN
                      --- do send the email here ...
                   Set @Subject = 'AnualHR Test'
                  Set @Body = @Name
                               
                   EXEC   msdb.dbo.sp_send_dbmail
                   @recipients = @Receipt,
                   @body = @Body,
                   @subject = @Subject,
                   @profile_name = 'Testmail'
                                            
                  Set @Send = 1;
                  Set @Receipt = '';
                  END
            set @Send = (@Send + 1)
            set @i = (@i + 1);
              END
             
      Drop table #TempMail;
0
Comment
Question by:kdeutsch
5 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 500 total points
ID: 38843793
Did you initialize the @Receipt variable to '' ?  (Do this before entering the loop)

Also, make sure you declare it long enough to hold 50 addresses:

declare @Receipt varchar(8000) = ''
0
 
LVL 21

Expert Comment

by:mastoo
ID: 38843811
Yes, if you don't initialize @Receipt it will be null, and concatenating anything with null results in null
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 38843858
more like this

declare @recipt varchar(8000)

   Select @Subject = 'AnualHR Test' 
                ,@Body = @Name
              ,@receipt=''
                
Select @ii = COUNT(RowId) ,@i=1,@send=1
  from #TempMail;
     
            While @i <= @ii
              BEGIN
             Select @receipt=@receipt+';'+@email
                from #TempMail 
            where Rowid between  @i and @i+49
             Select @Name = Name, @i = @i+50
                from #TempMail 
             where Rowid = @i;
              
             select @receipt=stuff(@receipt,1,1,'')     -- remove first semi-colon
                               
                   EXEC   msdb.dbo.sp_send_dbmail
                   @recipients = @Receipt,
                   @body = @Body, 
                   @subject = @Subject,
                   @profile_name = 'Testmail'
                                            
                  Set @Receipt = '';
                  END
           
              
      Drop table #TempMail;

Open in new window

0
 

Author Comment

by:kdeutsch
ID: 38843979
All,

Well, it was that i did not inialize the variable @Receipt, I called it out but forgot to inialize it.  But while looking at this here is my problem I want to send in groups of 50, but what happens if the last group is below 50, those personnel will not get the email, so this will really not work as some personnel will not get the email.
0
 

Author Closing Comment

by:kdeutsch
ID: 38844066
Thanks, thought I initalized and totally over looked it.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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