Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql Concantenating not working properly

Posted on 2013-02-01
5
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 33

Accepted Solution

by:
knightEknight earned 2000 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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this article I will describe the Backup & Restore 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 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