sp_msforeachdb problem

  • When I run the following command:

    EXEC sp_msforeachdb @COMMAND1="select '?', filename, size from sysfiles"

    It returns each database name, but it only returns the filenames from the databse I'm currently in:

    master D:\SQLServer\Data\JoeTest.mdf 12800

    master E:\SQLServer\Logfiles\JoeTest_log.ldf 6400

    tempdb D:\SQLServer\Data\JoeTest.mdf 12800

    tempdb E:\SQLServer\Logfiles\JoeTest_log.ldf 6400

    My understanding was, this SP was to run the command against EACH database returned by the sp_msforeachdb. Am I thinking about this wrong?

  • What it really does it iterate over the list of databases and replaces the "?" with the database name, so for your query you would want to do this:

    EXEC sp_msforeachdb @COMMAND1='select ''?'', filename, size from ?..sysfiles'

  • Thanks! I tried "close to that" before, but I had the ? in quotes for some reason.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply