How to perform a SQL command that takes several lines

1

I have a table where there are several records of supplies, now a column will be implemented in this table where a label will be implemented to facilitate finding these supplies in stock, I have already adjusted the table so that all new records when inserted in the table I earn a sequential number for these new records, however I need to add a label to the other supplies that have already been added to the inventory before. Is there any way to add a sequential number to multiple records with an SQL statement?

    
asked by anonymous 21.07.2017 / 19:23

1 answer

2

I believe the example below can solve your problem.

- Creating temporary table for tests

CREATE TABLE #tmp (id int primary key identity(1,1), counter int default 0)
GO

- Inserting data into the table

INSERT INTO #tmp DEFAULT VALUES
GO 10

- See how the table is

SELECT * FROM #tmp

- FILLING THE FIELD counter WITH INCREMENTABLE VALUES

DECLARE @counter int
SET @counter = 0

UPDATE #tmp

SET @counter = counter = @counter + 1

- Look again at the table with the counter filled field

SELECT * FROM #tmp
    
21.07.2017 / 20:11