Listing 1: Code to Create a Database with Multiple File Groups
Now on a different system you can restore just the primary file group and the first file group. The key is to restore the primary file group first using WITH PARTIAL to let SQL Server know you aren’t restoring the entire database, as shown in Listing 2.
Listing 2: Restoring the Primary File Group Using WITH PARTIAL
Next, you can restore any differential and/or transaction log backups to get the restored file groups to the desired point in time, and then bring the database online using the following code:
RESTORE DATABASE ExampleDB WITH RECOVERY;
If I query the database using the code in Listing 3, it will show me what’s online.
Listing 3: Code Used to Determine Which File Groups Are Online
SELECT [name], [state_desc] FROM ExampleDB.sys.database_files;
GO
name state_desc
---------------- -----------------
ExampleDB ONLINE
ExampleDB_log ONLINE
TestFile1 ONLINE
TestFile2 RECOVERY_PENDING
Now, of course, I can only use the portions of the database that I’ve restored. If I try to access anything in an offline file group, I’ll get an error similar to the following:
SELECT * FROM ExampleDB..t2;
GO
Msg 8653, Level 16, State 1, Line 1
The query processor is unable to produce a plan for the table or view 't2' because the table resides in a filegroup which is not online.