17.07.2015 Views

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

The Art of SQL Server FILESTREAM - Red Gate Software

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11: FileTableUSE NorthPoleGOCREATE TABLE Hierarchy(path_locator HIERARCHYID NOT NULLPRIMARY KEY CLUSTERED ,parent_path_locator AS ( CASE WHEN path_locator.GetLevel() = 1THEN NULLELSE path_locator.GetAncestor(1)END )PERSISTEDFOREIGN KEY REFERENCES Hierarchy ( path_locator ) ,name NVARCHAR(255) NOT NULL)Listing 11-1:Create a sample hierarchy in <strong>SQL</strong> <strong>Server</strong>.<strong>The</strong> script creates a new table containing three fields with names also found inFileTables. <strong>The</strong> path_locator is the primary key, and the parent_path_locatoris a computed column. In addition, the parent_path_locator is also a foreign keywhich references the primary key <strong>of</strong> the same table. This is to ensure we cannot add achild for a non-existing parent. <strong>The</strong> name is the would-be name <strong>of</strong> the file or folderstored in the FileTable.Listing 11-2 demonstrates how a new row can be added to the Hierarchy table. It is upto the application to create the HIERARCHYID values. A HIERARCHYID can be writtenas a string representation, whereby each level in the hierarchy is separated using '/'.A HIERARCHYID value always starts and ends with a forward slash.INSERT INTO Hierarchy( path_locator, name )VALUES ( '/1/', '1.txt' )Listing 11-2:Adding a row to the hierarchy.393

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!