Hi,

Is there any way to do this? I tried using a 'select scope_identity()', but that returns null, apparently because my identity column is not a numeric of any sort.

An answer to this would help me a lot! I would love to not have to use an integer or similar...

Cheers!

Well, I am not sure whether you are mistaken about identity and uniqueidentifier.

See the difference:

-- scope_identity(), @@identity and ident_current() are dealing with identities as in:

create table t (id integer identity(1000, 1), m varchar(100))
insert into table t (m) values ('sequence number starting from 1000 incremented by 1');
select scope_identity('t') as "Last inserted identity number for table t is";

whereas

create table ui (id uniqueidentifier, m varchar(100))

greates in table ui a globally unique identifier (GUID) which cannot be managed with above mentioned functions/variable. There are some ways to access such generated GUID, as for example using transact sql:

declare @tt table (idtt uniqueidentifier);
insert into ui output inserted.id into @tt  values (newid(), 'This is a GUID!');
select idtt as "Last inserted GUID: " from @tt;
-- Instead of newid() which generates randomly distributed GUIDs you can also use newsequentialid() which generates sequential GUIDs.

Attention: This code not tested!

GUIDs (example: 70E2E8DE-500B-4630-B3CB-16A131D35CD1) are extremly rarely used for primary keys and foreign keys. GUID identifiers should never appear on human-being interfaces. For such purposes identities which usually have integer datatype are quite usual.

-- tesu

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.