Update column in sequential order

by Amol 16. August 2009 11:59

This example can be very useful if you would want to add sequence number to a column.Below example will update the table with sequence number even if the table contains duplicate values.

See the example below:

--Create the sequence table

CREATE TABLE Sequence (ID INT, [Name] VARCHAR(100))

GO

--Insert the data into Sequence table

INSERT INTO Sequence (ID, [Name])

SELECT NULL, 'xyz1' UNION ALL
SELECT NULL, 'xyz2' UNION ALL
SELECT NULL, 'xyz3' UNION ALL
SELECT NULL, 'xyz4' UNION ALL
SELECT NULL, 'xyz5' UNION ALL
SELECT NULL, 'xyz6' UNION ALL
SELECT NULL, 'xyz6' UNION ALL
SELECT NULL, 'xyz6' 

 GO

 

--Update the Sequence table with the value of ID

DECLARE @ID INT
SET @ID =
UPDATE Sequence SET @ID = ID = @ID + 1

--Query Sequence table

SELECT *
FROM Sequence;


Tags: , ,

SQL Server Scripts

Category

Recent Posts

Tag cloud