join the MSSQLTips community

MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

 
Run The Same SQL Command Against All Databases - MSSQLTips

MSSQLTips

MSSQLTips.com - your daily source for SQL Server tips
Welcome to MSSQLTips Sign in | Join | Help
in Search

Run The Same SQL Command Against All Databases

Last post 01-29-2010 12:02 PM by aprato. 8 replies.
Page 1 of 1 (9 items)
Sort Posts: Previous Next
  • 01-17-2008 12:30 AM

    Run The Same SQL Command Against All Databases

    This post is related to this tip: Run The Same SQL Command Against All Databases

    http://www.mssqltips.com/tip.asp?tip=1414

  • 01-17-2008 5:23 PM In reply to

    Re: Run The Same SQL Command Against All Databases

    Thanks for your wonderful scripts!

    You save my time and money!

    Have a great day!

     

  • 04-03-2008 11:53 AM In reply to

    Re: Run The Same SQL Command Against All Databases

    nice script ! thanks a bunch!

  • 04-04-2008 7:07 AM In reply to

    Re: Run The Same SQL Command Against All Databases

     Thanks so much. 

    Tim Ford did a great job with this tip.

    Thank you,
    The MSSQLTips.com Team

  • 12-10-2008 3:23 PM In reply to

    • robvon
    • Not Ranked
    • Joined on 12-10-2008
    • Posts 1

    Re: Run The Same SQL Command Against All Databases

    You may need some "[" and "]" around the '?' to deal with dodgy database names.......

     as in:

    EXEC sp_MSforeachdb 'USE [?] SELECT ''?'', SF.filename, SF.size FROM sys.sysfiles SF'

    R

  • 12-14-2008 9:04 PM In reply to

    • timmer26
    • Top 25 Contributor
    • Joined on 12-14-2007
    • Kalamazoo, MI, USA
    • Posts 49

    Re: Run The Same SQL Command Against All Databases

    You are absolutely correct.  This is particularly important when dealing with spaces in database names.

  • 11-20-2009 3:49 AM In reply to

    Re: Run The Same SQL Command Against All Databases

    --// Just another flavor to offer IF OBJECT_ID('tempdb..##TempDBInfo') IS NOT NULL BEGIN PRINT 'Previous ##TmpDBInfo found and Dropped.' DROP TABLE ##TempDBInfo END CREATE TABLE ##TempDBInfo(DBName nVarChar(255), FileName nvarchar(255), fileSize INT) EXEC sp_MSforeachdb 'USE [?] INSERT INTO ##TempDBInfo SELECT ''?'', SF.filename, SF.size FROM sys.sysfiles SF' SELECT * FROM ##TempDBInfo --// DROP TABLE ##TempDBInfo
    Filed under:
  • 01-27-2010 11:19 AM In reply to

    • Akbar
    • Not Ranked
    • Joined on 01-27-2010
    • Posts 1

    Re: Run The Same SQL Command Against All Databases

    Hi All, Is there are way to grant Execute permission to a particular Stored procedure or Function in all database of SQL Server 2008 Instance? The below one use to Grant Execute permission to any procedure.

    EXEC sp_msforeachdb 'USE ?; GRANT EXECUTE TO UserOrRoleName'

    But when trying the below one I'm getting error

    EXEC sp_msforeachdb 'USE ?; GRANT EXECUTE ON [?].[dbo].[sp_StoredProc1] TO UserOrRoleName'

    Error Message: Cannot find the object 'sp_StoredProc1', because it does not exist or you do not have permission.

    I have the 'sp_StroedProc1' exist in all my database in the sql server 2008 instance. Any help will be highly appreciated.
  • 01-29-2010 12:02 PM In reply to

    • aprato
    • Top 10 Contributor
    • Joined on 12-01-2007
    • Greater Boston
    • Posts 539

    Re: Run The Same SQL Command Against All Databases

     
    EXEC sp_msforeachdb 'USE ?; if object_id(''sp_StoredProc1'') is not null begin print ''?'' GRANT EXECUTE ON [dbo].[sp_StoredProc1] TO public end'

Page 1 of 1 (9 items)