SQL Server 2016中与nolock和readpast的区别

时间:2017-08-31 10:23:25

标签: sql sql-server sql-server-2016

我正在一个查询窗口中为事务中的单个记录更新表Hr.employees

Begin Tran A

update hr.Employees
set lastname = 'davis'
where empid=1

在其他查询窗口中,我正在运行两个没有事务的select语句

select count(*) from hr.Employees with(nolock)
select count(*) from hr.Employees with(readpast)

它们都返回9,而readpast应返回8,因为它在返回数据时删除脏读。

CREATE TABLE [HR].[Employees](
    [empid] [int] IDENTITY(1,1) NOT NULL,
    [lastname] [nvarchar](20) NOT NULL,
    [firstname] [nvarchar](10) NOT NULL,
    [title] [nvarchar](30) NOT NULL,
    [titleofcourtesy] [nvarchar](25) NOT NULL,
    [birthdate] [datetime] NOT NULL,
    [hiredate] [datetime] NOT NULL,
    [address] [nvarchar](60) NOT NULL,
    [city] [nvarchar](15) NOT NULL,
    [region] [nvarchar](15) NULL,
    [postalcode] [nvarchar](10) NULL,
    [country] [nvarchar](15) NOT NULL,
    [phone] [nvarchar](24) NOT NULL,
    [mgrid] [int] NULL,
 CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED 
(
    [empid] 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 [HR].[Employees]  WITH CHECK ADD  CONSTRAINT [FK_Employees_Employees] FOREIGN KEY([mgrid])
REFERENCES [HR].[Employees] ([empid])
GO

ALTER TABLE [HR].[Employees] CHECK CONSTRAINT [FK_Employees_Employees]
GO

ALTER TABLE [HR].[Employees]  WITH CHECK ADD  CONSTRAINT [CHK_birthdate] CHECK  (([birthdate]<=getdate()))
GO

ALTER TABLE [HR].[Employees] CHECK CONSTRAINT [CHK_birthdate]
GO

CREATE NONCLUSTERED INDEX [idx_nc_lastname] ON [HR].[Employees]
(
    [lastname] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [idx_nc_postalcode] ON [HR].[Employees]
(
    [postalcode] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

0 个答案:

没有答案