Run same command on all SQL Server databases without cursors

By:   |   Updated: 2022-02-24   |   Comments (36)   |   Related: 1 | 2 | 3 | 4 | 5 | 6 | More > Scripts


Problem

There are times when I find myself needing to run a SQL command against each database on one of my SQL Server instances. There is a handy undocumented stored procedure that allows you to do this without needing to set up a cursor against your sysdatabases table in the master database. This can be done by using sp_MSforeachdb to run the same command in all databases.

Solution

The sp_MSforeachdb procedure is an undocumented procedure that allows you to run the same command against all databases. There are several ways to get creative with using this command and we will cover these in the examples below. This can be used to select data, update data and even create database objects.

General sp_MSforeachdb Syntax

This is the general syntax, where @command is a variable-length string that contains the query you want to run.

EXEC sp_MSforeachdb @command

The "?" Placeholder

In addition to using a straight command, we will see in the examples below how to use ? placeholder which substitutes the database name which allows us to change the context of which database the command is running in.

Example 1: Query Information From All Databases On A SQL Instance

Here is a simple example of where we query a system table from all databases including the system databases.

--Example 1
--This query will return a listing of all tables in all databases on a SQL instance: 

DECLARE @command varchar(1000) 
SELECT @command = 'USE ? SELECT name FROM sysobjects WHERE xtype = ''U'' ORDER BY name' 
EXEC sp_MSforeachdb @command 

You can alternately omit the process of declaring and setting the @command variable. The T-SQL command below behaves identically to the one above and is condensed to a single line of code:

--This query will return a listing of all tables in all databases on a SQL instance: 
EXEC sp_MSforeachdb 'USE ? SELECT name FROM sysobjects WHERE xtype = ''U'' ORDER BY name' 

Example 2: Execute A DDL Query Against All User Databases On A SQL Instance

In this example we will create stored procedure spNewProcedure1 in all databases except for the databases we exclude in the IF statement.

--Example 2
--This statement creates a stored procedure in each user database that will return a listing of all users in a database, sorted by their modification date 

DECLARE @command varchar(1000) 

SELECT @command = 'IF ''?'' NOT IN(''master'', ''model'', ''msdb'', ''tempdb'') BEGIN USE ? 
   EXEC(''CREATE PROCEDURE spNewProcedure1 AS SELECT name, createdate, updatedate FROM sys.sysusers ORDER BY updatedate DESC'') END' 

EXEC sp_MSforeachdb @command 

As you may notice, there are additional items to take into consideration when limiting the scope of the sp_MSforeachdb stored procedure, particularly when creating or modifying objects. You must also set the code to execute if the IF statement is true by using the T-SQL keywords BEGIN and END.

You should take note that the USE ? statement is contained within the BEGIN...END block. It is important to remember key T-SQL rules and account for them. In this case the rule that when creating a procedure, the CREATE PROCEDURE phrase must be the first line of code to be executed. To accomplish this you can encapsulate the CREATE PROCEDURE code within an explicit EXEC() function.

Example 3: Query File Information From All Databases On A SQL Instance

Throughout the examples provided above you saw the use of the question mark as a placeholder for the database name. To reference the database name as a string to be returned in a query, it needs to be embed between a double set of single quotation marks. To treat it as a reference to the database object simply use it by itself.

It is necessary to set the database for the query to run against, by using the USE ? statement, otherwise the code will execute in the context of the current database, for each database in your SQL instance. If you have 5 databases hosted in the current instance and you were to run the stored procedure code above while in the context of DBx it would execute the T-SQL text of the @command 5 times in DBx.

So in example 3 we get the correct output since we are using USE ? and then we use ''?'' to return the actual database name in the query.

--Example 3
--This query will return a listing of all files in all databases on a SQL instance:

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

What happens though if we omit the USE ? clause, which sets the scope of the query? As you can see below, though it is apparent the code executed for each database, it never changed context. Pay particular interest to the filename column and you will see that the query executed from within the context of the master database (where I ran the query from) for each database in the SQL instance (as noted by the database name being returned via the use of the "?" placeholder).

--Example 3b
--Remove the USE ? clause and you end up executing the query repetitively within the context of the current database: 

EXEC sp_MSforeachdb 'SELECT ''?'', SF.filename, SF.size FROM sys.sysfiles SF'
sp_MSforeachdb output

Why Not Just Use a Cursor?

