-
Mar 23rd, 2023, 08:37 AM
#1
[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
-
Mar 23rd, 2023, 08:48 AM
#2
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
-
Mar 23rd, 2023, 09:20 AM
#3
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.
-
Mar 23rd, 2023, 09:31 AM
#4
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
-
Mar 23rd, 2023, 09:43 AM
#5
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
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Mar 23rd, 2023, 09:55 AM
#6
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
-
Mar 23rd, 2023, 10:00 AM
#7
Re: [RESOLVED] SQL Update question
 Originally Posted by DataMiser
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
-
Mar 23rd, 2023, 10:02 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|