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.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 how to get multiple rows with a fetch cursor

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 test1
AS

declare @clientName varchar(1000)


declare myCursor CURSOR STATIC
for


select client_name
from clients
-------------------------
-- now prepare and send out the e-mails
declare @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 record

open myCursor
fetch 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 @clientName

end --while(@@fetch_status=0)
exec dbo.usp_SendCDOSysMailWithAuth @FromEmail, @ToEmail, @Subject, @Body, 0, @SMTPServer
close myCursor
deallocate 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 #clients
select 'John Doe' union all
select 'Jane Doe' union all
select 'Yak Man'


declare @clientName varchar(1000)
set @clientname = 'ClientName: '

select @clientName = @clientName + client_name + ', '
from #clients

select left(@clientname,len(@clientname)-1)

drop table #clients
Go to Top of Page

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.
Go to Top of Page

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.
Go to Top of Page

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.
clientname
client_name
clientName
Go to Top of Page

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 :)
Go to Top of Page

sbelsare
Starting Member

4 Posts

Posted - 2004-10-07 : 17:34:15
what does this statement do?
select left(@clientname,len(@clientname)-1)
Go to Top of Page

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
Go to Top of Page
   

- Advertisement -