Sure, a cursor can accomplish all that I've presented above, but let's look at the code required to set up a cursor to execute the command used in Example 3:

DECLARE @DB_Name varchar(100) 
DECLARE @Command nvarchar(200) 
DECLARE database_cursor CURSOR FOR 
SELECT name 
FROM MASTER.sys.sysdatabases 

OPEN database_cursor 

FETCH NEXT FROM database_cursor INTO @DB_Name 

WHILE @@FETCH_STATUS = 0 
BEGIN 
     SELECT @Command = 'USE ' + @DB_Name + '; SELECT ' + '''' + @DB_Name + '''' + ', SF.filename, SF.size FROM sys.sysfiles SF'
     EXEC sp_executesql @Command 

     FETCH NEXT FROM database_cursor INTO @DB_Name 
END 

CLOSE database_cursor 
DEALLOCATE database_cursor 

Considering the behavior is similar I'd rather type and execute a single line of T-SQL code versus sixteen.

Next Steps
  • sp_MSforeachdb is extremely useful for pulling together metadata about your various SQL databases. I use it quite frequently for reporting on such important metrics as database file sizes, amount of free space, and backup status.
  • See how this can be used to help you manage SQL Server.
  • Check out this related article Making a more reliable and flexible sp_MSforeachdb


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Tim Ford Tim Ford is a Senior Database Administrator with MindBody.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips


Article Last Updated: 2022-02-24

Comments For This Article




Wednesday, January 18, 2023 - 2:01:13 PM - Greg Robidoux Back To Top (90835)
Hi bludba.

I just tried with SQL Server 2022 and they are working.

Example 3b does not change the output because it does not include "USE ?". The author was trying to show if the database context does not change it will query the same database.

I also added some code to the cursor since it was not changing databases.

Wednesday, January 18, 2023 - 12:18:15 PM - bludba Back To Top (90834)
Hi
the code in these demonstrations don't actually run the queries against the selected databases. i.e. the databiles are all for the master database and not each database in the loop.

Wednesday, April 24, 2019 - 7:33:21 AM - vaishnavi Back To Top (79678)
SELECT @command = 'IF ''?'' NOT IN(''master'') BEGIN USE ? 
EXEC(''CREATE PROCEDURE pNewProcedure1 AS SELECT name, createdate, updatedate FROM sys.sysusers ORDER BY updatedate DESC'') END' 

in this query how to use where condition


Wednesday, January 23, 2019 - 9:16:20 AM - John Dalnes Back To Top (78851)

sp_MSforeachdb is a cursor!  :) Look at the code

EXEC sp_helptext 'sp_MSforeachdb'

exec sp_helptext 'sys.sp_MSforeach_worker'


Monday, April 9, 2018 - 6:37:47 AM - Kamil Back To Top (75644)
I've found that USE ?  sometimes is failing when database name contains - , so I would like recommend to use 

USE [?] is much reliable

Wednesday, July 26, 2017 - 5:32:03 PM - Tapan Back To Top (63299)

Hi Tim,


I found something interesting while reading this post so thought to share.


I was going through the code of sp_MSforeachdb and found that it does work on cursors internally.
Basically, this SP opens the global cursor which will be used by the SP (worker SP - sys.sp_MSforeach_worker - called from current SP).


Wednesday, June 28, 2017 - 3:12:29 PM - sankar Back To Top (58506)

 Hi Tim,

I have 50 databases in my server and i want to run this query on all databases.

EXEC sp_change_users_login 'Auto_Fix', 'XXXX', 'XXXX', 'XXXX'

My user, login, password are same for all the DB's

Please lete me know on this.

 

Thanks

 


Wednesday, July 15, 2015 - 8:48:01 AM - bN Back To Top (38212)

Thank you very much !


Monday, November 10, 2014 - 6:36:10 AM - Prameela Back To Top (35239)
Hi,

Your cursor script Is giving the output wrongly. It is returning the database name in loop but for all databases file name and size of the file is master.msd and master.ldf. please look into the attachment.

 


Sunday, June 2, 2013 - 6:34:40 AM - Menahem Shcolnick Back To Top (25237)

You used:

'IF ''[?]'' NOT IN (''master'',''tempDB'',''model'',''msdb'')

You Need to to remove the [] signs and Use:

'IF ''?'' NOT IN (''master'',''tempDB'',''model'',''msdb'')


Wednesday, March 6, 2013 - 4:40:16 PM - newbie Back To Top (22607)

