Determine MDF and LDF file match - sql-server

Is there a way to determine what MDF goes with what LDF file for SQL Server? We had a server crash and pull these files off and were only named with a random integer for the file name. So now we need to guess which MDF and LDF go together to get them up but what is the best way to do that?

You would find your current database's MDF and LDF with this:
sp_helpdb 'YourDBName'
Or you could see everything you have in your instance:
SELECT name, physical_name AS current_file_location FROM sys.master_files
In case of offline scenario try this:
SELECT DB.name, MF.name, MF.type_desc, MF.physical_name
FROM sys.databases DB
INNER JOIN sys.master_files MF ON db.database_id = mf.database_id
WHERE DB.state = 6
When DB.State= 6 means offline state.

Related

Find Root disk Path with SQL query

How do I find Server Root disk name with MS SQL query?
For Ex: c:\
Thanks!
SELECT DISTINCT DB_NAME(dovs.database_id) DBName,
mf.physical_name PhysicalFileLocation,
dovs.logical_volume_name AS LogicalName,
dovs.volume_mount_point AS Drive,
CONVERT(INT,dovs.available_bytes/1048576.0) AS FreeSpaceInMB
FROM sys.master_files mf
CROSS APPLY sys.dm_os_volume_stats(mf.database_id, mf.FILE_ID) dovs
ORDER BY FreeSpaceInMB ASC
GO
It will give you the DBname, Physical file location, Logicaldiskname,Drive,FreeSpace. I use this query.

Move SQL Database Files MDF & LDF to New Location

Environment:
SQL Server 2008 R2
Database not receiving active transactions.
In a production environment I need to move the MDF & LDF files to new drives. Since I have a Window of time to stop active transactions, I thought I could just take a backup of the database and then restore it while configuring the file groups to new location.
I figured this is much better than detaching and reattaching the database with new file name.
Since I am a novice, wanted to check with the experts here. Any suggestion/advise much appreciated.
Here's a simple example. It assumes your database has a single .mdf (data) file and a single .ldf (log) file. I will use the [model] database as an example.
--First, make note of the current location of the db files.
--Copy and paste the physical_names somewhere. Trust me, if you forget
--where the files were originally, this will save you some heartache.
SELECT d.name, f.name, f.physical_name
FROM master.sys.master_files f
JOIN master.sys.databases d
ON d.database_id = f.database_id
WHERE d.name = 'model' --Replace with the name of your db.
--Now set the new file paths.
--You can run the ALTER DATABASE statements while the db is online.
--Run once for the mdf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modeldev', --this is the "logical" file name.
FILENAME = 'D:\SqlData\model.mdf' --Replace with the new path\filename.
)
--Run once for the ldf/data file.
ALTER DATABASE [model] --Replace with the name of your db.
MODIFY FILE
(
NAME = 'modellog',
FILENAME = 'D:\SqlData\modellog.ldf' --Replace with the new path\filename.
)
--When business rules allow, take the db OFFLINE.
ALTER DATABASE [model] --Replace with the name of your db.
SET OFFLINE
--Move the physical db files to the new location on disk.
--Bring the db back ONLINE to complete the task.
ALTER DATABASE [model] --Replace with the name of your db.
SET ONLINE

MsSql whate drive is my file group stored on

I have moved some data from one file group to another, however when I check the drive with master.sys.xp_fixeddrives the drive does not say that it has been altered in size.
I want to know how to check what drive the filegroup is physically stored on.
Thanks
Filegroups are made up of a collection of files, all of which could be on separate drives, partitions, mount points, smb shares, etc. The filegroup doesn't exist anywhere but logically, you want to know where the file inside of the filegroup reside.
Below can be run to give you the information you are looking for in terms of where the files are in that filegroup. Optionally you can omit the where clause to see all of the files and filegroups, where the files reside. It will need to be run in the context of the database in question.
SELECT f.name AS [FGName], df.name AS [FileName], LEFT(df.physical_name, 1) AS [Drive]
, df.physical_name AS [Full_Path]
FROM sys.database_files df
inner join sys.filegroups f
on df.data_space_id = f.data_space_id
WHERE f.name = 'MyFilegroupNameHere'

Where is database .bak file saved from SQL Server Management Studio?

