Script to find username and corresponding loginname for all user DB

  • Comments posted to this topic are about the item Script to find username and corresponding loginname for all user DB

    Regards
    Shashank Srivastava
    MCITP - SQL SERVER 2008
    INDIA
    Follow me @ http://shashanksrivastavasqldba.blogspot.com/

  • we can check the username in oracle by below command

    show user;

    https:www.spectrumplus.in

  • Nice use of MS_ForEachDB here. We don't use it much here because it's undocumented and can be a little picky with syntax. We use this type of construct to loop DBs:

    DECLARE @databases TABLE(dbName VARCHAR(100))

    DECLARE @CurrentDB VARCHAR(100), @sql NVARCHAR(max)

    --Exclude system DBs

    INSERT @databases SELECT name from sys.databases WHERE database_id > 4

    WHILE EXISTS (SELECT TOP 1 dbName FROM @databases)

    BEGIN

    SET@CurrentDB = (SELECT TOP 1 dbName FROM @databases)

    SET @sql = 'Use ' + @CurrentDB + '; <Take some action>'

    --PRINT @sql

    EXEC sp_executesql @sql

    DELETE @databases WHERE dbName = @CurrentDB

    END

    Ken

  • Very nice script, I was in fact looking for a way to pull this information together. I found a few scripts that gave me too much and some too little. Thanks for doing the heavy work for us. I'm finding this script very handy.

  • Shaz I made a small change to your script to work with my databases. I have a couple that have a space in the file name. No major change, just added the few brakets to the ? below.

    set @Command= 'if not exists (select * from #systemdbs where name = ''[?]'') begin '+char(13)+

    +'use [?] ; insert #dbusersbuffer exec sp_helpuser'+char(13)

    +'insert #dbusers select ''?'', * from #dbusersbuffer'+char(13)

    +'truncate table #dbusersbuffer'+char(13)

    +'end'

  • Nice script. Thanks for taking the time to share. I ran into one problem though. I get this error:

    Msg 213, Level 16, State 7, Procedure sp_helpuser, Line 250

    Insert Error: Column name or number of supplied values does not match table definition.

    The reason is that sp_helpuser will return 2 result sets if aliases exist in any database.

    Thanks again,

    Lee

  • Thanks.

    Regards
    Shashank Srivastava
    MCITP - SQL SERVER 2008
    INDIA
    Follow me @ http://shashanksrivastavasqldba.blogspot.com/

  • Happy to help you. Thanks

    Regards
    Shashank Srivastava
    MCITP - SQL SERVER 2008
    INDIA
    Follow me @ http://shashanksrivastavasqldba.blogspot.com/

  • Thank you for the modification . Yes we need to make those changes to include databases that have spaces.

    Regards
    Shashank Srivastava
    MCITP - SQL SERVER 2008
    INDIA
    Follow me @ http://shashanksrivastavasqldba.blogspot.com/

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

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