使用SCOPE_IDENTITY

时间:2014-12-02 10:41:56

标签: sql-server-2008

我在Microsoft SQL Server Managment Studio查询窗口中有简单的代码。

insert into [clients] ("Addres","companyID") values (NULL,NULL);
SCOPE_IDENTITY();
select  * from clients;

当我运行它时我有错误

  

Msg 102,Level 15,State 1,Line 3
  'SCOPE_IDENTITY'附近的语法不正确。

有什么问题?没有行SCOPE_IDENTITY();一切都很顺利。

表客户:

CREATE TABLE [dbo].[clients](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Addres] [nvarchar](200) NULL,
    [companyID] [int] NULL,
PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

1 个答案:

答案 0 :(得分:1)

与select一起使用。

insert into [clients] ("Addres","companyID") values (NULL,NULL);
select SCOPE_IDENTITY();