Problem in using USE dbname

Hi,

I am trying to monitor log and data space usage through dbcc sqlperf(logspace) and dbcc showfilestats commands on a server.

what I want to do is to log all these for all the database using a cursor all work fine except

User @db

go

inside the cursor body.

Does anyone know any alternative to use this to what I want to get

Thanks

Ap

[391 byte] By [Adminanup] at [2007-12-25]
# 1
?

Instead of delimiting using GO, why not use a

semi-colon (or don't delimit at all)?


--
Adam Machanic
Pro SQL Server 2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

< href="mailto:Adminanup@discussions.microsoft.com">Adminanup@discussions..microsoft.com>

wrote in message href="news_3A6448689d-70c0-438a-b3fc-3757d168048f_40discussions.microsoft.com">news:6448689d-70c0-438a-b3fc-3757d168048f@discussions.microsoft.com...

Hi,

I am trying to monitor log and data space usage through dbcc

sqlperf(logspace) and dbcc showfilestats commands on a server.

what I want to do is to log all these for all the database using a cursor

all work fine except

User @db

go

inside the cursor body.

Does anyone know any alternative to use this to what I want to get

Thanks

Ap

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 2

Sorry was not clear earlier I want to do something like this

DECLARE AllDatabases CURSOR FOR

-- Get the names of all databases
SELECT name FROM master..sysdatabases

-- Open the cursor that holds the names of all databases on the server
OPEN AllDatabases

-- Declare variable for holding the name of the current database being processed
DECLARE @DB NVARCHAR(128)
DECLARE @EXECUTE NVARCHAR(128)

-- Get the name of the first database
FETCH NEXT FROM AllDatabases INTO @DB

-- Process all databases until there are no more
WHILE (@@FETCH_STATUS = 0)

BEGIN
set @execute='[USE '+@DB+']' ;
Execute @execute

FETCH NEXT FROM AllDatabases INTO @DB
END --(@@FETCH_STATUS = 0)

--
--End "Generate DBCC SHOWFILESTATS Commands" Section
--


-- close and deallocate cursor to hold name of all databases on server
CLOSE AllDatabases
DEALLOCATE AllDatabases

Adminanup at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 3
?

Well your code had a few issues. Here's a

modified version that actually works (I removed brackets where they shouldn't

have been, added parens for the EXECUTE, closed and deallocated the cursor

at the end (make sure to always do that), and added a PRINT statement so that

the cursor actually outputs something):

DECLARE AllDatabases CURSOR FOR

-- Get the names of all databases
SELECT name

FROM master..sysdatabases

-- Open the cursor that holds the names of all

databases on the server
OPEN AllDatabases

-- Declare variable for holding the name of the

current database being processed
DECLARE @DB NVARCHAR(128)
DECLARE

@EXECUTE NVARCHAR(128)

-- Get the name of the first database
FETCH

NEXT FROM AllDatabases INTO @DB

-- Process all databases until there are no more


WHILE (@@FETCH_STATUS = 0)

BEGIN
set @execute='USE href="mailto:'+@DB+'">'+@DB+'; print ''' + @DB +

''''
Execute(@execute)


FETCH NEXT FROM AllDatabases INTO @DB
END --(@@FETCH_STATUS = 0)

CLOSE AllDatabases
DEALLOCATE AllDatabases


--
Adam Machanic
Pro SQL Server

2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

< href="mailto:Adminanup@discussions.microsoft.com">Adminanup@discussions..microsoft.com>

wrote in message href="news_3A2a4108ca-39fb-4028-84e3-d7adf79f5d85_40discussions.microsoft.com">news:2a4108ca-39fb-4028-84e3-d7adf79f5d85@discussions.microsoft.com...

Sorry was not clear earlier I want to do something like this

DECLARE AllDatabases CURSOR FOR

-- Get the names of all databases


SELECT name FROM master..sysdatabases

-- Open the cursor that

holds the names of all databases on the server
OPEN AllDatabases



-- Declare variable for holding the name of the current database being

processed
DECLARE @DB NVARCHAR(128)
DECLARE @EXECUTE

NVARCHAR(128)

-- Get the name of the first database
FETCH NEXT FROM

AllDatabases INTO @DB

-- Process all databases until there are no more


WHILE (@@FETCH_STATUS = 0)

BEGIN
set @execute='[USE href="mailto:'+@DB+']'">'+@DB+']' ;
Execute @execute

FETCH NEXT FROM AllDatabases INTO @DB
END --(@@FETCH_STATUS = 0)



--
--End "Generate DBCC SHOWFILESTATS Commands" Section
--





-- close and deallocate cursor to hold name of all databases

on server
CLOSE AllDatabases
DEALLOCATE AllDatabases

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 4

I tried thsi it is printing but still not all the database are being looped it is just using the current active one so I need to do the USE @db to loop through all the databases and collect the dbcc sqlperf(logpsace) and filestats into a table.

Any ideas where I am missing

Adminanup at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 5
?

Are you running the exact code I posted? When

I run it on my end, every database is printed.

Here's a modified version that prints the database

name after switching to it, just to make sure (and it still works -- tested in

both SQL Server 2000 and 2005):

DECLARE AllDatabases CURSOR FOR

-- Get the names of all databases
SELECT name

FROM master..sysdatabases

-- Open the cursor that holds the names of all

databases on the server
OPEN AllDatabases

-- Declare variable for holding the name of the

current database being processed
DECLARE @DB NVARCHAR(128)
DECLARE

@EXECUTE NVARCHAR(128)

-- Get the name of the first database
FETCH

NEXT FROM AllDatabases INTO @DB

-- Process all databases until there are no more


WHILE (@@FETCH_STATUS = 0)

BEGIN
set @execute='USE href="mailto:'+@DB+'">'+@DB+'; print

db_name()'
Execute(@execute)


FETCH NEXT FROM AllDatabases INTO @DB
END --(@@FETCH_STATUS = 0)

CLOSE AllDatabases
DEALLOCATE AllDatabases


--
Adam Machanic
Pro SQL Server

2005, available now
href="http://www.apress.com/book/bookDisplay.html@bID=457">http://www..apress.com/book/bookDisplay.html?bID=457
--

style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">

< href="mailto:Adminanup@discussions.microsoft.com">Adminanup@discussions..microsoft.com>

wrote in message href="news_3A8748d567-3a80-4cb1-b944-48a2bfb11285_40discussions.microsoft.com">news:8748d567-3a80-4cb1-b944-48a2bfb11285@discussions.microsoft.com...

I tried thsi it is printing but still not all the database are being looped

it is just using the current active one so I need to do the USE @db to loop

through all the databases and collect the dbcc sqlperf(logpsace) and filestats

into a table.

Any ideas where I am missing

MVPUser at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...
# 6

The problem here is that the USE @DB statement is only valid for the EXECUTE statement. As soon as the EXECUTE statement completes, the current database is back to the original.

In order to accomplish what you want, there are 2 ways to do this:


a) Create a @strSQL variable string in the cursor that will be a concatenation of the statements that will do everything in one shot (switch database contexts, run DBCC showfilestats, saving/logging data, etc.):

DECLARE @strSQL nvarchar(1000)

SET @strSQL = 'USE [' + @dbName + '] INSERT INTO MyTable EXECUTE (''DBCC SHOWFILESTATS'')'

EXECUTE (@strSQL)

b) Create (and call in the cursor) a stored procedure that will do all of this for you by passing in the @DBName parameter:

CREATE PROCEDURE PRC_Test
@dbName sysname
AS
BEGIN

DECLARE @strSQL nvarchar(1000)

SET @strSQL = 'USE [' + @dbName + '] INSERT INTO MyTable EXECUTE (''DBCC SHOWFILESTATS'')'

EXECUTE (@strSQL)

END
Go

Also, you don't need the cursor for the sqlperf(logspace) command as the result set will include all databases.

Hope that helps.


Mike

Lyam1971 at 2007-9-3 > top of Msdn Tech,SQL Server,SQL Server Database Engine...

SQL Server

Site Classified