I need assisance, please.  I am using the sp_msForcEachDB and it is having difficulities.  1) although, I have the filter on the databases excluding master, model, msdb, tempdb, it is still executing the script in them.  2) when the script executes, it only seems to get a few of the databases and not all of them.  3) I have also tried putting the script into a variable and running the exec sp_msforEachDB @command  What am I doing wrong?  I am testing the script as a print to be sure it is working before I run the execute.  Yes, this is a cursor, I realize you are all going to 'smack' me on that also.    The purpose of the script is to reorg indexes that are fragmented between 5 and 30 percent.  Here is my script... I have included the output at the bottom. 

 

EXEC

 

 

sp_MSforeachdb

 

'IF ''[?]'' NOT IN (''master'',''tempDB'',''model'',''msdb'')

BEGIN

 

use [?]

 

DECLARE @dbName sysname,@table varchar(50), @index varchar(50), @frag int, @msg varchar(500),

 

@sql nvarchar(500), @dbsql nvarchar(500)

 

declare index_cur cursor for

 

SELECT OBJECT_NAME(object_id) AS tblName

 

,[?].dbo.index_name(object_id, index_id) AS ixName

 

,avg_fragmentation_in_percent

 

FROM [?].sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL, NULL)

 

WHERE avg_fragmentation_in_percent between 5 and 30

 

and index_type_desc IN(''CLUSTERED INDEX'', ''NONCLUSTERED INDEX'')

 

 

 

open index_cur

 

FETCH NEXT FROM index_cur into @table, @index, @frag

 

WHILE @@FETCH_STATUS = 0

 

BEGIN

 

if objectproperty (object_id (@table), ''OwnerId'') = 1

 

set @sql = N''ALTER INDEX '' + quotename(@index) + '' ON '' + ''[dbo].''+ quotename(@table)

 

+ '' REORGANIZE''

 

print @sql

 

FETCH NEXT FROM index_cur into @table, @index, @frag

 

 

 

END

 

 

 

CLOSE index_cur

 

DEALLOCATE index_cur

 

END'

 

 

 

 

Msg 4121, Level 16, State 1, Line 9

 

Cannot find either column "tempdb" or the user-defined function or aggregate "tempdb.dbo.index_name", or the name is ambiguous.

 

Msg 4121, Level 16, State 1, Line 9

 

Cannot find either column "model" or the user-defined function or aggregate "model.dbo.index_name", or the name is ambiguous.

 

Msg 4121, Level 16, State 1, Line 9

 

Cannot find either column "msdb" or the user-defined function or aggregate "msdb.dbo.index_name", or the name is ambiguous.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ALTER INDEX [IX_bs_unitanks_FloatTable_DateAndTime] ON [dbo].[BS_UNITANKS_FloatTable] REORGANIZE

 

ALTER INDEX [IX_beerfilter_FloatTable_DateAndTime] ON [dbo].[BeerFilter_FloatTable] REORGANIZE

 

ALTER INDEX [XPKItem] ON [dbo].[Item] REORGANIZE

 

 

 

 

 


Wednesday, December 26, 2012 - 3:13:45 PM - Christina Back To Top (21140)

I need to loop the following script through all databases.  I have tried almost everything I have researched online without any success.  Any suggestions?

 

DECLARE @table_name VARCHAR(500)  

 

DECLARE @schema_name VARCHAR(500)  

DECLARE @tab1 TABLE( 

        tablename VARCHAR (500) collate database_default 

       ,schemaname VARCHAR(500) collate database_default 

CREATE TABLE #temp_Table ( 

       [tablename] [sysname] NOT NULL,

[row_count] [int] NULL,

[reserved] [varchar](50) NULL,

[data] [varchar](50) NULL,

[index_size] [varchar](50) NULL,

[unused] [varchar](50) NULL)  

 

INSERT INTO @tab1  

SELECT Table_Name, Table_Schema  

FROM information_schema.tables  

WHERE TABLE_TYPE = 'BASE TABLE' 

 

 

 

DECLARE c1 CURSOR FOR 

SELECT Table_Schema + '.' + Table_Name   

FROM information_schema.tables t1  

WHERE TABLE_TYPE = 'BASE TABLE' 

 

OPEN c1 

FETCH NEXT FROM c1 INTO @table_name 

WHILE @@FETCH_STATUS = 0  

