How to update a field Id

1

I have a migrated information to a table I need in that field to create a number that starts from the 16th and ends until the last record that is in that column.

    
asked by Pedro Ávila 16.10.2018 в 05:58
source

1 answer

1

First I recommend you try this on a copy of your data

I show you the following example and how it occurred to me to solve it

I CREATE A TABLE WITH THE FOLLOWING STRUCTURE

CREATE TABLE demo(
 id INT,
 name varchar(20)
);

I INSERT A DATA SERIES WHERE I NOTICE I HAVE A REPEATED ID SIMILAR TO YOUR SCENARIO

insert into demo(id, name)
values
(1, 'alfa'),
(1, 'beta'),
(1, 'gama'),
(1, 'delta'),
(1, 'teta');

IF NOW I DO A SELECT

select * from demo;

I GET THE FOLLOWING DATA

id  name
1   alfa
1   beta
1   gama
1   delta
1   teta

NOW THROUGH THE DECLARATION OF A VARIABLE I WILL USE AS A COUNTER TO INCREASE THE VALUE

  

To initialize the counter , I establish that its value increases in   1 starting from number 15 when the id is equal to 1, that for the case   from my example all records have that id and therefore they   apply to each record

DECLARE @counter INT = 15;

UPDATE demo SET id = @counter, @counter = @counter + 1 WHERE id = 1;

I DO A NEW SELECT

select * from demo;

THE RESULT THAT YOU WOULD OBTAIN

id  name
16  alfa
17  beta
18  gama
19  delta
20  teta

It's an approach I hope you serve

    
answered by 16.10.2018 / 06:47
source