Recently when I came across the script for restoring a database, I got a doubt on why we have to use "FILE = 1"? Can't we restore a database without that statement!? Basically, what it is used for?
1 Answer
You are allowed to save more than one backup in a backup file (i.e. device). The FILE clause lets you access a particular backup operation when there are multiple to choose from within the .bak file.
For more information on the various options of the RESTORE command, please see the following Microsoft documentation for RESTORE Arguments.
If you look under the Backup Set Options section (within the Syntax description), you will find:
FILE ={ backup_set_file_number | @backup_set_file_number }
Identifies the backup set to be restored. For example, a backup_set_file_number of 1 indicates the first backup set on the backup medium and a backup_set_file_number of 2 indicates the second backup set. You can obtain the backup_set_file_number of a backup set by using the RESTORE HEADERONLY statement.
When not specified, the default is 1, except for RESTORE HEADERONLY in which case all backup sets in the media set are processed. For more information, see "Specifying a Backup Set," later in this topic.
Important
This FILE option is unrelated to the FILE option for specifying a database file, FILE = { logical_file_name_in_backup | @logical_file_name_in_backup_var }.
Yes, you should be able to restore without the FILE = 1 as 1 is the default anyway. And, if you only ever have a single backup-set in a backup file, then it shouldn't pose a problem.
An example to help illustrate when you would use the FILE option is the following, which is Example B from the RESTORE command documentation page (linked above). It shows doing two restores from a single backup file: the first RESTORE is the FULL backup, and the second RESTORE is the DIFFerential backup.
RESTORE DATABASE AdventureWorks2012 FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012.bak' WITH FILE = 6 NORECOVERY; RESTORE DATABASE AdventureWorks2012 FROM DISK = 'Z:\SQLServerBackups\AdventureWorks2012.bak' WITH FILE = 9 RECOVERY; - 2It's important to note for people that there is nothing magical about 6 & 9, in this example the full backup is indicated as the 6th file in the backup set - but that only pertains to this example. Same for the differential backup file, it's the 9th in the set in this example. Specifying 6 or 9 is only mentioning which file in the set to use. If you have only one file in your backup set, you must not specify a number, or specify 1 onlyKyle Burkett– Kyle Burkett2023-01-19 18:33:23 +00:00Commented Jan 19, 2023 at 18:33