Jump to content

Need help with looping through table records


Sam99

Recommended Posts

I am trying to create a TSQL script that can loop through each record in the table and send email notification if the Run_Status is Error.The email message should include the Run_Status and the File_Name results. Such as Deal B is Error.. The below TSQL script only displays the first record and it seems that it does not loop through other records.

DECLARE @RunStatus varchar(25)DECLARE status_cursor CURSOR    FOR SELECT Run_Date, File_Name, Record_Count, Run_Status, Run_Remarks FROM dbo.Daily_Check_LogOPEN status_cursorSet @RunStatus = nullFETCH NEXT FROM status_cursorIF @RunStatus = 'Error'BEGINPrint 'Error'EXEC msdb.dbo.sp_send_dbmail@profile_name = 'JobsNotifications',@recipients = 'my@email.com',@subject = 'Test',@body = 'Records Not Found.'ENDELSEPrint 'Success' CLOSE status_cursor;DEALLOCATE status_cursor;

My table records are shown as Run_Date File_Name Record_Count Run_Status Run_Remarks2013-02-16 Deals B 1902 Success Records Found 2013-02-17 Deals 0 Error No Record 2013-02-18 Deals A 2964 Success Records Found 2013-02-19 Deals A 0 Error No Record

Link to comment
Share on other sites

It doesn't look like there is any loop structure there, it looks like it opens the cursor, gets the first result, does the if statement, then closes the cursor. It looks like there should be a loop structure to go through all records in the cursor.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...