Link to home
Start Free TrialLog in
Avatar of ukerandi
ukerandiFlag 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
Avatar of Vitor Montalvão
Vitor Montalvão
Flag of Switzerland image

No, NEWID will always give you a new ID. You can't generate same ID again.
If you want to get the unique value always and wanted to get back the value then use date time functions such as getdate()
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.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial