How do I update top 10 in sql 2000?

  • How do you update the top 10 in sql 2000?

    Heare is what I have so far, but it updates all the records in the table instead of just the top 10 for example:

    UPDATE table_info

    SET q_active = '1'

    FROM (SELECT DISTINCT TOP 10 q_active FROM table_info)

    AS q_temp

    WHERE table_info.q_active = '0'

     

  • I don't know your table definition.

    But do you have a primary key on the table?

    if so

    UPDATE table_info

    SET q_active = '1'

    WHERE table_info.pkfield in (select top 10 pkField from table_info where table_Info.q_active = 0)

     

  • There is no primary key attached to the table

  • This works great, but is there also a way for it to keep updating 10 more fields everytime it is run?

  • hmm, if there is no primary key  on table then how did that work?

    If you continue to run that query over and over it will eventually update all rows.

    if you want, post your table definition and some sample data, and tell us why you want to update 10 rows at a time, every time you run it?

    http://www.aspfaq.com/etiquette.asp?id=5006

    you can also run a while loop until its all done
     
    while (select count(*) from table_Info where table_info.q_active = '0') > 0

    begin

        UPDATE table_info

        SET q_active = '1'

        WHERE table_info.pkfield in (select top 10 pkField from table_info where table_Info.q_active = 0)

    end

  • Thank you! it's working.  The reason I need to update a little bit at a time is because I already have a table with info in it, and I only want to display a small portion of it at a time.

  • Hi,

        The same is possible with simple Set RowCount

    Set rowcount 10

    UPDATE table_info

        SET q_active = '1'

        WHERE table_info.pkfield in (select top 10 pkField from table_info where table_Info.q_active = 0)

     

    Thanks,

    Murthy.

     

Viewing 7 posts - 1 through 6 (of 6 total)

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