Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
sbelsare
Starting Member
4 Posts |
Posted - 2004-10-07 : 12:59:08
|
| I want to send 1 email with all clientname records which the cursor gets for me. My code however is sending 1 email for 1 record i.e clientname got from db. What's wrong? please help.I ano table to understand here about the while if right.thanks.+++++++++++++++++++++++++++++++++++++++++CREATE PROCEDURE test1AS declare @clientName varchar(1000)declare myCursor CURSOR STATICforselect client_namefrom clients--------------------------- now prepare and send out the e-mailsdeclare @ToEmail varchar(255)declare @FromEmail varchar(255)declare @Subject varchar(255)declare @Body varchar(2000)declare @UserID numeric(38)declare @UserName varchar(255)declare @SMTPServer varchar(100)set @SMTPServer = 'test.testserver.com'-- loop for each recordopen myCursorfetch next from myCursor into @clientName--loop now:while (@@fetch_status=0)begin -- while(@@fetch_status=0) -- check if valid "To" e-mail address was found if ((@clientName is null) or (ltrim(@clientName) = '')) begin --should not come here anytime ideally set @FromEmail = 'me@test.com' set @ToEmail = 'me@test.com' set @Subject = 'was emailed to wrong person' set @Body = 'the client name got is : '+ @clientName + 'client is null or empty' end --if else begin set @FromEmail = 'me@test.com' set @ToEmail = 'me@test.com' set @Subject = '-testing' set @Body = 'this will send ClientName:'+ @clientName end --end else -- send the e-mail --exec dbo.usp_SendCDOSysMailWithAuth @FromEmail, @ToEmail, @Subject, @Body, 0, @SMTPServer --fetch next from myCursor into @clientName fetch next from myCursor into @clientNameend --while(@@fetch_status=0)exec dbo.usp_SendCDOSysMailWithAuth @FromEmail, @ToEmail, @Subject, @Body, 0, @SMTPServerclose myCursordeallocate myCursor GO |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-10-07 : 13:38:19
|
A cursor is not needed top perform this:create table #clients (client_name varchar(20))insert into #clientsselect 'John Doe' union allselect 'Jane Doe' union allselect 'Yak Man'declare @clientName varchar(1000)set @clientname = 'ClientName: 'select @clientName = @clientName + client_name + ', 'from #clientsselect left(@clientname,len(@clientname)-1)drop table #clients |
 |
|
|
sbelsare
Starting Member
4 Posts |
Posted - 2004-10-07 : 13:51:48
|
| thanks. but,what is @clientName here? Is there a typo by mistake. I am trying to understand. |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-10-07 : 13:56:19
|
| @clientname is a local variable. The statement concatenates the local variable with a comma and the client names from the table #clients. |
 |
|
|
sbelsare
Starting Member
4 Posts |
Posted - 2004-10-07 : 14:33:16
|
| I meant the "clientname". what is it? there are total 3 similar names you are using.clientnameclient_nameclientName |
 |
|
|
ehorn
Master Smack Fu Yak Hacker
1632 Posts |
Posted - 2004-10-07 : 14:35:17
|
| I use the same 3 you use in your post??? Who is on first :) |
 |
|
|
sbelsare
Starting Member
4 Posts |
Posted - 2004-10-07 : 17:34:15
|
| what does this statement do?select left(@clientname,len(@clientname)-1) |
 |
|
|
spirit1
Cybernetic Yak Master
11752 Posts |
Posted - 2004-10-08 : 05:23:55
|
it eliminates the last comma from @clientname.Go with the flow & have fun! Else fight the flow |
 |
|
|
|
|
|
|
|