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


Using XP_EXECRESULTSET To Obtain Database Size Information and More


Using XP_EXECRESULTSET To Obtain Database Size Information and More

Author
Message
MudLuck
MudLuck
SSC Veteran
SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)SSC Veteran (257 reputation)

Group: General Forum Members
Points: 257 Visits: 519

J.T.

Wanted to get your opinion on using this method you proposed versus using Dynamic SQL and say sp_executesql or EXECUTE?

1. does the optimizer handle them diffrently.

a. Are they treated as two seperate query plans for the same query or one.

b. Can XP_EXECRESULTSET take advantage of the caching of multiple calls from various users that sp_executesql can

c. do you execute a XP_EXECRESULTSET query with the same SPID as the current connection or is a new one made?

This is all I can think of off the top of my head anyone have other comparisions to ask let them rip.





J.T. Shyman
J.T. Shyman
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 4
Make sense. Thanks for the explanation.

-- J.T.

"I may not always know what I'm talking about, and you may not either."

J.T. Shyman
J.T. Shyman
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 4

I'm not sure how the optimizer would handle them. It would be easy enough to come up with a test query and then execute it via xp_execresultset and a dynamic sql method.

The main advantage of xp_execresultset is that is allows a query to be run in the context of another database without having to switch the current connection to that context. In the article I used FILEPROPERTY which only runs against the current database. I could do this in dynamic SQL (and someone in the forums of Boris Baliner’s article on “Quickly Viewing Available Space” (http://www.sqlservercentral.com/columnists/bBaliner/quicklyviewingavailablespace.asp) did just that) However, the result was quite long and required a lot of context switching.

I don't know if xp_execresultset can take advantage of caching but I wouldn't use it in any situation where that might be necessary in any case. As I stated, this is an undocumented procedure and Microsoft dropped it in SQL 2005. Using this procedure in an application would be ill-advised. I presented it as a tool for administrators as an alternative to writing long dynamic sql-based scripts.

Yes, it executes under the same SPID. To determine this add a ,@@SPID to the end of the query in the script, as shown below:

SET @SQLCmd='SELECT ''SELECT name AS [File],

filename as [File Name],

CAST(size/128.0 as DECIMAL(10,2)) AS [Size in MB],

CAST(FILEPROPERTY(name, ''''SpaceUsed'''')/128.0 as DECIMAL(10,2)) as [Space Used],

CAST(size/128.0-(FILEPROPERTY(name, ''''SpaceUsed'''')/128.0) AS DECIMAL(10,2)) AS [Available Space] ,@@SPID

FROM SYSFILES'''



-- J.T.

"I may not always know what I'm talking about, and you may not either."

HelloFOFO
HelloFOFO
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 76

Two questions:

1: the "sid > 1"

If the "sa" create a product database,the sid in the sysdatabases table will be 'Ox01'.Then the command doesn't work well.

2:the query listed below

Declare @DBName sysname
Declare @SqlCmd Nvarchar(2000)
Set @DBName = ''

While @DBName Is not NULL
Begin
Select @DBName = Min(Name) From master..sysdatabases Where sid > 1 And Name > @DBName
Set @SqlCmd = 'SELECT ''SELECT name AS [File], filename as [File Name]
, CAST(size/128.0 as DECIMAL(10,2)) AS [Size in MB]
, CAST(FILEPROPERTY(name, ''''SpaceUsed'''')/128.0 as DECIMAL(10,2)) as [Space Used]
, CAST(size/128.0-(FILEPROPERTY(name, ''''SpaceUsed'''')/128.0) AS DECIMAL(10,2)) AS [Available Space] FROM SYSFILES'''

IF @DBName is NULL Break
Exec Master..xp_execresultset @SqlCmd,@DBName
End

It just return "Command(s) completed successfully."


katesl
katesl
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 473

Thanks for mentioning that the xp_execresultset is missing from SQL Server 2005. Yikes! Before retiring your SQL Server 2000 servers, do this--

1) Copy proc sp_execresultset from the master database of any server, and create it in the master database of the SQL Server 2005 server. (The xp_execresultset is unike other xp_ procs; it isn't compiled and reading it shows that it calls sp_execresultset.)

2) Copy proc xp_execresultset from the master database of any server, and create it in the master database of the SQL Server 2005 server, named sp_execresultset2. (Doesn't work if you name it xp_ -- and, proc can be named sp_ to be called from databases other than master without "exec master.." -- at least that is true in SQL Server 2000.)

3) Edit the script to refer to sp_execresultset2 in place of master.dbo.xp_execresultset.



_________________
"Look, those sheep have been shorn."
data analyst replies, "On the sides that we can see.."
J.T. Shyman
J.T. Shyman
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 4

Take out the sid>1 conditional from the where clause.

One of the readers, Scott, pointed out that the SID=1 means sa created the database and that this isn't a good way to limit tha query to non-system databases. That should solve the problem. You are, as you pointed out, probably excluding all of your user databases as well because they were created by 'sa'.

Declare @DBName sysname
Declare @SqlCmd Nvarchar(2000)
Set @DBName = ''

While @DBName Is not NULL
Begin
Select @DBName = Min(Name) From master..sysdatabases Where Name > @DBName
Set @SqlCmd = 'SELECT ''SELECT name AS [File], filename as [File Name]
, CAST(size/128.0 as DECIMAL(10,2)) AS [Size in MB]
, CAST(FILEPROPERTY(name, ''''SpaceUsed'''')/128.0 as DECIMAL(10,2)) as [Space Used]
, CAST(size/128.0-(FILEPROPERTY(name, ''''SpaceUsed'''')/128.0) AS DECIMAL(10,2)) AS [Available Space] FROM SYSFILES'''

IF @DBName is NULL Break
Exec Master..xp_execresultset @SqlCmd,@DBName
End



-- J.T.

"I may not always know what I'm talking about, and you may not either."

J.T. Shyman
J.T. Shyman
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 4

Good ideas. Just remember that this is still unsupported so don't call Redmond if you have problems with it.

That's what we're all here for anyway.



-- J.T.

"I may not always know what I'm talking about, and you may not either."

Robert Sterbal
Robert Sterbal
SSC-Enthusiastic
SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)SSC-Enthusiastic (109 reputation)

Group: General Forum Members
Points: 109 Visits: 1

Really neat. Thanks.

I appreciated the cut and paste script in the comments. The graphic image of the script in the article was frustrating.

-Robert


J.T. Shyman
J.T. Shyman
SSC-Addicted
SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)SSC-Addicted (406 reputation)

Group: General Forum Members
Points: 406 Visits: 4
Yeah, I noticed that. Sorry about the original.

Glad you found this useful.

-- J.T.

"I may not always know what I'm talking about, and you may not either."

HelloFOFO
HelloFOFO
SSC-Enthusiastic
SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)SSC-Enthusiastic (169 reputation)

Group: General Forum Members
Points: 169 Visits: 76

Got it ...

Thanks


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