14

I want a group of users to have read-only access to all tables and views on all databases on SQL Server (I'm using SS2008). I'd like those users to have read-only access to all future tables and view.

How would you set that up?

Craig
  • 4,111
  • 9
  • 39
  • 49
  • Since this is a really old question that still gets a lot of views, be aware that in SQL 2014+, `GRANT CONNECT ANY DATABASE TO ;` along with `GRANT SELECT ALL USER SECURABLES TO ;` is going to be much easier than the old way of doing things. – Dave Mason Aug 13 '21 at 13:37

3 Answers3

10

add the user to the db_datareader role

example

exec sp_addrolemember 'db_datareader',YourLogin

Info about db_datareader: http://msdn.microsoft.com/en-us/library/ms188629(SQL.90).aspx

SQLMenace
  • 132,095
  • 25
  • 206
  • 225
2
DECLARE @dbname VARCHAR(50)
DECLARE @statement NVARCHAR(max)
DECLARE db_cursor CURSOR

LOCAL FAST_FORWARD
FOR SELECT name FROM MASTER.dbo.sysdatabases OPEN db_cursor
    FETCH NEXT FROM db_cursor INTO @dbname 
WHILE @@FETCH_STATUS = 0
BEGIN
    /* This sentence will be executed to gran the privileges. */
    SELECT @statement = 'use ['+@dbname+']; '+'EXEC sp_addrolemember N''db_datareader'', N''userPeter''';
    EXEC sp_executesql @statement
    FETCH NEXT FROM db_cursor INTO @dbname
END

In the location that appear userPeter you must write your username.

0

You should just be able to add the users to the db_datareader database role in each of the databases. You could write a script to loop through the databases and do it for you.

Tom H
  • 46,766
  • 14
  • 87
  • 128