join the MSSQLTips community

MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

 
SQL Server Cursor Examples - MSSQLTips

MSSQLTips

MSSQLTips.com - your daily source for SQL Server tips
Welcome to MSSQLTips Sign in | Join | Help
in Search

SQL Server Cursor Examples

Last post 01-27-2010 8:28 PM by admin. 5 replies.
Page 1 of 1 (6 items)
Sort Posts: Previous Next
  • 10-07-2008 12:30 AM

    SQL Server Cursor Examples

    This post is related to this tip: SQL Server Cursor Examples

    http://www.mssqltips.com/tip.asp?tip=1599

  • 10-07-2008 7:27 AM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 540

    Re: SQL Server Cursor Examples

    One thing that should be added... if you're going to use a cursor and the results do not have to be sensitive to change and you don't need to move back and forth within the set, then look at using the FAST_FORWARD option which is a low overhead, read only, forward only type with no tempdb overhead.  It runs through the resultset sequentially.

  • 10-09-2008 9:45 AM In reply to

    Re: SQL Server Cursor Examples

    I think another point to mention is to limit the amount of information obtained for use with the cursor.  I see a number of people who select everything from a table even though they only need two fields. 

  • 10-09-2008 5:36 PM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 540

    Re: SQL Server Cursor Examples

     Ahhhh, yes. Good one, Tim.  I can't tell you how many times I've seen SELECT * used when declaring a cursor.

  • 01-27-2010 9:37 AM In reply to

    Re: SQL Server Cursor Examples

    We are using a script very similar to this.  It uses exec msdb.dbo.sp_send_dbmail to email our clients appointment confirmations.  

    A sql view has been written that calls the data set correctly.   Some clients have two appts the same day for different reasons and at different times. Most emails are sent correctly, with the correct data. 

    But these records for clients that have two appts are sending only the data for one of the appts, and then sending it twice.  Any ideas?  Is there a flush command that can solve this?

    Cheers,

     

    Eric

  • 01-27-2010 8:28 PM In reply to

    Re: SQL Server Cursor Examples

    Eric,

    My first inclination would be that the query you use to populate the cursor is not getting the correct data.  If you just run the SELECT query in the 'Declare Cursor' portion of the code, are you getting the correct data? 

    If not, this code needs to be amended.

    If so, can you provide some sample data and the code?

    Thank you,
    The MSSQLTips Team

    Filed under:
Page 1 of 1 (6 items)