TSQL

  • how to run the below qury in all databases

    CREATE TABLE #TempPermissions

    (

    uid smallint,

    UserName varchar (100),

    Group_Name varchar (100),

    NT_Group varchar (3),

    NT_User varchar (3),

    SQL_USer varchar(3)

    )

    INSERT #TempPermissions

    SELECT su.uid, sl.name, su2.name 'Group Name',

    CASE WHEN

    su.isntgroup=1 THEN 'Yes' ELSE 'No' END 'NT Group',

    CASE WHEN

    su.isntuser=1 THEN 'Yes' ELSE 'No' END 'NT User',

    CASE WHEN

    su.issqluser=1 THEN 'Yes' ELSE 'No' END 'SQL User'

    FROM sysusers su

    INNER join sysmembers sm

    ON su.uid = sm.memberuid

    INNER join sysusers su2

    ON sm.groupuid = su2.uid

    INNER join master.dbo.syslogins sl

    ON su.sid = sl.sid

    WHERE su.NAME <> 'dbo'

    SELECT * FROM #TempPermissions

    SELECT DISTINCT 'EXEC sp_grantdbaccess ''' + UserName + '''' FROM #TempPermissions -- WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    SELECT 'EXEC sp_addrolemember ''' + Group_Name + ''', ''' + UserName + ''''

    FROM #TempPermissions

    --WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    DROP TABLE #TempPermissions

  • Create the temp table, use sp_MSForEachDB to insert into it from each database, then select from it after that.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • i am weak in tsql...can u please tell me how to do that

  • CREATE TABLE #TempPermissions

    (uid SMALLINT,

    UserName VARCHAR(100),

    Group_Name VARCHAR(100),

    NT_Group VARCHAR(3),

    NT_User VARCHAR(3),

    SQL_USer VARCHAR(3)) ;

    EXEC sp_MSForEachDB '

    INSERT #TempPermissions

    SELECT su.uid, sl.name, su2.name,

    CASE WHEN

    su.isntgroup=1 THEN ''Yes'' ELSE ''No'' END ''NT Group'',

    CASE WHEN

    su.isntuser=1 THEN ''Yes'' ELSE ''No'' END ''NT User'',

    CASE WHEN

    su.issqluser=1 THEN ''Yes'' ELSE ''No'' END ''SQL User''

    FROM sysusers su

    INNER join sysmembers sm

    ON su.uid = sm.memberuid

    INNER join sysusers su2

    ON sm.groupuid = su2.uid

    INNER join master.dbo.syslogins sl

    ON su.sid = sl.sid

    WHERE su.NAME <> ''dbo'';'

    SELECT *

    FROM #TempPermissions

    SELECT DISTINCT

    'EXEC sp_grantdbaccess ''' + UserName + ''''

    FROM #TempPermissions

    -- WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    SELECT 'EXEC sp_addrolemember ''' + Group_Name + ''', ''' + UserName + ''''

    FROM #TempPermissions

    --WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    DROP TABLE #TempPermissions

    All I did was add in the "for each database" portion of this. I haven't tested or checked the latter part of it.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • its not giving any result

  • Every iteration done inside the "for each" proc is done on an independent thread so you need to use a global temp table or a permanent table. Try it like this:

    IF OBJECT_ID(N'tempdb..##TempPermissions') > 0

    DROP TABLE ##TempPermissions ;

    GO

    CREATE TABLE ##TempPermissions

    (

    uid SMALLINT,

    UserName VARCHAR(100),

    Group_Name VARCHAR(100),

    NT_Group VARCHAR(3),

    NT_User VARCHAR(3),

    SQL_USer VARCHAR(3)

    ) ;

    EXEC sys.sp_MSforeachdb

    @command1 = N'

    INSERT ##TempPermissions

    SELECT su.uid, sl.name, su2.name,

    CASE WHEN

    su.isntgroup=1 THEN ''Yes'' ELSE ''No'' END ''NT Group'',

    CASE WHEN

    su.isntuser=1 THEN ''Yes'' ELSE ''No'' END ''NT User'',

    CASE WHEN

    su.issqluser=1 THEN ''Yes'' ELSE ''No'' END ''SQL User''

    FROM ?..sysusers su

    INNER join ?..sysmembers sm

    ON su.uid = sm.memberuid

    INNER join ?..sysusers su2

    ON sm.groupuid = su2.uid

    INNER join master.dbo.syslogins sl

    ON su.sid = sl.sid

    WHERE su.NAME <> ''dbo'';' ;

    SELECT *

    FROM ##TempPermissions ;

    SELECT DISTINCT

    'EXEC sp_grantdbaccess ''' + UserName + ''''

    FROM ##TempPermissions ;

    -- WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    SELECT 'EXEC sp_addrolemember ''' + Group_Name + ''', ''' + UserName + ''''

    FROM ##TempPermissions ;

    --WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    --DROP TABLE ##TempPermissions

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • opc.three (6/7/2011)


    Every iteration done inside the "for each" proc is done on an independent thread so you need to use a global temp table or a permanent table. Try it like this:

    IF OBJECT_ID(N'tempdb..##TempPermissions') > 0

    DROP TABLE ##TempPermissions ;

    GO

    CREATE TABLE ##TempPermissions

    (

    uid SMALLINT,

    UserName VARCHAR(100),

    Group_Name VARCHAR(100),

    NT_Group VARCHAR(3),

    NT_User VARCHAR(3),

    SQL_USer VARCHAR(3)

    ) ;

    EXEC sys.sp_MSforeachdb

    @command1 = N'

    INSERT ##TempPermissions

    SELECT su.uid, sl.name, su2.name,

    CASE WHEN

    su.isntgroup=1 THEN ''Yes'' ELSE ''No'' END ''NT Group'',

    CASE WHEN

    su.isntuser=1 THEN ''Yes'' ELSE ''No'' END ''NT User'',

    CASE WHEN

    su.issqluser=1 THEN ''Yes'' ELSE ''No'' END ''SQL User''

    FROM ?..sysusers su

    INNER join ?..sysmembers sm

    ON su.uid = sm.memberuid

    INNER join ?..sysusers su2

    ON sm.groupuid = su2.uid

    INNER join master.dbo.syslogins sl

    ON su.sid = sl.sid

    WHERE su.NAME <> ''dbo'';' ;

    SELECT *

    FROM ##TempPermissions ;

    SELECT DISTINCT

    'EXEC sp_grantdbaccess ''' + UserName + ''''

    FROM ##TempPermissions ;

    -- WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    SELECT 'EXEC sp_addrolemember ''' + Group_Name + ''', ''' + UserName + ''''

    FROM ##TempPermissions ;

    --WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    --DROP TABLE ##TempPermissions

    No, you don't. It's on the same connection, so it's got access to a normal temp table. I tested that part, I just didn't run the resulting script.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • My fault. I knew the for each proc made use of EXEC and I did not know it would have access to temp tables declared outside the EXEC-scope. The real issue was the missing ?'s in your previous post.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

  • opc.three (6/7/2011)


    The real issue was the missing ?'s in your previous post.

    Now that I can easily believe! Won't be the first, nor the last, time that I missed those. Too used to using my own cursors for this, instead of Microsoft's.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • any help to get the above query to run on all databases

  • GSquared (6/7/2011)


    opc.three (6/7/2011)


    Every iteration done inside the "for each" proc is done on an independent thread so you need to use a global temp table or a permanent table. Try it like this:

    IF OBJECT_ID(N'tempdb..##TempPermissions') > 0

    DROP TABLE ##TempPermissions ;

    GO

    CREATE TABLE ##TempPermissions

    (

    uid SMALLINT,

    UserName VARCHAR(100),

    Group_Name VARCHAR(100),

    NT_Group VARCHAR(3),

    NT_User VARCHAR(3),

    SQL_USer VARCHAR(3)

    ) ;

    EXEC sys.sp_MSforeachdb

    @command1 = N'

    INSERT ##TempPermissions

    SELECT su.uid, sl.name, su2.name,

    CASE WHEN

    su.isntgroup=1 THEN ''Yes'' ELSE ''No'' END ''NT Group'',

    CASE WHEN

    su.isntuser=1 THEN ''Yes'' ELSE ''No'' END ''NT User'',

    CASE WHEN

    su.issqluser=1 THEN ''Yes'' ELSE ''No'' END ''SQL User''

    FROM ?..sysusers su

    INNER join ?..sysmembers sm

    ON su.uid = sm.memberuid

    INNER join ?..sysusers su2

    ON sm.groupuid = su2.uid

    INNER join master.dbo.syslogins sl

    ON su.sid = sl.sid

    WHERE su.NAME <> ''dbo'';' ;

    SELECT *

    FROM ##TempPermissions ;

    SELECT DISTINCT

    'EXEC sp_grantdbaccess ''' + UserName + ''''

    FROM ##TempPermissions ;

    -- WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    SELECT 'EXEC sp_addrolemember ''' + Group_Name + ''', ''' + UserName + ''''

    FROM ##TempPermissions ;

    --WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    --DROP TABLE ##TempPermissions

    No, you don't. It's on the same connection, so it's got access to a normal temp table. I tested that part, I just didn't run the resulting script.

    above query is working ...but i just need to append a USE DBNAME for the particular access...pls help

  • Add "USE [?];" at the begginning of the string. That's what sp_MSForEachDB uses to plug in the database name.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • You do not need the USE because I fully-qualifed all the table names. The problem is with the SELECTs at the bottom. Try this version, I added a column to the temp table to store the DB name so we can output fully-qualified calls to the system procs that grant access add grant role members.

    IF OBJECT_ID(N'tempdb..##TempPermissions') > 0

    DROP TABLE ##TempPermissions ;

    GO

    CREATE TABLE ##TempPermissions

    (

    uid SMALLINT,

    db sysname,

    UserName VARCHAR(100),

    Group_Name VARCHAR(100),

    NT_Group VARCHAR(3),

    NT_User VARCHAR(3),

    SQL_USer VARCHAR(3)

    ) ;

    EXEC sys.sp_MSforeachdb

    @command1 = N'

    INSERT ##TempPermissions

    SELECT su.uid, ''?'', sl.name, su2.name,

    CASE WHEN

    su.isntgroup=1 THEN ''Yes'' ELSE ''No'' END ''NT Group'',

    CASE WHEN

    su.isntuser=1 THEN ''Yes'' ELSE ''No'' END ''NT User'',

    CASE WHEN

    su.issqluser=1 THEN ''Yes'' ELSE ''No'' END ''SQL User''

    FROM ?..sysusers su

    INNER join ?..sysmembers sm

    ON su.uid = sm.memberuid

    INNER join ?..sysusers su2

    ON sm.groupuid = su2.uid

    INNER join master.dbo.syslogins sl

    ON su.sid = sl.sid

    WHERE su.NAME <> ''dbo'';' ;

    SELECT *

    FROM ##TempPermissions ;

    SELECT DISTINCT

    'EXEC ' + db + '.sys.sp_grantdbaccess ''' + UserName + ''''

    FROM ##TempPermissions ;

    -- WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    SELECT 'EXEC ' + db + '.sys.sp_addrolemember ''' + Group_Name + ''', ''' + UserName + ''''

    FROM ##TempPermissions ;

    --WHERE NT_USER = 'Yes' or NT_Group = 'Yes'

    --DROP TABLE ##TempPermissions

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato

Viewing 13 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply