browse DBs

  • I am trying to create a sp that would jump from one DB to another using the USE command. I built a string and then I try to run EXECUTE(@SQLString) with no lock .  This is the sample code I am using:

    declare @DBName varchar(200)

    declare @SQlString varchar(300)

    set @DBName='model'

    set @SQlString = 'use '+@DBName

    execute(@SQlString)

    Is there any way I can accomplish that?

    Thanks

    Carlos

  • check this out :

    exec sp_msforeachdb 'Select ''?'' as dbName, * from ?.dbo.SysObjets'

  • I tried that but didn't serve my purpose. What I am actually trying to accomplish is to run a sp that is in the master Db so that I can assign database roles to all the dadabases at once. This is the sp :

    execute sp_addrolemember 'db_owner','adcnt\SQLAdmins'

    execute sp_addrolemember 'db_owner','adcnt\SQLFull'

    execute sp_addrolemember 'db_owner','adcnt\SQLRW'

    execute sp_addrolemember 'db_datareader','adcnt\SQLR'

    GO

    using sp_MSforeachdb only changes the first DB.

    Any suggestions?

     

     

  • What statement did you try to run exactly?

  • I run a sp that is in the Master DB with those 4 statements:

    execute sp_addrolemember 'db_owner','adcnt\SQLAdmins'

    execute sp_addrolemember 'db_owner','adcnt\SQLFull'

    execute sp_addrolemember 'db_owner','adcnt\SQLRW'

    execute sp_addrolemember 'db_datareader','adcnt\SQLR'

    GO

    and I run this from the query analizer pointing to the master db

    EXEC sp_MSforeachdb @command1="sp_Create_DBUsers"

    as I said before it only created the roles for the first DB

     

  • execute that string in the foreach sp :

    'use ?

    execute sp_addrolemember ''db_owner'',''adcnt\SQLAdmins''

    execute sp_addrolemember ''db_owner'',''adcnt\SQLFull''

    execute sp_addrolemember ''db_owner'',''adcnt\SQLRW''

    execute sp_addrolemember ''db_datareader'',''adcnt\SQLR'''

  • THANKS SO MUCH !!! Worked like a charm.

  • HTH.

Viewing 8 posts - 1 through 7 (of 7 total)

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