SQL script fails | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

SQL script fails

Hi All,<br /><br />I have modified Mohamed script for getting the total DB size to work on SQL2005, it works fine on test server but fails on live server.<br /><br />Individual DBCC and system streod procedure used in SQL script executes fine without any errors, seems some setting is preventing ..any suggestions would be great ?<br /><br /><br />For now planning to remove "set nocount on" since that requires permssions as PUBLIC role. <br /><br />Error:<br /><br />line 80 : FREE not declared.<br /><br /><br /><br /><br />SCRIPT IS LISTED BELOW:<br /><br />/*This query is used to check the database space used currently <br /> */<br /><br /><br />set nocount on <br /><br />declare @cmd varchar(500) <br />declare @db varchar(12<img src=’/community/emoticons/emotion-11.gif’ alt=’8)’ /> <br /><br />/*Create #results tables to store results, usually the temp table<br />starts with # sign */<br /><br />create table #results(FileType varchar(4) NOT NULL, <br />[Name] sysname NOT NULL, <br />Total numeric(9,2) NOT NULL, <br />Used numeric(9,2) NOT NULL, <br />[Free] numeric(9,2) NOT NULL, <br />dbname sysname NULL) <br /><br />/* Create #data table to store the total extents and used extents <br /> in temporarily<br />*/<br /><br />create table #data(Fileid int NOT NULL, <br />[FileGroup] int NOT NULL, <br />TotalExtents int NOT NULL, <br />UsedExtents int NOT NULL, <br />[Name] sysname NOT NULL, <br />[FileName] varchar(300) NOT NULL) <br />/* Create #log table to store the log size<br /> in temporarily<br />*/<br />create table #log(dbname sysname NOT NULL, <br />LogSize numeric(15,7) NOT NULL, <br />LogUsed numeric(9,5) NOT NULL, <br />Status int NOT NULL) <br /><br /><br /><br />begin <br /><br />declare dcur cursor local fast_forward <br />for select name from sysdatabases <br /><br />open dcur <br />fetch next from dcur into @db <br />while @@fetch_status=0 <br /><br /> begin <br /> set @cmd = ‘use ‘ +'[‘[email protected]+’]’ + ‘ DBCC showfilestats’ <br /> insert #data <br /> exec(@cmd) <br /> insert #results(FileType,[Name],Total,Used,[Free],dbname) <br /> select ‘Data’, <br /> left(right([FileName],charindex(”,reverse([FileName]))-1), <br /> charindex(‘.’,right([FileName], <br /> charindex(”,reverse([FileName]))-1))-1), <br /> CAST(((TotalExtents*64)/1024.00) as numeric(9,2)), <br /> CAST(((UsedExtents*64)/1024.00) as numeric(9,2)), <br />(CAST(((TotalExtents*64)/1024.00) as numeric(9,2)) <br /> -CAST(((UsedExtents*64)/1024.00) as numeric(9,2))), <br /> @db <br />from #data <br />delete #data <br />fetch next from dcur into @db <br />end <br />close dcur <br />deallocate dcur <br /><br />/* Get log file(s) size */ <br />insert #log <br />exec(‘dbcc sqlperf(logspace)’) <br />insert #results(FileType,[Name],Total,Used,[Free],dbname) <br />select ‘Log’,dbname+’_log’,LogSize, <br />((LogUsed/100)*LogSize),LogSize-((LogUsed/100)*LogSize), <br />dbname <br />from #log <br />end <br /><br />select @@servername as servername, dbname, sum(total) Total, sum(used) Used, sum(free) Free<br />from #results<br />group by dbname<br />order by Total desc<br />drop table #results, #data, #log<br /><br /><br />
The script looks ok to me….and I have executed the query on SQL 2005 database. It executed w/o error. Regards, Chetan Best Regards, Chetan
"Calm seas can never make skillful sailors".
Are you running it on a case-sensitive server? You may need to change the case in line 80 if it is erroring on ‘free’, the column has a different case earlier on in the table create Line 80 maybe should be select @@servername as servername, dbname, sum(Total) Total, sum(Used) Used, sum(Free) Free
for me it executes without errors too (unless you run it on databases which are offline)
It is working fine for me too in 2000 and 2005…
I don’t think if any of the database offline on the server should give this DECLARE error… Replace "sum(Free) Free" with "sum(Free) [Free]" and see if you get the same error…
MohammedU.
Moderator
SQL-Server-Performance.com All postings are provided “AS IS” with no warranties for accuracy.

Thx guys will check it out. Cheers
Satish
]]>