BEGIN   

        SET @table_name = REPLACE(@table_name, '[','');  

        SET @table_name = REPLACE(@table_name, ']','');  

 

        -- make sure the object exists before calling sp_spacedused 

        IF EXISTS(SELECT id FROM sysobjects WHERE id = OBJECT_ID(@table_name)) 

        BEGIN 

               INSERT INTO #temp_Table EXEC sp_spaceused @table_name, false;

                     END 

         

        FETCH NEXT FROM c1 INTO @table_name 

END 

CLOSE c1 

DEALLOCATE c1 

 

SELECT  t1.* 

       ,t2.schemaname  

FROM #temp_Table t1  

INNER JOIN @tab1 t2 ON (t1.tablename = t2.tablename ) 

ORDER BY schemaname,t1.tablename; 

 

ALTER TABLE #temp_Table ADD [DATE] DATE

UPDATE #temp_Table SET [DATE] =  CURRENT_TIMESTAMP

 

AlteR TABLE #temp_Table ADD [DATABASE] varchar (100)

UPDATE #temp_Table SET [DATABASE] = DB_NAME() 

 

CREATE TABLE #temp_Table_test (name varchar(100),

data_compression tinyint,

data_compression_desc varchar(100))

 

insert into #temp_Table_test SELECT T.name, P.data_compression, P.data_compression_desc 

 FROM SYS.TABLES T, SYS.partitions P

WHERE T.OBJECT_ID = P.OBJECT_ID

GROUP BY T.name, P.data_compression, P.data_compression_desc

 

 

ALTER TABLE #temp_Table ADD [COMPRESSED] VARCHAR (50)

UPDATE #temp_Table 

set [COMPRESSED] = b.data_compression_desc

from #temp_Table a,

#temp_Table_test b

where a.tablename = b.name

 

 

 

 

 

----------------------------------------------

insert into bic_reference.dbo.test_database_stats  

select * from #temp_Table

 

DROP TABLE #temp_Table

Drop Table #temp_Table_test

 

 

 

 


Tuesday, December 4, 2012 - 6:28:32 AM - Jordan Back To Top (20729)

Thanks a lot man ! You save my day.

Regards,

Jordan


Friday, August 3, 2012 - 7:14:31 AM - Amol Birar Back To Top (18901)

Very useful indeed


Tuesday, June 19, 2012 - 1:55:45 PM - Kevin Archibald Back To Top (18105)

Nice!  I use this to shrink db and truncate logs of EVERY DB.

 

DECLARE @cmd1 nvarchar(2000)

SET @cmd1 = 'IF ''?'' NOT IN(''master'', ''model'', ''tempdb'', ''msdb'')' + 'BEGIN ' 

+ 'USE ?

DBCC SHRINKFILE( ?_log, 1 )

BACKUP LOG ? WITH TRUNCATE_ONLY

DBCC SHRINKFILE( ?_log, 1 )

END

 

EXEC sp_MSForEachdb 

@command1 = @cmd1 

GO


Monday, June 18, 2012 - 5:34:53 PM - Ludwig Guevara Back To Top (18090)

Hi,

I also use a simple script (Callit from a Job) to check for the status of all the databases.

Runs after a Failover or a Server Reboot.

 

CREATE PROC [dbo].[usp_CheckDB]
AS
BEGIN
    BEGIN TRY
        BEGIN TRANSACTION
            exec sp_msforeachdb '
                use [?];
                exec sp_helpdb ?';
       
    END TRY
    ---
    BEGIN CATCH
        DECLARE @ErrorMessage NVARCHAR(4000);
        DECLARE @ErrorSeverity INT;
        DECLARE @ErrorState INT;

        SELECT
            @ErrorMessage = ERROR_MESSAGE(),
            @ErrorSeverity = ERROR_SEVERITY(),
            @ErrorState = ERROR_STATE();

        -- Use RAISERROR inside the CATCH block to return error
        -- information about the original error that caused
        -- execution to jump to the CATCH block.
        RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState );
    END CATCH
    COMMIT
END


Monday, June 18, 2012 - 5:32:37 PM - Ludwig Guevara Back To Top (18089)

Hi,

I use this script to fix all Orphan users in a Server

(Use a cursor, because requires to check for all the users in the database)

