[SQLServer大对象]FileTable从文件系统迁移文件
阅读导航 从文件系统中迁移文件到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 的元数据:
<span> 1: </span><span>-- 添加一个路径定位器到 PhotoMetadata。</span>
<span> 2: </span><span>ALTER</span> <span>TABLE</span> PhotoMetadata <span>ADD</span> pathlocator hierarchyid;
<span> 3: </span>
<span> 4: </span><span>-- 获得在文件系统中图片的根路径。</span>
<span> 5: </span><span>DECLARE</span> @UNCPathRoot <span>varchar</span>(100) = <span>'\\RemoteShare\Photographs'</span>;
<span> 6: </span>
<span> 7: </span><span>-- 获得FileTable的根路径。</span>
<span> 8: </span><span>DECLARE</span> @FileTableRoot <span>varchar</span>(1000);
<span> 9: </span><span>SELECT</span> @FileTableRoot = FileTableRootPath(<span>'dbo.PhotoTable'</span>);
<span> 10: </span>
<span> 11: </span><span>-- 更新PhotoMetadata。</span>
<span> 12: </span>
<span> 13: </span><span>-- 使用 FileTable 路径代替文件系统 UNC 路径。</span>
<span> 14: </span><span>UPDATE</span> PhotoMetadata
<span> 15: </span> <span>SET</span> UNCPath = REPLACE(UNCPath, @UNCPathRoot, @FileTableRoot);
<span> 16: </span>
<span> 17: </span><span>-- 更新 FileTable 的 pathlocator 列。 </span>
<span> 18: </span><span>UPDATE</span> PhotoMetadata
<span> 19: </span> <span>SET</span> 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命名空间

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

For objects with the same name that already exist in the SQL Server database, the following steps need to be taken: Confirm the object type (table, view, stored procedure). IF NOT EXISTS can be used to skip creation if the object is empty. If the object has data, use a different name or modify the structure. Use DROP to delete existing objects (use caution, backup recommended). Check for schema changes to make sure there are no references to deleted or renamed objects.

The import steps are as follows: Copy the MDF file to SQL Server's data directory (usually C:\Program Files\Microsoft SQL Server\MSSQL\DATA). In SQL Server Management Studio (SSMS), open the database and select Attach. Click the Add button and select the MDF file. Confirm the database name and click the OK button.

When the SQL Server service fails to start, here are some steps to resolve: Check the error log to determine the root cause. Make sure the service account has permission to start the service. Check whether dependency services are running. Disable antivirus software. Repair SQL Server installation. If the repair does not work, reinstall SQL Server.

On July 29, at the roll-off ceremony of AITO Wenjie's 400,000th new car, Yu Chengdong, Huawei's Managing Director, Chairman of Terminal BG, and Chairman of Smart Car Solutions BU, attended and delivered a speech and announced that Wenjie series models will be launched this year In August, Huawei Qiankun ADS 3.0 version was launched, and it is planned to successively push upgrades from August to September. The Xiangjie S9, which will be released on August 6, will debut Huawei’s ADS3.0 intelligent driving system. With the assistance of lidar, Huawei Qiankun ADS3.0 version will greatly improve its intelligent driving capabilities, have end-to-end integrated capabilities, and adopt a new end-to-end architecture of GOD (general obstacle identification)/PDP (predictive decision-making and control) , providing the NCA function of smart driving from parking space to parking space, and upgrading CAS3.0

To view the SQL Server port number: Open SSMS and connect to the server. Find the server name in Object Explorer, right-click it and select Properties. In the Connection tab, view the TCP Port field.

If you accidentally delete a SQL Server database, you can take the following steps to recover: stop database activity; back up log files; check database logs; recovery options: restore from backup; restore from transaction log; use DBCC CHECKDB; use third-party tools. Please back up your database regularly and enable transaction logging to prevent data loss.

SQL Server database files are usually stored in the following default location: Windows: C:\Program Files\Microsoft SQL Server\MSSQL\DATALinux: /var/opt/mssql/data The database file location can be customized by modifying the database file path setting.

If the SQL Server installation fails, you can clean it up by following these steps: Uninstall SQL Server Delete registry keys Delete files and folders Restart the computer
