SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to Loop through all DBs in an Instance?


How to Loop through all DBs in an Instance?

Author
Message
ReamerXXVI
ReamerXXVI
SSChasing Mays
SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)

Group: General Forum Members
Points: 655 Visits: 372
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 #nameOfAllDbsInTheInstance
FROM sys.sysdatabases

DECLARE DBName CURSOR
FOR SELECT D.[name]
FROM #nameOfAllDbsInTheInstance AS D
OPEN DBName
FETCH NEXT FROM DBName
INTO @DatabaseName

WHILE @@FETCH_STATUS = 0

BEGIN

SET @String = 'USE ' + @DatabaseName
PRINT @String

EXEC(@String)
SELECT DB_NAME()

FETCH NEXT FROM DBName
INTO @DatabaseName

END

DROP TABLE #nameOfAllDbsInTheInstance
CLOSE DBName
DEALLOCATE DBName

------------------------------------------
Bhuvnesh
Bhuvnesh
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32148 Visits: 4079
YOur cursor doesnt do here anything , simply "USe database"
What actaully you are trying to achieve here ?

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
ReamerXXVI
ReamerXXVI
SSChasing Mays
SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)SSChasing Mays (655 reputation)

Group: General Forum Members
Points: 655 Visits: 372
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?
Bhuvnesh
Bhuvnesh
SSC-Dedicated
SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)SSC-Dedicated (32K reputation)

Group: General Forum Members
Points: 32148 Visits: 4079
tom.moore.777 89426 (1/23/2013)
the whole point is how to change DB context dynamically?

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

-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done;-)
Kingston Dhasian
Kingston Dhasian
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11327 Visits: 5329
tom.moore.777 89426 (1/23/2013)

Can anyone provide a method to loop through a DB's in an instance WITHOUT using the proc sp_MSforeachdb


Any particular reason for avoiding the use of sp_MSforeachdb?


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: General Forum Members
Points: 143621 Visits: 18649
Kingston Dhasian (1/23/2013)
tom.moore.777 89426 (1/23/2013)

Can anyone provide a method to loop through a DB's in an instance WITHOUT using the proc sp_MSforeachdb


Any particular reason for avoiding the use of sp_MSforeachdb?


msForeachdb has a few bugs and limits.

iirc there was a character limitation. There is also inconsistent behavior such as it skipping databases.

Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)SSC Guru (143K reputation)

Group: General Forum Members
Points: 143621 Visits: 18649
Here are two alternatives to sp_MSforeachdb

http://sqlblog.com/blogs/aaron_bertrand/archive/2010/12/29/a-more-reliable-and-more-flexible-sp-msforeachdb.aspx

And then this one that I have been using:
http://spaghettidba.com/2011/09/09/a-better-sp_msforeachdb/

With a few samples of usage here
http://jasonbrimhall.info/2012/07/17/a-trio-of-eachdb/

Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw
Learn Extended Events

anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62458 Visits: 8590
If you want to do the same thing in each database, then this would be another alternative solution


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)
),
'&#x 0D;',CHAR(13) + CHAR(10)
)
--SELECT @SQL
EXECUTE sp_executesql @SQL



Just remove the space between the x and the 0 in the following string in the script '&#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.



How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search