阅读导航 从文件系统中迁移文件到filetable 批量加载文件到filetable 如何批量加载文件到filetable 通过博文 [sqlserver大对象]filetable初体验 ,已经可以将文件加载到数据库中,并查看和访问这些文件。 将文件加载到 filetable,可以使用工具xcopy或roboco
阅读导航
从文件系统中迁移文件到filetable
批量加载文件到filetable
如何批量加载文件到filetable
通过博文[sqlserver大对象]——filetable初体验,已经可以将文件加载到数据库中,并查看和访问这些文件。
将文件加载到 filetable,可以使用工具xcopy或robocopy,也可以自己编写脚本(如powershell)或者应用程序,复制文件到filetable中。
现在说一说文件的迁移。
从文件系统中迁移文件到filetable
迁移文件条件
文件存储在文件系统中
在 sql server 中元数据的表包含一个指向文件的指针
执行前提
要将文件迁入到 filetable,需要将每一个文件的原始unc路径用filetable的unc路径代替。
现在我们假定现有 filetable photometadata 包含图片数据,。这个表有一个varchar(512)类型的unc路径列,其中包含执行.jpg文件的实际路径。
将.jpg及其目录结构一起复制到filet的根目录下。
执行
使用代码修改 photometadata 的元数据:
1: -- 添加一个路径定位器到 photometadata。
2: alter table photometadata add pathlocator hierarchyid;
3:
4: -- 获得在文件系统中图片的根路径。
5: declare @uncpathroot varchar(100) = '\\remoteshare\photographs';
6:
7: -- 获得filetable的根路径。
8: declare @filetableroot varchar(1000);
9: select @filetableroot = filetablerootpath('dbo.phototable');
10:
11: -- 更新photometadata。
12:
13: -- 使用 filetable 路径代替文件系统 unc 路径。
14: update photometadata
15: set uncpath = replace(uncpath, @uncpathroot, @filetableroot);
16:
17: -- 更新 filetable 的 pathlocator 列。
18: update photometadata
19: set pathlocator = getpathlocator(uncpath);
批量加载文件到filetable
对于批量操作,filetable和其他表基本一样,但是有些需要注意的地方。
filetable有系统定义的约束,这些约束是为了确保文件的完整性和目录空间具有可维护性。这些约束验证数据批量加载到filetable中。由于一些大量插入操作允许忽略表约束,所以接下来的是被强制要求的。
强制约束的批量加载操作可以像在任何其他表一样在 filetable使用,具体操作如下:
bcp 带 check_constraints 子句。
bulk insert 带 check_constraints 子句。
insert into … select * from openrowset(bulk …) 不带 ignore_constraints 子句。
非强制约束的批量加载操作会失败,除非 filetable 系统定义的约束已禁用,具体操作如下:
bcp 不带 check_constraints 子句。
bulk insert 不带 check_constraints 子句。
insert into … select * from openrowset(bulk …) 带 ignore_constraints 子句。
如何批量加载文件到filetable
可以使用多种方法批量加载文件到filetable:
bcp
使用 check_constraints 子句。
禁用filetable命名空间,并且不使用 check_constraints 子句。然后重新启用filetable命名空间。
bulk insert
使用 check_constraints 子句。
禁用filetable命名空间,并且不使用 check_constraints 子句。然后重新启用filetable命名空间
insert into … select * from openrowset(bulk …)
使用 check_constraints 子句。
禁用filetable命名空间,并且不使用 check_constraints 子句。然后重新启用filetable命名空间