Question : how to skip tempdb in SP_MSFOREACHTABLE and SP_SPACEUSED

How to skip Tempdb  in below procedure
The below procedure gives us all tables information in the server, and runs weekly once. This procedures running good but some time its giving the error Error Message:
The object '[dbo].[#t1_000000002A1C]' does not exist in database 'tempdb'.
So then we decided need to skip tempdb in this procedure. But I dont know where I need to modify. Please any body help.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO
 
 
 
alter   PROCEDURE sp_spaceused_all_test @action smallint = 0
AS
 
SET NOCOUNT ON
/* 
 
THIS procedures acuracy is dependant on table statistics being up to date
If table statistics are not up-to-date run
sp_msforeachdb 'DBCC UPDATEUSAGE (''?'')' during off peak hours to recalculate stats.
Takes aproximately 10 minutes to run on both servers. 
 
@ACTION statuses: 
0 = normal. Just returns data
1 = summary. Returns data in summary
2 = insert. Inserts data into history table.
 
EXEC sp_spaceused_all_test @action = 2
*/
CREATE TABLE #t1 (name sysname
	, rows INT
	, reserved VARCHAR(200)
	, data VARCHAR(200)
	, index_size VARCHAR(200)
	, unused VARCHAR(200)
	, db sysname
	)
CREATE TABLE #t2 (
	name sysname
	, rows INT
	, reserved VARCHAR(200)
	, data VARCHAR(200)
	, index_size VARCHAR(200)
	, unused VARCHAR(200)
)
 
DECLARE @sql VARCHAR(2000)
 
 
set @sql = '?.dbo.SP_MSFOREACHTABLE ''INSERT INTO #t2 exec SP_SPACEUSED  ''''|'''''',''|'' INSERT INTO #t1 SELECT *,''?'' FROM #t2 TRUNCATE TABLE #t2'
 
 
EXEC sp_msforeachdb2 @sql
SELECT name
	,rows
	,CAST(REPLACE(reserved,' KB','') AS INT) reserved
	,CAST(REPLACE(data,' KB','') AS INT) data
	,CAST(REPLACE(index_size,' KB','') AS INT) index_size
	,CAST(REPLACE(unused,' KB','') AS INT) unused
	,db
	,Getdate() run_dt
INTO #t3
FROM #t1
WHERE db <> 'tempdb'
 
SELECT name
	,rows
	,CAST(reserved/1024.00/1024.00 AS DEC(18,4)) reserved_gb
	,CAST(data/1024.00/1024.00 AS DEC(18,4)) data_gb
	,CAST(index_size/1024.00/1024.00 AS DEC(18,4)) index_size_gb
	,CAST(unused/1024.00/1024.00 AS DEC(18,4)) unused_gb
	,reserved reserved_kb
	,data data_kb
	,index_size index_size_kb
	,unused unused_kb
	,db
	,run_dt
INTO #t4
FROM #t3
ORDER BY reserved DESC
 
 
IF @action = 1 --Summary
BEGIN
	SELECT db
		,sum(reserved_gb) reserved_gb
		,sum(data_gb) data_gb
		,sum(index_size_gb)index_size_gb
		,sum(unused_gb) unused_gb
		,CAST(CAST(run_dt AS VARCHAR(11)) AS DATETIME) run_dt
	FROM #t4
	GROUP BY db, CAST(CAST(run_dt AS VARCHAR(11)) AS DATETIME) 
	ORDER BY reserved_gb DESC
END
ELSE IF @action = 0 --Detail
BEGIN
	SELECT *
	FROM #t4
	ORDER BY reserved_gb DESC, db, name
END
ELSE IF @action = 2
BEGIN 
 
	--CHECK IF TABLE EXISTS If Not Create
	if not exists (select * from dba_maintenance.dbo.sysobjects where name = 'LT_AUDIT_Test' and xtype = 'U')
	BEGIN 
		CREATE TABLE TestDB.DBO.LT_AUDIT_Test (
		name sysname
		,rows INT
		,reserved_gb DEC(18,4)
		,data_gb DEC(18,4)
		,index_size_gb DEC(18,4)
		,unused_gb DEC(18,4)
		,reserved_kb INT
		,data_kb INT
		,index_size_kb INT
		,unused_kb INT
		,db sysname
		,run_dt DATETIME
		,server sysname
		)
	END
 
	INSERT INTO [Testdb].[dbo].[LT_AUDIT_Test]
	SELECT [name]
		, [rows]
		, [reserved_gb]
		, [data_gb]
		, [index_size_gb]
		, [unused_gb]
		, [reserved_kb]
		, [data_kb]
		, [index_size_kb]
		, [unused_kb]
		, [db]
		, [run_dt]
		, @@SERVERNAME 
	FROM #t4
 
 
END
ELSE 
BEGIN 
 
	GOTO raise_error
 
END
 
DROP TABLE #t1
DROP TABLE #t2
DROP TABLE #t3
DROP TABLE #t4
RETURN
 
raise_error:
 
RAISERROR ('You have Selected an incorrect Action. Actions must be 0 (detail), 1 (summary) or 2 (insert to history table)', 16, 1)
RETURN
SET NOCOUNT OFF
 
 
 
 
GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

Answer : how to skip tempdb in SP_MSFOREACHTABLE and SP_SPACEUSED

sorry...didn't write the db name fully...

sp_msforeachdb ' USE [?] IF DB_NAME() != ''tempdb'' BEGIN  ....statements to run ...END  '
Random Solutions  
 
programming4us programming4us