Determining if a SQL Server table is read-only

我的梦境 提交于 2019-12-02 15:01:39

问题


What's the best way to determine if a given SQL Server table is read-only, either because of account permissions or the database being marked as read-only?


回答1:


--for database (updated for newer versions)
SELECT DATABASEPROPERTYEX(DB_NAME(), 'Updateability')

--for tables.
SELECT
   *
FROM
   sys.tables t
   LEFT JOIN
   sys.database_permissions dp2 ON dp2.major_id = t.object_id AND dp2.permission_name = 'SELECT'
WHERE
   NOT EXISTS (SELECT * FROM 
         sys.database_permissions dp
      WHERE
         dp.major_id = t.object_id
         AND
         dp.permission_name IN ('INSERT', 'DELETE', 'UPDATE'))

Modify * as needed to get what you want: this checks for none/SELECT where there is no I/U/D rights

You also have FILEGROUPPROPERTY etc if you have read only filegroups

Edit: for current connection and direct table rights per user. Does not iterate through group hierarchy

SELECT
   *
FROM
   sys.tables t
   JOIN
   sys.database_permissions dp2 ON dp2.major_id = t.object_id AND dp2.permission_name = 'SELECT'
WHERE
   dp2.grantee_principal_id = USER_ID()
   AND
   dp.permission_name IN ('INSERT', 'DELETE', 'UPDATE'))



回答2:


You mean besides trying to write to it right?




回答3:


Have a rummage around in SQL Server's Information Schema Views, specifically, TABLE_PRIVILEGES.



来源:https://stackoverflow.com/questions/2576643/determining-if-a-sql-server-table-is-read-only

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!