检索使用Uniqueidentifier插入的最后一行,它不是IDENTITY

时间:2010-08-11 11:09:19

标签: sql sql-server database tsql sql-server-2008

我对查询感到困惑。

我需要找到一个表中添加的LAST行,其中包含一个数据类型为Uniqueidentifier的列 专栏是: aspnet_Applications.ApplicationId 注意:此列Uniqueidentifier不是和IDENTITY列。

我还需要插入最后一行并在另一个表上更新它 aspnet_Users.ApplicationId

我尝试在MS SQL 2008中使用SCOPE_IDENTITY但不起作用,因为SCOPE_IDENTITY仅适用于IDENTITY列。

这是我的代码。有什么想法吗?

    CREATE DATABASE Test;
GO
USE Test;
GO

-- Create entities
CREATE TABLE [dbo].[aspnet_Applications](
    [ApplicationName] [nvarchar](256) NOT NULL,
    [LoweredApplicationName] [nvarchar](256) NOT NULL,
    [ApplicationId] [uniqueidentifier] NOT NULL,
    [Description] [nvarchar](256) NULL,
PRIMARY KEY NONCLUSTERED 
(
    [ApplicationId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
    [LoweredApplicationName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED 
(
    [ApplicationName] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[aspnet_Applications] ADD  DEFAULT (newid()) FOR [ApplicationId]
GO


CREATE TABLE [dbo].[aspnet_Users](
    [ApplicationId] [uniqueidentifier] NOT NULL,
    [UserId] [uniqueidentifier] NOT NULL,
    [UserName] [nvarchar](256) NOT NULL,
    [LoweredUserName] [nvarchar](256) NOT NULL,
    [MobileAlias] [nvarchar](16) NULL,
    [IsAnonymous] [bit] NOT NULL,
    [LastActivityDate] [datetime] NOT NULL,
PRIMARY KEY NONCLUSTERED 
(
    [UserId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[aspnet_Users]  WITH CHECK ADD FOREIGN KEY([ApplicationId])
REFERENCES [dbo].[aspnet_Applications] ([ApplicationId])
GO

ALTER TABLE [dbo].[aspnet_Users] ADD  DEFAULT (newid()) FOR [UserId]
GO

ALTER TABLE [dbo].[aspnet_Users] ADD  DEFAULT (NULL) FOR [MobileAlias]
GO

ALTER TABLE [dbo].[aspnet_Users] ADD  DEFAULT ((0)) FOR [IsAnonymous]
GO

-- Add data
DECLARE @MyIdentity binary(16);
INSERT INTO dbo.aspnet_Applications
(
    ApplicationName,
    LoweredApplicationName,
    Description
)
VALUES
(
    'x',
    'x',
    'Dummy text'
);
SET @MyIdentity = SCOPE_IDENTITY(); -- DOES NOT WORK
PRINT @MyIdentity; -- DOES NOT WORK

INSERT INTO dbo.aspnet_Users
(
    ApplicationId,
    UserName,
    LoweredUserName,
    MobileAlias,
    IsAnonymous,
    LastActivityDate
)
VALUES
(   
    @MyIdentity,
    'Administrator',
    'administrator',
    '',
    0,
    sysutcdatetime()
);

2 个答案:

答案 0 :(得分:14)

这是一个更多的工作,但对于您的插入,即使您已经在ApplicationID上有一个DEFAULT值,您可以这样做:

DECLARE @MyIdentity uniqueidentifier;
SET @MyIdentity = NewID();
INSERT INTO dbo.aspnet_Applications
(
    ApplicationName,
    LoweredApplicationName,
    ApplicationId, 
    Description
)
VALUES
(
    'x',
    'x',
    @MyIdentity,
    'Dummy text'
);

SELECT @MyIdentity

基本上,您事先设置了GUID,因此您已经知道要插入的内容。

答案 1 :(得分:4)

可以使用OUTPUT子句来获取插入的值,但LBT's answer可能更直接,更有效。

DECLARE @ids table(id uniqueidentifier) 

INSERT INTO dbo.aspnet_Applications
(
    ApplicationName,
    LoweredApplicationName,
    Description
)
OUTPUT INSERTED.ApplicationId  into @ids
VALUES
(
    'x',
    'x',
    'Dummy text'
)

顺便说一句,如果不是外键,你可以使用composable DML进行此操作。

INSERT
INTO   dbo.aspnet_Users
       (
              ApplicationId  ,
              UserName       ,
              LoweredUserName,
              MobileAlias    ,
              IsAnonymous    ,
              LastActivityDate
       )
SELECT ApplicationId  ,
       'Administrator',
       'administrator',
       ''             ,
       0              ,
       sysutcdatetime()
FROM   ( INSERT
       INTO    dbo.aspnet_Applications
               (
                       ApplicationName       ,
                       LoweredApplicationName,
                       Description
               )
               OUTPUT INSERTED.ApplicationId VALUES
               (
                       'x',
                       'x',
                       'Dummy text'
               )
       ) AS I