How to perform an SQL command that fills several lines

Asked

Viewed 69 times

1

I have a table where there are several records of supplies, now will be implemented a column in this table where will be saved a label that facilitates to find these supplies in the stock, I have already adjusted the table so that all new records when inserted into the table get a sequential number for these new records, but I need to add a label for the other supplies that have been added to the stock before. Is there any way to add a sequential number to multiple records with an SQL command?

  • Ever thought of an update? where the new field is null..

  • I think what you need, is incremental update, example: 1,2,3,4 etc, correct?

  • I believe that an incremental update really solves @Geiltonxavier, you can put an example in a reply?

  • I’ll add an example as a response.

1 answer

2


I believe that the example below can solve your problem.

-- Creating temporary table for testing

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

-- Look at the table

SELECT * FROM #tmp

-- FILLING COUNTER FIELD WITH INCREMENTAL VALUES

DECLARE @counter int
SET @counter = 0

UPDATE #tmp

SET @counter = counter = @counter + 1

-- See the table again with the counter field filled

SELECT * FROM #tmp
  • you can only edit the answer with the full SQL command?

  • I couldn’t implement what you suggested

  • you came to test this example?

  • I tried it but I couldn’t make it work

  • I don’t know if you understood the example, when I create the table #temp I create the counter column, this counter column would be your column, understand?

  • Ah good, now it’s working

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.