Re: How can permissions of multiple tables be changed at once?

From: Dan Guzman (guzmanda_at_nospam-online.sbcglobal.net)
Date: 09/21/04


Date: Mon, 20 Sep 2004 22:41:15 -0500

Below is a sample script you can customize and run to apply mass
permissions.

SET NOCOUNT ON

DECLARE @GrantStatement nvarchar(4000)

DECLARE GrantStatements CURSOR
LOCAL FAST_FORWARD READ_ONLY FOR
SELECT
    N'GRANT SELECT ON ' +
    QUOTENAME(TABLE_SCHEMA) +
    N'.' +
    QUOTENAME(TABLE_NAME) +
    N' TO MyRole'
FROM INFORMATION_SCHEMA.TABLES
WHERE
    OBJECTPROPERTY(
        OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +
            N'.' +
            QUOTENAME(TABLE_NAME)),
        'IsMSShipped') = 0 AND
    TABLE_TYPE = 'BASE TABLE'
OPEN GrantStatements
WHILE 1 = 1
BEGIN
    FETCH NEXT FROM GrantStatements
        INTO @GrantStatement
    IF @@FETCH_STATUS = -1 BREAK
    BEGIN
       RAISERROR (@GrantStatement, 0, 1) WITH NOWAIT
       EXECUTE sp_ExecuteSQL @GrantStatement
    END
END
CLOSE GrantStatements
DEALLOCATE GrantStatements

-- 
Hope this helps.
Dan Guzman
SQL Server MVP
"bing" <bing@discussions.microsoft.com> wrote in message 
news:908EE4BF-CF98-46CD-A543-36634FA87DE7@microsoft.com...
>A user database has a lot tables.  How can I change all the tables to have
> the same permissions (e.g. SELECT, INSERT, etc.) at once rather than 
> manually
> do it by going through table by table?
>
> Thanks in advance for any help,
>
> Bing 


Relevant Pages

  • Re: How can permissions of multiple tables be changed at once?
    ... We do use roles to manage users and permissions. ... Bing ... "Dan Guzman" wrote: ... > DECLARE GrantStatements CURSOR ...
    (microsoft.public.sqlserver.server)
  • Re: OBJECT Level Permissions
    ... Below is a procedure that will grant EXECUTE permissions on all user procs ... can be modified for SQL 7 if needed. ... DECLARE GrantStatements CURSOR ...
    (microsoft.public.sqlserver.programming)
  • Re: How to set permissions for objects quickly
    ... SELECT permissions on all user tables and views can be assigned by adding ... DECLARE GrantStatements CURSOR ... OBJECTPROPERTY( ... >>> Can you explain what you mean by "change sql login username"? ...
    (microsoft.public.sqlserver.security)
  • Re: User permissions
    ... DECLARE @UserName sysname ... DECLARE GrantStatements CURSOR LOCAL FAST_FORWARD ... SQL Server MVP ...
    (microsoft.public.sqlserver.security)
  • Re: User permissions
    ... I will try your script and hopefully it will ... >DECLARE GrantStatements CURSOR LOCAL FAST_FORWARD ... >SQL Server MVP ...
    (microsoft.public.sqlserver.security)