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 2005 Forums
 Transact-SQL (2005)
 loop records in a stored proc???

Author  Topic 

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2009-04-08 : 17:01:37
I need to loop threw records and grab the data to send out emails in the stored proc. How do I do this?

Dave
Helixpoint Web Development
http://www.helixpoint.com

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-04-08 : 17:12:59
using a cursor. But if you post more on this, sample data, output etc etc, we might be able to suggest a set based approach.
Go to Top of Page

helixpoint
Constraint Violating Yak Guru

291 Posts

Posted - 2009-04-08 : 17:42:54
Does not seem to loop

ALTER PROCEDURE [dbo].[sendHelixMail]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

Declare firstQuery Cursor for
SELECT BatchSchedule.emailAdd
FROM BatchEmailSchedule INNER JOIN
BatchSchedule ON BatchEmailSchedule.batchID = BatchSchedule.batchID
WHERE (BatchEmailSchedule.emailDate <= GETDATE())
for Read Only;

Declare @emailAdd nvarchar(75);
Open firstQuery;

Fetch Next from firstQuery Into @emailAdd;
WHILE (@@fetch_status <> -1)
print @emailAdd


close firstQuery;

deAllocate firstQuery;
END

Dave
Helixpoint Web Development
http://www.helixpoint.com
Go to Top of Page

sakets_2000
Master Smack Fu Yak Hacker

1472 Posts

Posted - 2009-04-08 : 17:59:30
try if it loops with this,

ALTER PROCEDURE [dbo].[sendHelixMail] 
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

Declare firstQuery Cursor for
SELECT BatchSchedule.emailAdd
FROM BatchEmailSchedule INNER JOIN
BatchSchedule ON BatchEmailSchedule.batchID = BatchSchedule.batchID
WHERE (BatchEmailSchedule.emailDate <= GETDATE())
for Read Only;

Declare @emailAdd nvarchar(75);
Open firstQuery;

Fetch Next from firstQuery Into @emailAdd;
WHILE (@@fetch_status <> -1)
begin
print @emailAdd
Fetch Next from firstQuery Into @emailAdd;
end

close firstQuery;

deAllocate firstQuery;
END
Go to Top of Page
   

- Advertisement -