Results 1 to 8 of 8

Thread: [RESOLVED] SQL Update question

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Resolved [RESOLVED] SQL Update question

    I find myself needing to update a rather large table with a new field.
    What I need is for this new field to hold basically a row number. There are already a lot of records in the table and the rownumber field was just added so it is 0 on every row.

    What I am wondering is. Is there a simple query that can update every existing row with a value so that each row gets the next sequential value or am I going to need to loop through the db and do them all one by one?

    Thanks

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: SQL Update question

    What DBMS?
    Exact version please, because there is a way, but depends on the Version (MySQL for example)
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: SQL Update question

    The DB is FoxPro I think version 8, though I am trying to execute the query through code from within my program.

    I tried looping through and updating with a simple VB6 piece and it failed after just under 1500 records. The issue there being that there are cases where more than one row can contain the same data which is the reason for adding the new field so there is a way to determine which of those rows to update when needed.

    I do not know if this can be done given the current state of the data, I would like to avoid having to clear the table if at all possible.

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: SQL Update question

    Ouch.

    hmm.... if it's FoxPro (which is a Sub/Superset of DBF):
    1) Make a backupcopy of that table
    2) Import that Table into Excel
    3) In Excel in that additional Column do this "enter the first number and pull the mouse down" self-numbering magic
    4) Export that sheet to CSV
    5) Import that CSV into FoxPro (a new Table)
    6) Compare your original Table with that one
    7) If OK replace original table with that one

    If it were anything else i could give you a simple SQL_Statement
    OTOH, FoxPro has something called RECNO: https://learn.microsoft.com/en-us/pr...ectedfrom=MSDN
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  5. #5
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,605

    Re: SQL Update question

    Fox pro, no idea. If it has a max function somewhere then:


    Code:
    declare @i bigint  
    set @i = (SELECT ISNULL(MAX([newid]),0)  FROM  [Table_4] )
    
    update  [Table_4]
    set [newid]  = @i , @i = @i + 1
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] SQL Update question

    Thanks for that info on RecNo()
    Turns out to be simple after all

    Code:
    Update TableName Set RowNO=RecNO()
    Worked like a charm

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,440

    Re: [RESOLVED] SQL Update question

    Quote Originally Posted by DataMiser View Post
    Thanks for that info on RecNo()
    Turns out to be simple after all

    Code:
    Update TableName Set RowNO=RecNO()
    Worked like a charm
    Seriously????
    I feel like a blind chicken poking in a haystack.... LOL
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: [RESOLVED] SQL Update question

    LOL

    Yep, I was very surprised but had to try it and see, went through and looked at the data and everything looks good,

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width