Re: Help Troubleshooting this T-SQL
- From: Erland Sommarskog <esquel@xxxxxxxxxxxxx>
- Date: Tue, 7 Jul 2009 22:57:51 +0000 (UTC)
John Wright (JohnWright@xxxxxxxxxxxxxxxxxxxxxxxxx) writes:
OPEN PersonCursor
FETCH NEXT FROM PersonCursor
INTO @Name, @Email,@txtMail
While @@Fetch_Status = 0
BEGIN
EXEC msdb.dbo.sp_Send_dbmail
@profile_name='WZSQLAdmin',
@Recipients = @email,
@Subject = @mySubject,
@body = @Msg,
@body_format='HTML'
--Check to see if there is a text email
IF @txtMail is not null
BEGIN
EXEC msdb.dbo.sp_Send_dbmail
@profile_name='WZSQLAdmin',
@Recipients = @txtMail,
@Subject = @mySubject,
@body = @Msg,
@body_format='HTML'
END
FETCH NEXT FROM PersonCursor
INTO @Name, @Email
END
This procedure will only send to the first person in the list (in the
PresonCursor). It will not fetch the next record. Can anyone see why?
Yes. This is how you should code your cursor loops:
DECLARE cur CURSOR STATIC LOCAL FOR
SELECT cur
OPEN cur
WHILE 1 = 1
BEGIN
FETCH cur INTO ...
IF @@fetch_status <> 0
BREAK
-- Do your stuff
END
DEALLLOCATE cur
If you code your cursors this way, you will never commit the error you
have done twice in the SendEPNNotification procedure.
Of course, some TRY-CATCH to capture and log errors can help trouble-
shooting as well.
--
Erland Sommarskog, SQL Server MVP, esquel@xxxxxxxxxxxxx
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
.
- References:
- Help Troubleshooting this T-SQL
- From: John Wright
- Help Troubleshooting this T-SQL
- Prev by Date: Re: Variable server names
- Next by Date: Re: Update seems to be locked.
- Previous by thread: Help Troubleshooting this T-SQL
- Next by thread: sp_columns running slow
- Index(es):
Relevant Pages
|