I want to automatically generate unique id with per-defined code attach to it.
ex:
UID12345678
CUSID5000
I tried uniqueidentifier
data type but it generate a id which is not suitable for a user id.
Any one have suggestions?
I want to automatically generate unique id with per-defined code attach to it.
ex:
UID12345678
CUSID5000
I tried uniqueidentifier
data type but it generate a id which is not suitable for a user id.
Any one have suggestions?
6条答案
按热度按时间gxwragnw1#
The only viable solution in my opinion is to use
ID INT IDENTITY(1,1)
column to get SQL Server to handle the automatic increment of your numeric valueSo try this:
Now, every time you insert a row into
tblUsers
without specifying values forID
orUserID
:then SQL Server will automatically and safely increase your
ID
value, andUserID
will contain values likeUID00000001
,UID00000002
,...... and so on - automatically, safely, reliably, no duplicates.Update: the column
UserID
is computed - but it still OF COURSE has a data type, as a quick peek into the Object Explorer reveals:e37o9pze2#
marc_s's Answer Snap
ivqmmu1c3#
Reference: https://learn.microsoft.com/en-us/sql/t-sql/functions/newid-transact-sql?view=sql-server-2017
-- Creating a table using NEWID for uniqueidentifier data type.
tyg4sfes4#
If you want to add the id manually you can use,
PadLeft() or String.Format() method.
Then you can append this with UID.
mutmk8jj5#
The 'newid()' method unique id generate for per record.
g0czyy6m6#
Table Creating
Values inserting
Select Table
Output