Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Links Database Forum

» Database Journal Home
» Database Articles
» Database Tutorials
MS Access
Database Tools
SQL Scripts & Samples
» Database Forum
» Slideshows
» Sitemap
Free Newsletters:
News Via RSS Feed

follow us on Twitter
Database Journal |DBA Support |SQLCourse |SQLCourse2

Featured Database Articles

SQL Scripts & Samples

Posted Sep 30, 2008

Grant SELECT permissions on all tables in all databases

By Staff

>>Script Language and Platform: SQL Server 2000
This code will loop through all user tables in all databases and grant select permissions on each table. It will add the user from the variables set at the beginning of the script if the user does not already have access to each database. This script can be modified for whatever object types or permissions you need. PRINT statements can be "un-commented" in case debug output is desired. Tested for SQL 2000, but not yet for SQL 2005 (filters for schemas may be required).

Take care to use security-based scripts wisely and always test first!

Author: Pat Johnson


        Script:         Permissions Loop
        Purpose:        Loop thru objects in multiple databases and grant
                                specific permissions on specific objects.
                                Filters can be added in WHERE clauses as needed.
        Author:         Pat Johnson (
        Date:           9/29/2008


        @LoginName VARCHAR(128),
        @UserName VARCHAR(128),
        @DatabaseName VARCHAR(100),
        @SQLScript VARCHAR(6000)

SET @LoginName = 'LoginName'  -- Can be Windows login such as 'DOMAINLoginName'
SET @UserName = 'UserName'            -- Many times the same as the LoginName

IF EXISTS (SELECT * FROM master..syslogins WHERE [name] = @LoginName)
        DECLARE DatabaseCursor CURSOR FOR
                SELECT [name] FROM master..sysdatabases
                ORDER BY [name]

        OPEN DatabaseCursor
        FETCH NEXT FROM DatabaseCursor INTO @DatabaseName

        WHILE @@FETCH_STATUS = 0

                        PRINT 'Updating Database:  ' + @DatabaseName + ''

                        SET @SQLScript = '
                                USE ' + @DatabaseName + '
                                IF NOT EXISTS (SELECT * FROM ' + @DatabaseName + '..sysusers WHERE [name] = ''' + @UserName + ''')
                                        EXEC SP_ADDUSER ''' + @LoginName + ''',''' + @UserName + ''''

--                      PRINT @SQLScript
                        EXEC (@SQLScript)

                        -- Modify script below to change object types or type of permissions: U = User table, etc.
                        SET @SQLScript = '
                        USE ' + @DatabaseName + ' 
                                @TableName VARCHAR(100),
                                @SQLScript VARCHAR(4000)

                        DECLARE TableCursor CURSOR FOR
                                SELECT [name] FROM ' + @DatabaseName + '..sysobjects WHERE type = ''U'' ORDER BY [name]
                        OPEN TableCursor
                        FETCH NEXT FROM TableCursor INTO @TableName
                        WHILE @@FETCH_STATUS = 0
                                        SET @SQLScript = ''GRANT SELECT ON '' + @TableName + '' TO ' + @UserName + '''
--                                      PRINT @SQLScript

                                        FETCH NEXT FROM TableCursor INTO @TableName
                        CLOSE TableCursor
                        DEALLOCATE TableCursor'

--                      PRINT @SQLScript
                        EXEC (@SQLScript)

                        FETCH NEXT FROM DatabaseCursor INTO @DatabaseName


        CLOSE DatabaseCursor
        DEALLOCATE DatabaseCursor
ELSE PRINT 'Login ' + @LoginName + ' does not exist.'

Disclaimer: We hope that the information on these script pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided "as -is", without any warranty, whether express or implied, of its accuracy, completeness, or fitness for a particular purpose... Disclaimer Continued

Back to Database Journal Home

SQL Scripts & Samples Archives

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Latest Forum Threads
SQL Scripts & Samples Forum
Topic By Replies Updated
sql help!! add 1 August 22nd, 11:58 AM
How To Combine These 2 Queries into 1 Query ? tarek_land 1 June 7th, 08:37 AM
solving query svibuk 1 February 3rd, 06:08 AM
converting from a character string to uniqueidentifier saturnius 4 January 4th, 05:56 AM

Thanks for your registration, follow us on our social networks to keep up-to-date