简体   繁体   English

GUID的SCOPE_IDENTITY()?

[英]SCOPE_IDENTITY() for GUIDs?

Can anyone tell me if there is an equivalent of SCOPE_IDENTITY() when using GUIDs as a primary key in SQL Server? 在SQL Server中使用GUID作为主键时,有人能告诉我是否有等效的SCOPE_IDENTITY()

I don't want to create the GUID first and save as a variable as we're using sequential GUIDs as our primary keys. 我不想先创建GUID并保存为变量,因为我们使用顺序GUID作为主键。

Any idea on what the best way to retrieve the last inserted GUID primary key? 有什么最好的方法来检索最后插入的GUID主键吗?

You can get the GUID back by using OUTPUT. 您可以使用OUTPUT获取GUID。 This works when you're inserting multiple records also. 当您同时插入多个记录时,这也适用。

CREATE TABLE dbo.GuidPk (
    ColGuid uniqueidentifier NOT NULL DEFAULT NewSequentialID(),
    Col2    int              NOT NULL
)
GO

DECLARE @op TABLE (
    ColGuid uniqueidentifier
)

INSERT INTO dbo.GuidPk (
    Col2
)
OUTPUT inserted.ColGuid
INTO @op
VALUES (1)

SELECT * FROM @op

SELECT * FROM dbo.GuidPk

Reference: Exploring SQL 2005's OUTPUT Clause 参考: 探索SQL 2005的OUTPUT子句

There is no SCOPE_IDENTITY() equivalent when using GUIDs as primary keys, but you can use the OUTPUT clause to achieve a similar result. 使用GUID作为主键时,没有SCOPE_IDENTITY()等效,但您可以使用OUTPUT子句来实现类似的结果。 You don't need to use a table variable for output. 您不需要使用表变量进行输出。

CREATE TABLE dbo.GuidTest (
    GuidColumn uniqueidentifier NOT NULL DEFAULT NewSequentialID(),
    IntColumn int NOT NULL
)

GO

INSERT INTO GuidTest(IntColumn)
OUTPUT inserted.GuidColumn
VALUES(1)

The example above is useful if you want to read the value from a .Net client. 如果要从.Net客户端读取值,上面的示例很有用。 To read the value from .Net you would just use the ExecuteScalar method. 要从.Net读取值,您只需使用ExecuteScalar方法。

...
string sql = "INSERT INTO GuidTest(IntColumn) OUTPUT inserted.GuidColumn VALUES(1)";
SqlCommand cmd = new SqlCommand(sql, conn);
Guid guid = (Guid)cmd.ExecuteScalar();
...

you want to use NEWID() 你想使用NEWID()

    declare @id uniqueidentifier
    set @id  = NEWID()
    INSERT INTO [dbo].[tbl1]
           ([id])
     VALUES
           (@id)

    select @id

but clustered index problem are there in GUID . 但GUID中存在聚集索引问题。 read this one too NEWSEQUENTIALID() .These are my ideas ,think before use GUID as primary Key . 另读这个NEWSEQUENTIALID() 。这些是我的想法,在使用GUID作为主键之前要考虑。 :) :)

CREATE TABLE TestTable(KEY uniqueidentifier, ID VARCHAR(100), Name VARCHAR(100), Value tinyint);
Declare @id uniqueidentifier ;  
DECLARE @TmpTable TABLE (KEY uniqueidentifier);     
INSERT INTO [dbo].[TestTable]
    ([ID], [Name], Value])           
    OUTPUT INSERTED.KEY INTO @TmpTable           
    VALUES(@ID, @Name, @Value);           
SELECT @uniqueidentifier = KEY FROM @TmpTable; 
DROP TABLE TestTable;

Using this thread as a resource, I created the following for use within a trigger: 使用此线程作为资源,我创建了以下内容以在触发器中使用:

DECLARE @nextId uniqueIdentifier;
DECLARE @tempTable TABLE(theKey uniqueIdentifier NOT NULL DEFAULT NewSequentialID(), b int);
INSERT INTO @tempTable (b) Values(@b);
SELECT @nextId = theKey from @tempTable;

Might help someone else doing the same thing. 可能会帮助别人做同样的事情。 Curious if anyone has anything bad to say performance wise if this is not a good idea or not. 如果这不是一个好主意,如果有人有任何不好的表现,那就很好奇。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM