{"id":81,"date":"2011-12-16T12:46:19","date_gmt":"2011-12-16T12:46:19","guid":{"rendered":"https:\/\/sqlserverlearner.azurewebsites.net\/2011\/12\/16\/sp_msforeachdb-understanding-and-using-indetail\/"},"modified":"2011-12-16T12:46:19","modified_gmt":"2011-12-16T12:46:19","slug":"sp_msforeachdb-understanding-and-using-indetail","status":"publish","type":"post","link":"https:\/\/sqlserverlearner.com\/2011\/12\/16\/sp_msforeachdb-understanding-and-using-indetail\/","title":{"rendered":"sp_MSforeachdb – Understanding and Using – InDetail"},"content":{"rendered":"

table.hovertable {
\nfont-family: verdana,arial,sans-serif;
\nfont-size:11px;
\ncolor:#333333;
\nborder-width: 1px;
\nborder-color: #999999;
\nborder-collapse: collapse;
\n}
\ntable.hovertable th {
\nbackground-color:#c3dde0;
\nborder-width: 1px;
\npadding: 8px;
\nborder-style: solid;
\nborder-color: #a9c6c9;
\n}
\ntable.hovertable tr {
\nbackground-color:#d4e3e5;
\n}
\ntable.hovertable td {
\nborder-width: 1px;
\npadding: 8px;
\nborder-style: solid;
\nborder-color: #a9c6c9;
\n}<\/p>\n

The System stored procedure sp_MSforeachdb is not listed in MSDN.<\/p>\n

Following is the indept analysis on this procedure.
\nThis procedure is used to run a command\/query using all the databases on the server.
\n? is used to indicate the database name.
\nThe query passed to this procedure is executed n number of times, where n is the number of databases on the server.<\/p>\n

Name: sp_MSforeachdb
\nOwner: sys
\nType: stored procedure<\/p>\n

Input Parameters:<\/p>\n\n\n\n\n\n\n\n
@command1<\/td>\nnvarchar(4000)<\/td>\n<\/tr>\n
@@replacechar<\/td>\nnchar(2)<\/td>\n<\/tr>\n
@@command2<\/td>\nnvarchar(4000)<\/td>\n<\/tr>\n
@@command3<\/td>\nnvarchar(4000)<\/td>\n<\/tr>\n
@@precommand<\/td>\nnvarchar(4000)<\/td>\n<\/tr>\n
@@postcommand<\/td>\nnvarchar(4000)<\/td>\n<\/tr>\n<\/table>\n

\nExample query:
\n[sql]
\nexec sp_MSforeachdb ‘select ”?”’
\n[\/sql]<\/p>\n

The above query will list all the database names.<\/p>\n

[sql]
\nexec sp_MSforeachdb ‘select DB_NAME()’
\n[\/sql]<\/p>\n

The above query will return the same database name but for n number of times, where n is the number of databases.<\/p>\n

we can come up with multiple uses of this procedure.
\nUse ? where ever you need to use the database name.<\/p>\n

[sql]
\nexec sp_MSforeachdb ‘select ”?” as databasename,count(*) from [?].sys.tables’
\n[\/sql]<\/p>\n

The above query will give the number of tables in each database.<\/p>\n

@replacechar is the second input parameter which specifies the charecter that has to be replaced by the database name. Default value for this parameter is ?.<\/p>\n

[sql]
\nexec sp_MSforeachdb ‘select ”&”’,’&’
\n[\/sql]<\/p>\n

In the above query & is used instead of ?.<\/p>\n

The next parameters @command2 and @command3 are the next set of commands that can be run for each database name.<\/p>\n

[sql]
\nexec sp_MSforeachdb ‘select ”&”’,’&’,’select ”Command2&”’,’select ”command3&”’
\n[\/sql]<\/p>\n

The above query runs @command1, @command2 and @command3 in the same order for each database name.<\/p>\n

@precommand is the command that is executed first(before starting process for the first database)
\n@postcommand is the command that is executed last(after completing process for the last database) <\/p>\n

[sql]
\nexec sp_MSforeachdb ‘select ”?”’,’?’,’select ”command2?”’,’select ”command3?”’,’select ”start”’,’select ”end”’
\n[\/sql]<\/p>\n

The above query executes the following:
\n – executes @precommand
\n – executes @command1, @command2 and @command3 in the same order for each database name
\n – executes @postcommand<\/p>\n

Further sp_MSforeachtable is a similar procedure that is used for executing a query on each table.<\/p>\n","protected":false},"excerpt":{"rendered":"

table.hovertable { font-family: verdana,arial,sans-serif; font-size:11px; color:#333333; border-width: 1px; border-color: #999999; border-collapse: collapse; } table.hovertable th { background-color:#c3dde0; border-width: 1px; padding: 8px; border-style: solid; border-color: #a9c6c9; } table.hovertable tr { background-color:#d4e3e5; } table.hovertable td { border-width: 1px; padding: 8px; border-style: solid; border-color: #a9c6c9; } The System stored procedure sp_MSforeachdb is not listed in MSDN. Following is…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[419,420,421,422],"_links":{"self":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/81"}],"collection":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/comments?post=81"}],"version-history":[{"count":0,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/posts\/81\/revisions"}],"wp:attachment":[{"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/media?parent=81"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/categories?post=81"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sqlserverlearner.com\/wp-json\/wp\/v2\/tags?post=81"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}