Determining if a SQL Server table is read-only
What's the best way to de开发者_如何学运维termine if a given SQL Server table is read-only, either because of account permissions or the database being marked as read-only?
--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'))
You mean besides trying to write to it right?
Have a rummage around in SQL Server's Information Schema Views, specifically, TABLE_PRIVILEGES.
精彩评论