exec sp_msforeachdb '
        USE [?];
       
        DECLARE
            @DB sysname
           
        SELECT DB_NAME()
       
        BEGIN
            BEGIN
                DECLARE
                    @username varchar(25)
               
                DECLARE fixusers CURSOR FOR
                    SELECT UserName = name
                    FROM sysusers
                    WHERE issqluser = 1
                        AND (sid is not null and sid <> 0x0)
                        AND name <>''dallas''
                        AND suser_sname(sid) is null
                    ORDER BY name
                   
                    OPEN fixusers
                        FETCH NEXT FROM fixusers
                            INTO @username
                           
                        WHILE @@FETCH_STATUS = 0
                            BEGIN
                                EXEC sp_change_users_login ''update_one'', @username, @username
                                FETCH NEXT FROM fixusers
                                    INTO @username
                            END   
                    CLOSE fixusers
                DEALLOCATE fixusers
            END
                       
        END'

Thanks for the tip

Regards


Wednesday, May 16, 2012 - 6:40:50 AM - Sandeep Maher Back To Top (17478)

Hi,

I got your email id in forum.
I am having issues in SQL,
Issues is that I am having more than 50 customers and there is more than 50 Database in SQl for every customers.
Now All SP's And Functions are same but now problem is in maintenance, If I modify any SP then I need to modify SP manually in all database.
So is there any way to avoid such maintenance problem  So I can modify one SP and that will get update in all remaining Database?

Or Can I get script in c# code using the can i modify SP in all database?
EXEC sp_MSforeachdb can not work for me because some of my SP text length is more than 10,000.

Kindly help me.. Thanks in Advance.


Tuesday, May 1, 2012 - 6:27:28 PM - corey_lawson Back To Top (17226)

This, and sp_msforeachtable, are internally not magic or undocumented, in that they don't invoke any CLR functionality, extended stored procs, obfuscated TSQL, etc.

They're just plain ol' TSQL procs that executes the code specified in the parameters inside a CURSOR that loops over the desired objects...

 


Tuesday, March 13, 2012 - 11:54:24 PM - SURAJ Back To Top (16367)

Dear sir

      I AM FACING SOME PROBLEM IN SQL QUERY CURSOR DECLARATION I WANT TO USE CURSOR WITHOUT DECLARE VARIABLE

    PLEASE GIVE ME SOLUCTION

   MENTON MY QUERY

    DECLARE @ABCTABLE AS TABLE  (STKCODE INT,STOCKNO VARCHAR(20))

    declare abc_cursor cursor for select  stkcode ,stockno from stock   

   OPEN abc_curosr                ** THIS QUERY EXECUTE  SUCESSFUL BUT HOW TO USE IN FETCH NEXT FROM CURSOR

    FETCH NEXT FROM abc_curosr  INTO @stkcode,@stockno      ** ERROR CURSOR VARIABLE NOT READ

         WHILE @@FETCH_STATUS = 0
           BEGIN

              INSET INTO  @ABCTABLE (STKCODE,STOCKNO) VALUES (@stkcode,@stockno)

            FETCH NEXT FROM abc_curosr       INTO  @DB_Name
END

CLOSE abc_curosr      
DEALLOCATE abc_curosr      


Tuesday, March 13, 2012 - 11:35:27 PM - surajkuamwat Back To Top (16366)

Respect sir

             i am facing some problem in sql query  using cursor declaration t was try to do with declare variable

           

 

              declare

 

 

cmain cursorforselect*from gstock

 

                open

 

cmain

             how to use cursor in fetch status...

 

 


Tuesday, February 28, 2012 - 4:31:43 AM - akash Back To Top (16187)

Server: Msg 105, Level 15, State 1, Line 3
Unclosed quotation mark before the character string '\General/S'.


Tuesday, February 28, 2012 - 4:31:02 AM - akash Back To Top (16186)

DECLARE @DB_Name varchar(100)
DECLARE @Command nvarchar(200)
DECLARE database_cursor CURSOR FOR
SELECT name FROM master..sysdatabases where name like '%_Se'

OPEN database_cursor

FETCH NEXT FROM database_cursor INTO @DB_Name

WHILE @@FETCH_STATUS = 0
BEGIN

SELECT @Command = 'USE ['+ @DB_NAME +  '] INSERT INTO  genMenuMaster
                    (MenuId, ModuleId, MenuName, MenuType, ParentId, MenuURL, [Position])
VALUES     (1163, 32, ''SMS Mobile DND Status Report'', 0, ''General/SMSMobileDNDStatus.aspx'', 60)'
   EXEC sp_executesql @Command

     FETCH NEXT FROM database_cursor INTO @DB_Name
