I’ve been playing a little more with FileTables lately, and I thought I’d share some of what I’ve seen when applying my own constraints. I’m testing with a SQL 2014 Developer Edition instance, but I can recreate everything here in 2012 editions too. When you make a FileTable SQL handles the creation of all objects, including constraints.You can’t drop or modify these, but you can add your own. In this script I create a constraint on a FileTable to limit the type of files I can insert to just .txt files. ALTER TABLE dbo.LimitType WITH CHECK ADD CONSTRAINT CK_LimitType_FileName CHECK (file_type='txt'); GO Now that I have my constraint let’s test it out. Inserting a blank .txt file works, as expected… INSERT INTO dbo.LimitType(name, file_stream) SELECT 'InsertedTextFile.txt', 0x …but when we try to add a blank image file we’re blocked by our constraint… INSERT INTO dbo.LimitType(name, file_stream) SELECT 'InsertedImageFile.jpeg', 0x Msg 547, Level 16, State 0, Line 5 The INSERT statement conflicted with the CHECK constraint “CK_LimitType_FileName”. The conflict occurred in database “DemoDB”, table “dbo.LimitType”, column ‘file_type’. The statement has been terminated. …and if you try to copy it into the shared folder you’ll get a different message. That’s not a problem, it’s what we wanted. However, there is a problem if you try to limit the size of the files you store in your FileTable. Creating a constraint on the cached_file_size doesn’t work. You can still insert files greater than the limit you set in your constraint. To test I’m going to add a check constraint to another FileTable that is supposed to limit the file size to 1 MB. ALTER TABLE dbo.LimitSize WITH CHECK ADD CONSTRAINT CK_LimitSize_cached_file_size CHECK (cached_file_size = 1000); GO I’ve copied the .iso file that contains the install bits for SQL 2014 Developer Edition, along with a few small txt files. This is what the shared folder looks like. Below, on the left, is what the .iso file properties should look like. On the right is what the properties look like from the FileTable copy. If I queried the tables I’d see the cached_file_size as 0 there as well. There is already a Connect item for this, created when SQL 2012 came out. But it was closed and marked as won’t be fixed. I can verify that this is still happening in the RTM version of SQL 2014, which is where I’ve done my tests.
↧