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
alexweatherall
alexweatherall
Old Hand
Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)Old Hand (313 reputation)

Group: General Forum Members
Points: 313 Visits: 39
Warning : when using this procedure the result set it executes is run on the one of the main processor threads of SQL Server, not your connection thread. If you perform a long running query it can bring SQL Server to a stand still. Do not use on production servers - or at least with care.

It happened to me (luckily on a DEV server) and my colleagues were asking me why SQL Server had stopped responding to their queries. Because the main thread was handling mine!

You have been warned :-)



alex hatcher
alex hatcher
Valued Member
Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)Valued Member (59 reputation)

Group: General Forum Members
Points: 59 Visits: 278
Great article and very helpful.

however, I wanted to experiment with it... why is the SQL a image and not text on the page? Can't copy and paste imaged SQL.
J.T. Shyman
J.T. Shyman
Old Hand
Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)Old Hand (398 reputation)

Group: General Forum Members
Points: 398 Visits: 4

I'm afraid that's because the way I sent the article to the editors.

Here's the code in text:

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'''

--PRINT @SQLCmd

IF @DBName IS NULL BREAK

EXEC MASTER..XP_EXECRESULTSET @SQLCmd, @DBName

END

and

DECLARE @SQL NVARCHAR(4000)

DECLARE @USER SYSNAME

DECLARE @DATABASE SYSNAME

DECLARE @PERMISSION SYSNAME

SET @USER='BOB' –- Replace with name of user

SET @DATABASE='MyDB' –- Replace with database name

SET @PERMISSION='EXECUTE' -- Replace with rights. You can use a comma separated list here. For example 'SELECT,UPDATE' if you want to modify permissions on tables

SET @SQL='SELECT ''GRANT '+@PERMISSION+' ON ''+NAME+'' TO '+@USER+' ''FROM '+@DATABASE+'..SYSOBJECTS WHERE TYPE=''P'' AND NAME LIKE ''USP%'''

EXEC SP_EXECUTESQL @SQL

EXEC MASTER..XP_EXECRESULTSET @SQL, @DATABASE



-- J.T.

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

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