Reorganizar columnas consecutivamente en una base de datos


 I have a sample table of about 200 rows. The row contains a column, 'order_number', which contains a number equivalent to the level of the row, and the numbers are always consecutive. For example, the first row has 1, second row 2, third row 3, 100th row 100, etc. The numbers must always be consecutive.

So, here's the issue: if I decide to delete the row 50, there'll be a gap in the table (1-49, 51-200); how can I move the rows 51-200 up so that the everything becomes 1-199? There must be no gap in the 'order number' at all.

  Por favor, ayuda.

Gracias.

preguntado el 09 de marzo de 12 a las 14:03

1 Respuestas

If no other table references the order_number, you should be able to just do;

DELETE FROM TableA WHERE order_id = 50;                      -- What you just did
UPDATE TableA SET order_id = order_id-1 WHERE order_id > 50; -- To rearrange

Demostración simple aquí.

respondido 09 mar '12, 14:03

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.