how to get inserted sequential uniqueidentifier

2019-07-27 08:17发布

my table looks like this:

create table foos(
id uniqueidentifier primary KEY DEFAULT (newsequentialid()),
..
)

so the id is sequentially generated automatically, I'm not setting it

how do I get it's value after the insert ? (with identity I was doing insert ... select @@identity)

2条回答
Melony?
2楼-- · 2019-07-27 09:05

Returning the NewSequentialID() after Insert using the Output Clause

The basic idea:

create table foos(id uniqueidentifier primary KEY DEFAULT (newsequentialid()))

declare @Ids table(id uniqueidentifier)

insert foos
output inserted.id into @Ids
default values

select *
from @Ids
查看更多
爷、活的狠高调
3楼-- · 2019-07-27 09:09

You would use the other data that got inserted with the id to select it out again. So where you have the .. you would have:

SELECT id
WHERE ..

which would return the id

查看更多
登录 后发表回答