dimanche 10 mai 2015

update a column with numeric serie

I have a table with a column name 'ln' (line number) if have 5 rows in my table and ln shows

 ln
 ---
 1
 2
 3
 4
 5

Then I delete the row where ln=3 I got

 ln
 ---
 1
 2
 4
 5

Now I want to regen 'ln' to be 1 2 3 4

I learned how to generate a list of number

WITH RECURSIVE
cnt(x) AS ( SELECT 1 UNION ALL SELECT x+1 FROM cnt  LIMIT 4 )
SELECT x FROM cnt;
x
----------
1
2
3
4

And I learned how to set the ln col to a value.

update file set ln = 0

But I can glue both

update file set ln = (WITH RECURSIVE cnt(x) AS
( SELECT 1 UNION ALL SELECT x+1 FROM cnt
  LIMIT (select count(ln) from file)
)
SELECT x FROM cnt)"

All ln are filled with the last value (4 here).

Thanx for any advices Cheers, Phi

Aucun commentaire:

Enregistrer un commentaire