﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server 2008 - General  / How to Loop through all DBs in an Instance? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 13:38:11 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>If you want to do the same thing in each database, then this would be another alternative solution[code="sql"]DECLARE @SQL NVARCHAR(MAX)SELECT @SQL = 	REPLACE(		CAST(			(				SELECT 'USE ' + QUOTENAME(name) +';' + CHAR(13) + CHAR(10) +				--PUT WHAT YOU WANT TO DO IN EACH DATABASE IN THIS BLOCK				----					'SELECT ' + CHAR(13) + CHAR(10) +						'DatabaseName = DB_NAME(), ' + CHAR(13) + CHAR(10) +						'a.FILEID, ' + CHAR(13) + CHAR(10) +						'[FILE_SIZE_MB] = CONVERT(DECIMAL(12, 2), ROUND(a.size / 128.000, 2)), ' + CHAR(13) + CHAR(10) +						'[SPACE_USED_MB] = CONVERT(DECIMAL(12, 2), ROUND(fileproperty(a.NAME, ' + CHAR(39) + 'SpaceUsed' + CHAR(39) +') / 128.000, 2)), ' + CHAR(13) + CHAR(10) +						'[FREE_SPACE_MB] = CONVERT(DECIMAL(12, 2), ROUND((a.size - fileproperty(a.NAME, ' + CHAR(39) + 'SpaceUsed' + CHAR(39) +')) / 128.000, 2)), ' + CHAR(13) + CHAR(10) +						'a.NAME, a.FILENAME ' + CHAR(13) + CHAR(10) +					'FROM dbo.sysfiles a;' + CHAR(13) + CHAR(10)				----				FROM 					sys.databases				FOR XML PATH('')			) AS NVARCHAR(MAX)		),		'&amp;#x 0D;',CHAR(13) + CHAR(10)	)	--SELECT @SQLEXECUTE sp_executesql @SQL[/code]Just remove the space between the x and the 0 in the following string in the script '&amp;#x 0D;'The above example loops through all the databaes and gets their file usage, free space, used space, total space etc, so I can track DB growth.</description><pubDate>Thu, 24 Jan 2013 01:51:39 GMT</pubDate><dc:creator>anthony.green</dc:creator></item><item><title>RE: How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>Here are two alternatives to sp_MSforeachdb[url]http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/29/a-more-reliable-and-more-flexible-sp-msforeachdb.aspx[/url]And then this one that I have been using:[url]http://spaghettidba.com/2011/09/09/a-better-sp_msforeachdb/[/url]With a few samples of usage here[url]http://jasonbrimhall.info/2012/07/17/a-trio-of-eachdb/[/url]</description><pubDate>Thu, 24 Jan 2013 00:19:48 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>[quote][b]Kingston Dhasian (1/23/2013)[/b][hr][quote][b]tom.moore.777 89426 (1/23/2013)[/b][hr]Can anyone provide a method to loop through a DB's in an instance WITHOUT using the proc sp_MSforeachdb [/quote]Any particular reason for avoiding the use of sp_MSforeachdb?[/quote]msForeachdb has a few bugs and limits.iirc there was a character limitation.  There is also inconsistent behavior such as it skipping databases.</description><pubDate>Thu, 24 Jan 2013 00:17:46 GMT</pubDate><dc:creator>SQLRNNR</dc:creator></item><item><title>RE: How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>[quote][b]tom.moore.777 89426 (1/23/2013)[/b][hr]Can anyone provide a method to loop through a DB's in an instance WITHOUT using the proc sp_MSforeachdb [/quote]Any particular reason for avoiding the use of sp_MSforeachdb?</description><pubDate>Wed, 23 Jan 2013 23:57:52 GMT</pubDate><dc:creator>Kingston Dhasian</dc:creator></item><item><title>RE: How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>[quote][b]tom.moore.777 89426 (1/23/2013)[/b][hr]the whole point is how to change DB context dynamically?[/quote]yes it can be done with the same way as you did but need additional code too like :set @lstr = 'USe ' + @databasename + '; create table Test (id int ) ;'exec (@lstr)this will create test table in all the databases passed by @databasename from cursor</description><pubDate>Wed, 23 Jan 2013 23:55:36 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>RE: How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>It is just an example, the cursor here just gets the DBName and I try and change to the context of that DB.I know it's not actually do anything as it is just and example, the whole point is how to change DB context dynamically?</description><pubDate>Wed, 23 Jan 2013 23:01:59 GMT</pubDate><dc:creator>ReamerXXVI</dc:creator></item><item><title>RE: How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>YOur cursor doesnt do here anything , simply "USe database" What actaully you are trying to achieve here ?</description><pubDate>Wed, 23 Jan 2013 22:53:51 GMT</pubDate><dc:creator>Bhuvnesh</dc:creator></item><item><title>How to Loop through all DBs in an Instance?</title><link>http://www.sqlservercentral.com/Forums/Topic1410900-391-1.aspx</link><description>Hi,Can anyone provide a method to loop through a DB's in an instance WITHOUT using the proc sp_MSforeachdb I have tried the following, and the "EXEC(@String)" does not error, but I always stay in the context of the DB I run it from.Very frustrating, there must be a way to do this?!?!?!?------------------------------------------DECLARE @DatabaseName nvarchar(50)DECLARE @String nvarchar(50)SELECT name INTO #nameOfAllDbsInTheInstanceFROM sys.sysdatabasesDECLARE DBName CURSOR FOR SELECT D.[name]FROM #nameOfAllDbsInTheInstance AS DOPEN DBNameFETCH NEXT FROM DBNameINTO @DatabaseName		  WHILE @@FETCH_STATUS = 0		BEGIN 		SET @String = 'USE ' + @DatabaseName		PRINT @String			EXEC(@String)		SELECT DB_NAME()	 		FETCH NEXT FROM DBName 		INTO @DatabaseName				ENDDROP TABLE #nameOfAllDbsInTheInstanceCLOSE DBNameDEALLOCATE DBName------------------------------------------</description><pubDate>Wed, 23 Jan 2013 22:47:34 GMT</pubDate><dc:creator>ReamerXXVI</dc:creator></item></channel></rss>