END

CLOSE database_cursor
DEALLOCATE database_cursor

 


Tuesday, February 7, 2012 - 8:49:37 AM - Fernando Paez Becker Back To Top (15920)

There is anothers parameters to comments... EXEC sp_msforeachdb sintax accepts

EXEC sp_msforeachdb @precommand='---YOUR TEXT FOR GENERAL PURPUSE HERE-TEMP TABLE CREATION IE...

CREATE TABLE ##TEMP1 (ID INT,NAME VARCHAR(15);SELECT * INTO ##TEMP2 FROM ##TEMP1',

@COMMAND1='USE [?];SELECT * INTO ##TEMP1 WHERE ...',

@COMMAND2='USE [?];SELECT * INTO ##TEMP2 WHERE ...',

 @COMMAND3='USE [?];SELECT * INTO ##TEMP1 WHERE ...',

@POSTCOMMAND='SELECT * FROM ##TEMP1 UNION SELECT * FROM ##TEMP2;DROP TABLE ##TEMP1;DROP TABLE ##TEMP2;'

 

 


Thursday, September 8, 2011 - 4:16:37 PM - Jason Back To Top (14618)

When you need a special condition of list of DBs such as not offline, not read-only, use temp table or CTE or table variable, for example, the code below

Regards,

Jason

http://usa.redirectme.net

 

-----------------

SET NOCOUNT ON

DECLARE @dbName varchar(255), @rc int

SELECT name
INTO #db
FROM sys.databases
WHERE
    name NOT IN ('master', 'model', 'msdb', 'tempdb', 'admindb') AND
    name NOT LIKE '%ReportServer%' AND
    DATABASEPROPERTYEX([name], 'IsInStandBy') = 0 AND
    DATABASEPROPERTYEX([name], 'Status') = 'ONLINE'

SELECT @rc = 1, @dbName = MIN(name)
FROM #db

WHILE @rc <> 0
BEGIN

    EXEC DB.schema.DO_SOMETHING


    SELECT TOP 1 @dbName = name
    FROM #db
    WHERE name > @dbName
    ORDER BY name

    SET @rc = @@ROWCOUNT
END

DROP TABLE #db


Tuesday, February 1, 2011 - 11:49:50 AM - Larrybud Back To Top (12774)

There are times when you might need a cursor:

1) running a script against certain databases, instead of all

2) wanting your script results in one table (for a select, for example)

 


Wednesday, November 3, 2010 - 10:11:28 AM - Pradip Patel Back To Top (10329)

You need to replace

     SELECT @Command = 'SELECT ' + '''' + @DB_Name + '''' + ', SF.filename, SF.size FROM sys.sysfiles SF'
with

     SELECT @Command = 'USE '+ @DB_NAME +  ' SELECT ' + '''' + @DB_Name + '''' + ', SF.filename, SF.size FROM sys.sysfiles SF'

when you want to execute by cursor otherwise it gives you all file name as master.mdf

 

 

 


Monday, April 26, 2010 - 3:02:40 PM - thomashmaine Back To Top (5287)

is there a way to have a variabled declared  withing the statement?

 

select @command1 = 'use [?]; exec sp_grantlogin [@user2]'

print @user2

exec sp_MSforeachdb @command1


Friday, January 29, 2010 - 12:02:55 PM - aprato Back To Top (4820)

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


Wednesday, January 27, 2010 - 11:19:23 AM - Akbar Back To Top (4799)
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.

Friday, November 20, 2009 - 3:49:19 AM - bobmihada Back To Top (4474)
--// 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

Sunday, December 14, 2008 - 9:04:32 PM - timmer26 Back To Top (2404)

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


Wednesday, December 10, 2008 - 3:23:23 PM - robvon Back To Top (2387)

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


Friday, April 4, 2008 - 7:07:46 AM - admin Back To Top (822)

 Thanks so much. 

Tim Ford did a great job with this tip.

Thank you,
The MSSQLTips.com Team


Thursday, April 3, 2008 - 11:53:47 AM - prasanthg74 Back To Top (811)

nice script ! thanks a bunch!


Thursday, January 17, 2008 - 5:23:02 PM - probaby Back To Top (223)

Thanks for your wonderful scripts!

You save my time and money!

Have a great day!

 















get free sql tips
agree to terms