I was trying to create a backup for my SQL Server Database using SQL Server Management Studio 2008 Express. I have created the backup but it is getting saved at some path which I am not able to find. I am saving it on my local HD and I checked in Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>DATA> but its not there.
What's the default save path for this DB .bak?
Should be in
Program Files>Microsoft SQL Server>MSSQL 1.0>MSSQL>BACKUP>
In my case it is
C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup
If you use the gui or T-SQL you can specify where you want it
T-SQL example
BACKUP DATABASE [YourDB] TO DISK = N'SomePath\YourDB.bak'
WITH NOFORMAT, NOINIT, NAME = N'YourDB Full Database Backup',
SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
With T-SQL you can also get the location of the backup, see here Getting the physical device name and backup time for a SQL Server database
SELECT physical_device_name,
backup_start_date,
backup_finish_date,
backup_size/1024.0 AS BackupSizeKB
FROM msdb.dbo.backupset b
JOIN msdb.dbo.backupmediafamily m ON b.media_set_id = m.media_set_id
WHERE database_name = 'YourDB'
ORDER BY backup_finish_date DESC
If the backup wasn't created in the default location, you can use this T-SQL (run this in SSMS) to find the file path for the most recent backup for all DBs on your SQL Server instance:
SELECT DatabaseName = x.database_name,
LastBackupFileName = x.physical_device_name,
LastBackupDatetime = x.backup_start_date
FROM ( SELECT bs.database_name,
bs.backup_start_date,
bmf.physical_device_name,
Ordinal = ROW_NUMBER() OVER( PARTITION BY bs.database_name ORDER BY bs.backup_start_date DESC )
FROM msdb.dbo.backupmediafamily bmf
JOIN msdb.dbo.backupmediaset bms ON bmf.media_set_id = bms.media_set_id
JOIN msdb.dbo.backupset bs ON bms.media_set_id = bs.media_set_id
WHERE bs.[type] = 'D'
AND bs.is_copy_only = 0 ) x
WHERE x.Ordinal = 1
ORDER BY DatabaseName;
As said by Faiyaz, to get default backup location for the instance, you cannot get it into msdb, but you have to look into Registry. You can get it in T-SQL in using xp_instance_regread stored procedure like this:
EXEC master.dbo.xp_instance_regread
N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\\Microsoft SQL Server\MSSQL12.MSSQLSERVER\MSSQLServer',N'BackupDirectory'
The double backslash (\\) is because the spaces into that key name part (Microsoft SQL Server).
The "MSSQL12.MSSQLSERVER" part is for default instance name for SQL 2014. You have to adapt to put your own instance name (look into Registry).
Set registry item for your server instance. For example:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL.2\MSSQLServer\BackupDirectory
Use the script below, and switch the DatabaseName with then name of the database that you've backed up. On the column physical_device_name, you'll have the full path of your backed-up database:
select a.backup_set_id, a.server_name, a.database_name, a.name, a.user_name, a.position, a.software_major_version, a.backup_start_date, backup_finish_date, a.backup_size, a.recovery_model, b.physical_device_name
from msdb.dbo.backupset a join msdb.dbo.backupmediafamily b
on a.media_set_id = b.media_set_id
where a.database_name = 'DatabaseName'
order by a.backup_finish_date desc
I dont think default backup location is stored within the SQL server itself.
The settings are stored in Registry. Look for "BackupDirectory" key and you'll find the default backup.
The "msdb.dbo.backupset" table consists of list of backups taken, if no backup is taken for a database, it won't show you anything
...\Program Files\Microsoft SQL Server\MSSQL 1.0\MSSQL\Backup
have you tried:
C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\Backup
Script to get all backups in the last week can be found at:
http://wraithnath.blogspot.com/2010/12/how-to-find-all-database-backups-in.html
I have plenty more backup SQL scripts there also at
http://wraithnath.blogspot.com/search/label/SQL
So eventually on SQL Server 2019 you can just call built in SERVERPROPERTY function to query the default backup folder:
select serverproperty('InstanceDefaultBackupPath')
You may want to take a look here, this tool saves a BAK file from a remote SQL Server to your local harddrive: FIDA BAK to local

Physical location of FILESTREAM data

How could I know the physical location (so I can see it in Windows Explorer) path of a FILESTREAM data that I've just inserted into DB?
There is one option for this: method PhysicalPathName(). If you are on SQL Server 2012 or upper now, this code will work for you:
SELECT stream.PhysicalPathName() AS 'Path' FROM Media
OPTION (QUERYTRACEON 5556)
For SQL Server 2008/2008 R2 you will need to enable trace flag 5556 for the whole instance:
DBCC TRACEON (5556, -1)
GO
or for the particular connection in which you are calling PhysicalPathName() method:
DBCC TRACEON (5556, -1)
GO
I know this is an older post but as it still comes up high in the Google search rankings I thought I'd post an answer. Certainly in later versions of SQL (I've not tried this on 2008) you can run the following query:
SELECT t.name AS 'table',
c.name AS 'column',
fg.name AS 'filegroup_name',
dbf.type_desc AS 'type_description',
dbf.physical_name AS 'physical_location'
FROM sys.filegroups fg
INNER JOIN sys.database_files dbf
ON fg.data_space_id = dbf.data_space_id
INNER JOIN sys.tables t
ON fg.data_space_id = t.filestream_data_space_id
INNER JOIN sys.columns c
ON t.object_id = c.object_id
AND c.is_filestream = 1
Source
As Pawel has mentioned, it is not a good idea to access the FILESTREAM files using Windows Explorer. If you are still determined to go ahead and explore this, the following tip might help.
The FILESTREAM file names are actually the log-sequence number from the database transaction log at the time the files were created. Paul Randal has explained it in this post. So One option is to find out the log sequence number and look for a file named after that in the file stream data container.
First you need to understand that the FileStream is being stored on the server hosting your SQL Server 2008 database. If you have a DBA, ask them where they created it the FileStream at. Of course, you'll then need rights to the server to navigate it to see the directories. You won't be able to manipulate the files in any way either, but you will be able to see them. Most DBA's won't be keen on letting you know where the FileStream is located at.
However, you can get at the path by a few other means. One way that comes to mind is by selecting upon the PathName() of the FileStream field. Assume that the FileStream enabled field is ReportData, and the table in which it resides is TblReports. The following t-sql syntax will yield an UNC to the location:
select top 1 ReportData.PathName(0)
from dbo.datReport
I believe you can also get at the path by other means through enterprise manager, but I forget how to at the moment.
--filestream file path
SELECT col.PathName() AS path FROM tbl

Resources