Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   Mit Update Datensatznummer setzen (https://www.delphipraxis.net/214686-mit-update-datensatznummer-setzen.html)

BlueStarHH 21. Feb 2024 19:55

Datenbank: Firebird • Version: 3.x • Zugriff über: IBDAC

Mit Update Datensatznummer setzen
 
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

Sinspin 22. Feb 2024 06:45

AW: Mit Update Datensatznummer setzen
 
Hallo,

hol dir Max(RowNo) und addiere 1.
Das sollte besser funktionieren.

BlueStarHH 22. Feb 2024 07:25

AW: Mit Update Datensatznummer setzen
 
Zitat:

Zitat von Sinspin (Beitrag 1533781)
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?

Kas Ob. 22. Feb 2024 08:20

AW: Mit Update Datensatznummer setzen
 
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

Jumpy 22. Feb 2024 08:40

AW: Mit Update Datensatznummer setzen
 
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 ;

BlueStarHH 24. Feb 2024 10:46

AW: Mit Update Datensatznummer setzen
 
Zitat:

Zitat von Jumpy (Beitrag 1533786)
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!


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:57 Uhr.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz