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

Comments (1) -

Kiran Patil
Kiran Patil India
2/18/2011 6:04:24 AM #

A very useful and handy functions there amol.
keep them coming. !!

Add comment




  Country flag
biuquote
  • Comment
  • Preview
Loading


Category

Recent Posts

Tag cloud