Select And Update Records

  • I would like to grab 10,000 records, update a flag in those records, then return the recordset. I would like to do that all in one stored procedure.

    Note: I don't necessarily want to return the updated records. It's kind of like this:

    Select Top 10000 Email From tblEmails Where Flag <> 'P';

    Then...Set the flag of those Emails to 'P' and return them.

    Does that make sence?

    Any suggestions?

  • Are you just asking how you might update the first 10,000 records? Is so try this:

    update tblEmails

    set Flag = 'P' where email in (select top 10000 email from tblEmails where Flag <>'P')

    -------------------------

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • Thats half of it.....I know how to update it like that, but I want to return the emails that I just updated.

    Any suggestions?

  • You can do both but not as one statement. This does what you state.

    update tblEmails

    set Flag = 'P' where email in (select top 10000 email from tblEmails where Flag <>'P')

    select top 10000 email from tblEmails

    Or if you want the flag involved try creating a temp table to store the identifiers for the records, use it to update them and return those records basically same as previous.

    "Don't roll your eyes at me. I will tape them in place." (Teacher on Boston Public)

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply