partial backup/restore
Listing 1: Code to Create a Database with Multiple File Groups
GO
ALTER DATABASE ExampleDB ADD FILEGROUP TestFileGroup1;
ALTER DATABASE ExampleDB ADD FILEGROUP TestFileGroup2;
GO
ALTER DATABASE ExampleDB ADD FILE (
NAME = TestFile1,
FILENAME = 'C:\SQLskills\TestFile1.ndf',
SIZE = 5MB)
TO FILEGROUP TestFileGroup1;
ALTER DATABASE ExampleDB ADD FILE (
NAME = TestFile2,
FILENAME = 'C:\SQLskills\TestFile2.ndf',
SIZE = 5MB)
TO FILEGROUP TestFileGroup2;
GO
CREATE TABLE ExampleDB..t1 (c1 INT) ON TestFileGroup1;
CREATE TABLE ExampleDB..t2 (c1 INT) ON TestFileGroup2;
GO
INSERT INTO ExampleDB..t1 VALUES (1);
INSERT INTO ExampleDB..t2 VALUES (2);
GO
BACKUP DATABASE ExampleDB TO DISK = 'c:\SQLskills\ExampleDB.bck';
GO
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
FILEGROUP = 'primary'
FROM DISK = 'c:\SQLskills\ExampleDB.bck'
WITH PARTIAL, NORECOVERY;
GO
RESTORE DATABASE ExampleDB
FILEGROUP = 'TestFileGroup1'
FROM DISK = 'c:\SQLskills\ExampleDB.bck'
WITH NORECOVERY;
GO
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.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现