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: FileTable-- Insert a new record, specifying the new path_locatorINSERT INTO Documents (name, file_stream, path_locator)VALUES (@name, 0x, @path_locator);ENDELSEBEGIN-- Raise error because the specified parent folder does not existRAISERROR ('<strong>The</strong> parent name does not exist in the FileTable at the specifiedlevel.', 16, 1);ENDENDListing 11-21:A stored procedure to insert a new row in a FileTable.It is possible to create simpler values for the path_locator field, such as just appending1/ to the parent_path_locator value, but then your values are deviating from thedefault structure. Also, you have to provide some mechanism to ensure uniqueness <strong>of</strong> thepath_locator, which will likely involve a call to NEWID() anyway.In the same fashion, we can now create a second-level subfolder using Listing 11-22.<strong>The</strong> only difference from Listing 11-21 is found in the INSERT statement, where thefile_stream column has been replaced by the is_directory column.-- Find the path_locator value for the parent directoryDECLARE @parent_path_locator HIERARCHYIDSELECT @parent_path_locator = path_locatorFROM DocumentsWHERE name = 'Order Files'AND path_locator.GetLevel() = 1AND is_directory = 1-- Create a new path_locator value that places the new file-- as a child <strong>of</strong> the parent directoryDECLARE @path_locator VARCHAR(675)SET @path_locator = @parent_path_locator.ToString() +CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), NEWID()), 1, 6))) + '.' +CONVERT(VARCHAR(20), CONVERT(BIGINT, SUBSTRING(CONVERT(BINARY(16), NEWID()), 7, 6))) + '.' +419

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

Saved successfully!

Ooh no, something went wrong!