Comments on: Operations On All Tables In All Databases in SQL Server using sp_MSforeachdb and sp_MSforeachtable https://sqlserverlearner.com/2011/12/21/operations-on-all-tables-in-all-databases-in-sql-server-using-sp_msforeachdb-and-sp_msforeachtable/ LEARN SQL SERVER ONLINE Fri, 11 Jan 2013 05:20:57 +0000 hourly 1 https://wordpress.org/?v=6.5.2 By: Devi Prasad https://sqlserverlearner.com/2011/12/21/operations-on-all-tables-in-all-databases-in-sql-server-using-sp_msforeachdb-and-sp_msforeachtable/comment-page-1/#comment-417 Fri, 11 Jan 2013 05:20:57 +0000 https://sqlserverlearner.azurewebsites.net/2011/12/21/operations-on-all-tables-in-all-databases-in-sql-server-using-sp_msforeachdb-and-sp_msforeachtable/#comment-417 In reply to Mick Wagner.

Hi Mack,

Below query includes the database name in the temp table.So that you will find the list of all tables in all databases in temp table(#Tables) along with the database name.
[sql]
–drop table #Tables
CREATE TABLE #Tables (
DatabaseName VARCHAR(255),
TableName VARCHAR(255),
rows int,
reserved VARCHAR(255),
data VARCHAR(255),
index_size VARCHAR(255),
unused VARCHAR(255) );

EXEC sp_MSforeachdb
@command1 = ‘
IF not exists(select 1 where ”?” in (”master”,”model”,”msdb”,”tempdb”))
EXEC [?].dbo.sp_MSforeachtable
@command1 = ”INSERT INTO #Tables(TableName,rows,reserved,data,index_size,unused) EXEC sp_spaceused ””&”””,
@replacechar = ”&”

UPDATE #Tables SET databasename = ”?” WHERE databasename IS NULL

SELECT SUM(rows) from #Tables
SELECT MAX(rows) from #Tables
[/sql]

]]>
By: Mick Wagner https://sqlserverlearner.com/2011/12/21/operations-on-all-tables-in-all-databases-in-sql-server-using-sp_msforeachdb-and-sp_msforeachtable/comment-page-1/#comment-416 Tue, 08 Jan 2013 23:26:02 +0000 https://sqlserverlearner.azurewebsites.net/2011/12/21/operations-on-all-tables-in-all-databases-in-sql-server-using-sp_msforeachdb-and-sp_msforeachtable/#comment-416 Great query! I was working on something similiar but couldn’t get it to work.

Is there a way to include the database name in the temp table in query1?

Thanks,

]]>