Avatar of ukerandi
Flag for United Kingdom of Great Britain and Northern Ireland asked on

t-sql autogenerate code

I have used following code to create unique id, if we deleted details is it possible to
come same number
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008

Avatar of undefined
Last Comment
Scott Pletcher

8/22/2022 - Mon
Vitor Montalvão

No, NEWID will always give you a new ID. You can't generate same ID again.
Easwaran Paramasivam

If you want to get the unique value always and wanted to get back the value then use date time functions such as getdate()
Jim Horn

Vitor is correct in that the answer is 'No, you can't do that'.

If this is a hard business requirement to resurrect previously deleted NEWID() values, you may want to consider a 'soft delete' where there is a bit column named something like IsDeleted that contains a 1 for deleted rows and 0 for active rows.  That way the record still exists even when 'deleted', and to make the row active again all you have to do is change IsDeleted back to 0.

Either that or build some kind of backup/restore strategy where rows marked as deleted are moved to another table/database/whatever, and there is functionality to make those rows active again by a separate process to access them which moves the row back.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Scott Pletcher

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.