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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 2: Getting Started with <strong>FILESTREAM</strong><strong>The</strong>re may be times when you are required to check the table schema programmatically.You can query the system metadata tables to see if a table has all the required characteristicsto have a <strong>FILESTREAM</strong> column. Querying is_rowguidcol in sys.columns willtell you whether the column is marked as ROWGUIDCOL. Similarly, the is_nullablecolumn can be queried to see if the column allows NULL values. Additional information,such as whether the column has a unique constraint or single column primarykey, can be retrieved from sys.key_constraints, sys.indexes and sys.index_columns. Listing 2-10 shows the T-<strong>SQL</strong> code to find out whether the Items table has aROWGUIDCOL with the required attributes to have <strong>FILESTREAM</strong> columns.IF EXISTS ( SELECT *FROM sys.key_constraints scINNER JOIN sys.indexes siON sc.unique_index_id = si.index_idAND si.object_id = OBJECT_ID('items')INNER JOIN ( SELECT * ,COUNT(*) OVER ( PARTITION BY index_id,object_id ) AS ColCount/* number <strong>of</strong> columns in the index */FROM sys.index_columns) ic ON ic.index_id = si.index_idAND ic.object_id = si.object_idINNER JOIN sys.columns cON c.column_id = ic.column_idAND c.object_id = ic.object_idAND c.is_rowguidcol = 1 -- ROWGUIDCOLAND c.is_nullable = 0 -- NOT NULLWHERE is_unique_constraint = 1 -- UNIQUE constraintOR ( is_primary_key = 1AND ColCount = 1) -- SINGLE COLUMN Primary Key)BEGINPRINT 'Table can have <strong>FILESTREAM</strong> columns'ENDELSEBEGINPRINT 'Table is not ready to have <strong>FILESTREAM</strong> columns'ENDListing 2-10:Checking whether a table is <strong>FILESTREAM</strong> ready.64

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

Saved successfully!

Ooh no, something went wrong!