The Undocumented Sp_MSforeachdb Procedure in SQL Server

Introduction

All versions of SQL Server have some Stored Procedures or functions that are not documented in the MSDN. This may be because they are used by Microsoft internally. This type of Stored Procedure or function (undocumented) can be changed by Microsoft without any notification.

The "sp_MSforeachdb" Stored Procedure comes with SQL Server, but it is not documented in MSDN. This Stored Procedure could be found in the Master database. The Stored Procedure "sp_MSforeachdb" is very similar to sp_MSforeachtable. This Stored Procedure is used to execute a TSQL statement against each database of a SQL Server instance.

Syntax

sp_MSforeachdb [ @command1 = ] 'command1' [ , [ @replacechar = ] replacechar ] [ , [ @command2 = ] command2 ] [ , [ @command3 = ] command3 ] [ , [ @precommand = ] precommand] [ , [ @postcommand = ] postcommand]

Parameter

Parameter Description
@command1 It is the first command to be executed by this Stored Procedure and it data type is nvarchar(2000)
@replacechar It is a character in the command string that needs to be replaced with the table name being processed. Default value of this parameter is a "?".
@command2
@command3
@command2 and @command3 are two additional commands that can be run for each table. Here first Command1 is executing then command2 and then command3 will execute.
@precommand This is command that to be run before the processing any table. It data type is nvarchar(2000)
@postcommand This is command that to be run after the processed all table. It data type is nvarchar(2000)

Definition of procedure in SQL Server 2008 R2

  1. CREATE PROCEDURE sys.sp_MSforeachdb    
  2. @command1 NVARCHAR(2000),   
  3. @replacechar nchar(1) = N'?',   
  4. @command2 NVARCHAR(2000) = NULL,  
  5. @command3 NVARCHAR(2000) = NULL,    
  6. @precommand NVARCHAR(2000) = NULL,   
  7. @postcommand NVARCHAR(2000) = NULL    
  8. AS    
  9.     SET deadlock_priority low    
  10.         
  11.  /* This proc RETURNs one or more rows for each accessible db, with each db defaulting to its own result SET */    
  12.  /* @precommand and @postcommand may be used to force a single result SET via a temp table. */    
  13.     
  14.  /* Preprocessor won't replace within quotes so have to use STR(). */    
  15.  DECLARE @inaccessible NVARCHAR(12), @invalidlogin NVARCHAR(12), @dbinaccessible NVARCHAR(12)    
  16.  SELECT @inaccessible = LTRIM(STR(CONVERT(INT, 0x03e0), 11))    
  17.  SELECT @invalidlogin = LTRIM(STR(CONVERT(INT, 0x40000000), 11))    
  18.  SELECT @dbinaccessible = N'0x80000000'  /* SQLDMODbUserProf_InaccessibleDb; the negative number doesn't work in CONVERT() */    
  19.     
  20.  IF (@precommand IS NOT NULL)    
  21.   EXEC(@precommand)    
  22.     
  23.  DECLARE @origdb NVARCHAR(128)    
  24.  SELECT @origdb = DB_NAME()    
  25.     
  26.  /* IF it's a single user db and there's an entry for it in sysprocesses who isn't us, we can't use it. */    
  27.    /* CREATE the SELECT */    
  28.  EXEC(N'DECLARE hCForEachDatabASe cursor global for SELECT name from mASter.dbo.sysdatabASes d ' +    
  29.    N' where (d.status & ' + @inaccessible + N' = 0)' +    
  30.    N' and (DATABASEPROPERTY(d.name, ''issingleuser'') = 0 and (hAS_dbaccess(d.name) = 1))' )    
  31.     
  32.  DECLARE @retval INT    
  33.  SELECT @retval = @@error    
  34.  IF (@retval = 0)    
  35.   EXEC @retval = sys.sp_MSforeach_worker @command1, @replacechar, @command2, @command3, 1    
  36.     
  37.  IF (@retval = 0 and @postcommand IS NOT NULL)    
  38.   EXEC(@postcommand)    
  39.     
  40.    DECLARE @tempdb NVARCHAR(258)    
  41.    SELECT @tempdb = REPLACE(@origdb, N']', N']]')    
  42.    EXEC (N'use ' + N'[' + @tempdb + N']')    
  43.     
  44.  RETURN @retval   

Example

The following script helps to list a database in a SQL Server instance.

Example script:

exec sp_MSforeachdb 'print "?"'

Output:

undocumented sp_MSforeachdb procedure in SQL server

In another example, the following script helps us to determine the space used and allocated for each database in the SQL Server instance.

Example script:

exec sp_MSforeachdb 'EXECUTE sp_spaceused [?];'

Output:

undocumented sp_MSforeachdb procedure in SQL server1

Common uses of this Stored Procedure

This stored produce may be used for the following purposes:

  • To get the size of all the database of a SQL Server instance
  • To change the owner all the databases to the specific user
  • To check the Logical and Physical integrity of all objects in the database
  • To get all the physical names and attributes (like size, growth, usage) of all databases in a SQL Server instance
  • Run the same command on each database without cursors (like takes back up of all database in SQL Server instance)

Conclusion

These undocumented Stored Procedures can be used if we want to perform the same operation on each database of a SQL Server instance. Please note that Microsoft may change the functionality and definition of this Stored Procedure at any time.
 


Similar Articles