我在 SQL Server 2014 数据库中有一个用户,该用户只有插入表的权限(没有选择权限)。在表上启用更改跟踪时,插入将失败并显示The SELECT permission was denied on the object ...
为什么会这样?
重现步骤:
USE [master];
CREATE DATABASE insert_test;
GO
USE insert_test;
GO
CREATE TABLE dbo.test(id INT NOT NULL IDENTITY(1,1) PRIMARY KEY, value NVARCHAR(50));
ALTER DATABASE insert_test SET CHANGE_TRACKING=ON;
ALTER TABLE dbo.test ENABLE CHANGE_TRACKING;
GO
CREATE LOGIN test_user WITH PASSWORD='FD3nIk1p(4$LKH!eSY';
CREATE USER test_user FOR LOGIN test_user;
GRANT INSERT ON dbo.test TO test_user;
GO
EXECUTE AS USER='test_user';
INSERT INTO dbo.test(value) VALUES(N'hello'); -- Fails with: The SELECT permission was denied on the object 'test', database 'insert_test', schema 'dbo'
/*
-- Cleanup
USE [master];
GO
DROP DATABASE insert_test;
GO
DROP LOGIN test_user;
GO
*/
更改跟踪操作在用户的上下文中运行,并且为了跟踪正在更改的内容,它必须在更改之前使用 SELECT 读取行。