Last post Mar 02, 2018 05:21 AM by Brando ZWZ
Mar 01, 2018 05:44 AM|cool.asp|LINK
The AspNetUser table in Identity uses nvarchar(128) as Id. Let's say I wanted to use a similar Id in a table storing transactions. Does SQL automatically generate ids using nvarchar (similar to using an int)? Or do I have to generate a guid in C# or something?
What is the standard way of using nvarchar as an id in SQL? Thanks
Mar 01, 2018 06:06 AM|mshoaiblibra|LINK
If you are following any format for generating Identity number then you have code your self.
For Instance: If you want to generate specific identity such as 2018-1987-65431563 or FEWA-2018-13564646 Then you need to code some sample of code.
Otherwise you can you UNIQUEIDENTIFIER in SQL. Check following line of code.
CREATE TABLE #Table
IdentityColumn UNIQUEIDENTIFIER DEFAULT NEWID(),
INSERT INTO #Table(Name) VALUES ('John')
INSERT INTO #Table VALUES (NEWID(), 'Smith')
SELECT * FROM #Table
Mar 02, 2018 05:21 AM|Brando ZWZ|LINK
As far as I know, identity convert the guid to string in code as the name. Since multiple sql's datatype doesn't support guid.
If you want to directly generate this value in the sql, I suggest you could try to use below sql query:
It convert the UNIQUEIDENTIFIER type to nvarchar.
DECLARE @User_ID UNIQUEIDENTIFIER = NEWID()
SELECT CAST(@User_ID AS NVARCHAR(128))