Created
March 11, 2025 13:26
-
-
Save taniomi/3723025ed1068b9348a59c07b66fb663 to your computer and use it in GitHub Desktop.
Grants select on all schemas except for specified ones
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DECLARE @sql NVARCHAR(MAX) = ''; | |
| SELECT @sql += | |
| 'GRANT SELECT ON SCHEMA::' + QUOTENAME(name) + ' TO [role];' + CHAR(13) | |
| FROM sys.schemas | |
| WHERE name NOT IN ('dbo', 'dev') | |
| AND name NOT LIKE 'db_%'; -- Exclude schemas starting with "db_" | |
| PRINT @sql; -- Check generated SQL before executing | |
| EXEC sp_executesql @sql; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment