Advertisement

03.17.2008 at 08:27AM PDT, ID: 23247311
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.2

how to skip tempdb in SP_MSFOREACHTABLE and SP_SPACEUSED

Asked by Godir8 in MS SQL Server, MySQL Server

Tags:

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.
Start Free Trial
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
 
Loading Advertisement...
 
[+][-]03.17.2008 at 08:33AM PDT, ID: 21143130

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.17.2008 at 08:34AM PDT, ID: 21143137

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, MySQL Server
Tags: T-sql
Sign Up Now!
Solution Provided By: chapmandew
Participating Experts: 1
Solution Grade: A
 
 
[+][-]03.17.2008 at 09:16AM PDT, ID: 21143587

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.17.2008 at 09:26AM PDT, ID: 21143667

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.17.2008 at 10:02AM PDT, ID: 21144056

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.17.2008 at 10:11AM PDT, ID: 21144122

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.17.2008 at 10:34AM PDT, ID: 21144341

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.17.2008 at 10:38AM PDT, ID: 21144378

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.17.2008 at 10:48AM PDT, ID: 21144447

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]05.01.2008 at 09:57AM PDT, ID: 21480391

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]05.04.2008 at 09:09AM PDT, ID: 21496074

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]05.08.2008 at 06:44PM PDT, ID: 21529899

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080924-EE-VQP-39 / EE_QW_2_20070628