开发者

Getting last updated on a table as a dbowner

开发者 https://www.devze.com 2023-03-03 18:52 出处:网络
I need to get a last updated timestamp when for a table. This operation is to be performed by a dbowner, who has GRANT EXEC permissions on all sprocs. Any ideas how to accomplish this? Here is what I

I need to get a last updated timestamp when for a table. This operation is to be performed by a dbowner, who has GRANT EXEC permissions on all sprocs. Any ideas how to accomplish this? Here is what I tried.

I tried getting this info using SMO, but because of the lack of permissions it returns a null table object.


using (SqlConnection connection = new SqlConnection(connectionString))
{
   ServerConnection serverConnection = new ServerConnection(connection);
   Server server = new Server(serverConnection);
   Database database = server.Databases["MyDb"];
   Table table = database.Tables["MyTable"];    // here table is null for dbowner user, but a valid object for a super user
   DateTime lastModified = table.DateLastModified;
}

Also tried creating a new sproc, but when I call it I get The user does not have permission to perform this action.


CREATE PROCEDURE getTableLastModifiedDate
    (
        @TableName nvarchar(max)
    )
开发者_如何学CAS
BEGIN
    SELECT last_user_update FROM sys.dm_db_index_usage_stats 
    WHERE OBJECT_NAME(OBJECT_ID)=@TableName AND database_id = DB_ID(DB_NAME())
END
GO


From the help topic for sys.dm_db_index_usage_stats:

Requires VIEW SERVER STATE permission.

If you look at the server permissions: VIEW SERVER STATE is implied by ALTER SERVER STATE in turn implied by CONTROL SERVER. System administrators have the required permissions, database administrators do not.

The best solution is to sign the procedure and then grant the required permission through the signature, see Signing an activated procedure for an example.

0

精彩评论

暂无评论...
验证码 换一张
取 消