Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,
I have created a table and populated some records. But, I missed to populate the serial number on Uid column.

How to populate sequence number on these 5 records(101 to 105) - LOOP should not be used here...!

Is there any simple UPDATE ?

Uid UName Phone

-----------------------------

NULL ABC1 12345

NULL EFG1 67890

NULL JKL1 13567

NULL MNO1 24680

NULL QWS1 67981
Posted

Here is a sample code

CREATE TABLE #Table1
(
	Uid INT,
	UName VARCHAR(50),
	Phone VARCHAR(50)
)

INSERT INTO #Table1
SELECT NULL, 'ABC1', '12345' UNION ALL
 
SELECT NULL, 'EFG1', '67890' UNION ALL
 
SELECT NULL, 'JKL1', '13567' UNION ALL
 
SELECT NULL, 'MNO1', '24680' UNION ALL
 
SELECT NULL, 'QWS1', '67981'

--Before Updating Uid
SELECT * FROM #Table1;


WITH CTE AS
(
	SELECT ROW_NUMBER() oVER (ORDER BY UName) + 100 AS Id, * FROM #Table1
)


UPDATE T
SET UId = Id
FROM #Table1 T
INNER JOIN CTE ON T.UName = CTE.UName AND T.Phone = CTE.Phone

--Afer Updating Uid
SELECT * FROM #Table1

DROP TABLE #Table1
 
Share this answer
 
Comments
Hari Krishna Prasad Inakoti 30-Nov-12 6:20am    
I'm using single table only
__TR__ 30-Nov-12 6:24am    
Ok. What is the problem?
Hari Krishna Prasad Inakoti 6-Dec-12 3:51am    
I got solution here.Thanq for reply
I got solution.


SQL
Declare @Seq Int

Select @Seq = 100

Update Stdrec Set ID= @Seq, @Seq = @Seq +1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900