Where Are My Files?

Where Are My Files?

QL Server version 7 is an exciting new product. And although it is called “version 7,” so much of it has changed that I consider it a new product rather than an upgrade from version 6.5.

One of the many improvements is how databases are stored. In version 6.5, databases were created on devices. Although for most purposes these devices were simply files, you first had to create the devices before creating a database. In version 7, this artificial abstraction from the file system has been eliminated. Databases are created directly on operating system files.

This architecture allows databases to grow automatically. In version 6.5, you had to allocate space manually. If your DBA didn’t watch carefully, a database could run out of space even though the disk had room. However, files always grow automatically, without intervention, so in version 7, databases can also expand as needed without the DBA doing anything.

Where is the information about the files used for databases stored? In version 6.5, a table in master called sysdevices contained a row for each device used by SQL Server. In version 7, each database contains a table called sysfiles that stores a row for each file used by the database. So how does one obtain a list of all files associated with SQL Server?

Here are the steps:

  1. Create a cursor on the names of the databases.
  2. For each row, use dynamic SQL to retrieve the information from the sysfiles of that database and place it in a temporary table.
  3. Interpret the results and display for output.

Feel free to add to the following code or suggest improvements. If any of you are already using version 7, please let me know what you think of it.

set nocount ongoDECLARE dbcursor CURSORFOR select  name from master..sysdatabasesFOR READ ONLY declare @dbname varchar(50), @sqlstmt varchar(200)create table #filelist(	dbname varchar(128),	filename varchar(128),	physname varchar(260),	size int,	maxsize int,	growth int,	status int)		open dbcursorfetch next from dbcursor into @dbnamewhile @@fetch_status = 0begin	set @sqlstmt = 'insert into #filelist (dbname,filename,physname, size,maxsize,growth,status) ' +	'select ' + "'" + @dbname + "'" + ',name,filename,size,maxsize,growth,status from ' + @dbname + '..sysfiles'--	print @sqlstmt	exec (@sqlstmt)	fetch next from dbcursor into @dbname	endclose dbcursordeallocate dbcursorselect dbname,filename,physname, cast(size / 128.0 as decimal(6,2))  as 'Size (MB)',case maxsize	when -1 		then 'NOLIMIT'	else		cast(cast(maxsize  / 128.0 as decimal(6,2)) as varchar(20))end as 'Max Size (MB)',case  	when status & 0x100000 >  0		then cast (growth as char(5)) + '%'	else		cast (cast(growth / 128.0 as decimal(6,2))as varchar(20)) + ' MB'end as   'Growth Factor'from #filelistorder by dbname,filenamegodrop table #filelist	
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as