Sequentielle Nummerierung durch UPDATE

Vor einiger Zeit habe ich auf SQLTeam.com etwas sehr Nützliches entdeckt. Die Generierung einer sequentiellen Nummerierung mit Hilfe eines einzelnen UPDATE Statements.

SET NOCOUNT ON
CREATE TABLE SEQ_T
(
col1 INT DEFAULT 0
)
DECLARE @SEQUENCE INT
SET @SEQUENCE = 0
WHILE @SEQUENCE <=10
BEGIN
INSERT INTO SEQ_T DEFAULT VALUES
SET @SEQUENCE = @SEQUENCE+1
END
SET NOCOUNT OFF
SELECT * FROM SEQ_T
SET @SEQUENCE = 0
UPDATE SEQ_T SET @SEQUENCE = col1 = @SEQUENCE + 1
SELECT * FROM SEQ_T
DROP TABLE SEQ_T

col1
-----------
0
0
0
0
0
0
0
0
0
0
0

(11 row(s) affected)


(11 row(s) affected)

col1
-----------
1
2
3
4
5
6
7
8
9
10
11

(11 row(s) affected)

...einziger Nachteil ist, dass man nicht explizit in der Lage ist, die Sortierung vorzugeben.

Noch kein Feedback
Einen Kommentar hinterlassen

Ihre E-Mail-Adresse wird nicht auf dieser Seite angezeigt.
SchlechtExzellent
(Zeilenumbrüche werden zu <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)
Trackback-Adresse für diesen Eintrag

http://www.insidesql.org/blogs/htsrv/trackback.php?tb_id=1055