AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Datenbanken Mit Update Datensatznummer setzen
Thema durchsuchen
Ansicht
Themen-Optionen

Mit Update Datensatznummer setzen

Ein Thema von BlueStarHH · begonnen am 21. Feb 2024 · letzter Beitrag vom 24. Feb 2024
Antwort Antwort
BlueStarHH

Registriert seit: 28. Mär 2005
Ort: Hannover-Hainholz
801 Beiträge
 
Delphi 11 Alexandria
 
#1

Mit Update Datensatznummer setzen

  Alt 21. Feb 2024, 19:55
Datenbank: Firebird • Version: 3.x • Zugriff über: IBDAC
Hallo,

ich habe diese Tabelle:
(ID ist der PK, Integer)

Code:
ID Text RowNo
10  a
35  b
41  c
Ich möchte per update nun die Spalte RowNo mit der Datensatznummer füllen. Dabei soll die Tabelle nach ID sortiert sein. Also

Code:
ID Text RowNo
10  a   1
35  b   2
41  c   3
Mein Versuch war:

Code:
update MyTable b1 set RowNo = (SELECT row_number() over(order by b2.ID) FROM MyTable b2 where b2.id = b1.ID);
Damit steht überall in RowNo eine 1
  Mit Zitat antworten Zitat
Benutzerbild von Sinspin
Sinspin

Registriert seit: 15. Sep 2008
Ort: Dubai
618 Beiträge
 
Delphi 10.3 Rio
 
#2

AW: Mit Update Datensatznummer setzen

  Alt 22. Feb 2024, 06:45
Hallo,

hol dir Max(RowNo) und addiere 1.
Das sollte besser funktionieren.
Stefan
Nur die Besten sterben jung
A constant is a constant until it change.
  Mit Zitat antworten Zitat
BlueStarHH

Registriert seit: 28. Mär 2005
Ort: Hannover-Hainholz
801 Beiträge
 
Delphi 11 Alexandria
 
#3

AW: Mit Update Datensatznummer setzen

  Alt 22. Feb 2024, 07:25
Hallo,

hol dir Max(RowNo) und addiere 1.
Das sollte besser funktionieren.
Wie stelle ich damit sicher, dass die Nummer fortlaufend sortiert nach der ID-Spalte vergeben wird?
  Mit Zitat antworten Zitat
Kas Ob.

Registriert seit: 3. Sep 2023
214 Beiträge
 
#4

AW: Mit Update Datensatznummer setzen

  Alt 22. Feb 2024, 08:20
Hi,

Well to my knowledge, all DB engines or most of the popular ones do have intrinsic ROWID and ROWNUM, with little difference in some places between different engines.

ROWNUM is what you are looking for, you didn't mention what DB SQL engine in question so... ROWNUM is most likely what you are looking for.

for extra information
Oracle : https://stackoverflow.com/questions/...owid-vs-rownum
SQLite : https://superuser.com/questions/1643...a-sqlite-query
MySQL : https://dev.mysql.com/doc/refman/8.0...ion_row-number there is another approach than the mentioned like "SELECT @rowid:=@rowid+1 as rowid FROM MY_TABLE, (SELECT @rowid:=0) as init"
MariaDB: https://mariadb.com/kb/en/rownum/
PgSQL : https://www.postgresql.org/docs/8.4/...ns-window.html
  Mit Zitat antworten Zitat
Jumpy

Registriert seit: 9. Dez 2010
Ort: Mönchengladbach
1.733 Beiträge
 
Delphi 6 Enterprise
 
#5

AW: Mit Update Datensatznummer setzen

  Alt 22. Feb 2024, 08:40
update MyTable b1 set RowNo = (Select x.nr From (SELECT id, row_number() over(order by ID) as nr FROM MyTable) X Where X.id=b1.id ;
Ralph
  Mit Zitat antworten Zitat
BlueStarHH

Registriert seit: 28. Mär 2005
Ort: Hannover-Hainholz
801 Beiträge
 
Delphi 11 Alexandria
 
#6

AW: Mit Update Datensatznummer setzen

  Alt 24. Feb 2024, 10:46
update MyTable b1 set RowNo = (Select x.nr From (SELECT id, row_number() over(order by ID) as nr FROM MyTable) X Where X.id=b1.id ;
Danke, das klappt!
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:09 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz