我正在寻找如何从MS Sql-Server 2012中的视图跟踪更改。而且,登录用户的角色是Public。因此,很难做到这一点。
例如,假设存在模式。
CREATE TABLE [dbo].[USER_CREDENTIAL](
[USERID] [nvarchar](48) NOT NULL,
[VALID_FROM] DATETIME NULL,
[EXPIRED_AT] DATETIME NULL,
[CREDENTIAL_ID] int NOT NULL,
CONSTRAINT [UNIQUE_USERID] PRIMARY KEY CLUSTERED( [USERID] ASC)
) ;
CREATE VIEW [VIEW_OF_USER_CREDENTIAL] as
SELECT * FROM dbo.[USER_CREDENTIAL];
只能被允许访问该视图。当某些数据被插入/更新/删除到USER_CREDENTIAL时,视图将被更改。我将查询视图。
我看到了the document。我尝试过,但是要跟踪的目标是数据表,而登录用户缺少该角色。我收到错误消息。
Object 'foo' is of a data type that is not supported by the CHANGETABLE function. The object must be a user-defined table.
我尝试了以下方法。我添加了临时表和触发器,这些触发器使更改后的数据在视图更改时被插入到临时表中。但是,它也失败了,因为它被拒绝了权限。
CREATE TABLE dbo.[CHANGES_FROM_A_VIEW] (
[VERSION] [int] IDENTITY(1,1) NOT NULL,
[USERID] [nvarchar](48) NOT NULL,
CONSTRAINT [UNIQUE_VERSION] PRIMARY KEY CLUSTERED ( [VERSION] ASC)
)
CREATE TRIGGER [SOMETHING_CHANGED] ON dbo.[VIEW_OF_USER_CREDENTIAL] ...
ALTER DATABASE database_name
SET CHANGE_TRACKING = ON (CHANGE_RETENTION = 2 DAYS,AUTO_CLEANUP = ON)
ALTER TABLE [CHANGES_FROM_A_VIEW]
ENABLE CHANGE_TRACKING WITH (TRACK_COLUMNS_UPDATED = ON)
SELECT * FROM CHANGETABLE(CHANGES dbo.CHANGES_FROM_A_VIEW, 0) AS C
有人知道解决这个问题的方法吗?