Microsoft SQL Server 2012 Step by Step – A Review

The latest tech book I’ve been reading is Microsoft SQL Server 2012 Step by Step, written by Patrick LeBlanc and published by Microsoft Press. This is an excellent book for anyone new to SQL. If your familiar with older version of SQL you may still find information about some of the new features, but you should remember that it’s written with beginners in mind.

The book is split into nine sections that cover everything from installing and configuring SQL 2012 to high availability and disaster recovery. In between you’ll read about the basics of database design and T-SQL. There’s a very good chapter covering backup and recover of databases. 

Like most other Step by Step books I’ve read this book teaches by example. First there’s a discussion about the  topic of each chapter, followed by an example that walks you through the concepts being taught. What I especially like is that you will see how to perform tasks like creating a table through the Management Studio but you’ll also learn how to perform the same tasks using T-SQL.

This is a great book for anyone who wants to get up to speed with SQL 2012 quickly.

SQL Server Data Tools – A Last Look (for now)

This post will wrap up my introduction to SQL Server Data Tools (SSDT). In my first post I wrote about my using SSDT to set up a developer sandbox of a SQL database. My second post covered using SSDT to build a prototype database using LocalDB instead of a SQL instance. I added a third post to clear up some facts I didn’t cover correctly, about LocalDB. Today I’m going to show what the LocalDB looks like, as well as a little more on those .dacpac files created when you take a snapshot of your project.

When you create your first SQL database project SSDT will start SQL but not as a service. You can verify this by opening Task Manager and looking at the running processes. You should see a process sqlservr.exe running under your name. If you’re also running a SQL instance you’ll see a second sqlservr.exe process running under whatever the startup account is. Notice that the location of the executable is C:\Program Files\Microsoft SQL Server\110\LocalDB\Binn. clip_image002

Sqlservr.exe will also create a new set of system databases for your new project. You’ll find these at C:\Users\your name\AppData\Local\Microsoft\Microsoft SQL Server LocalDB\Instances. You’ll see an instance for every SQL Database project you create.  Confirm this by querying sysfiles. Open that location and you’ll also see the error logs and blackbox traces you’d normally see in SQL instances. Open either to see how your instance is behaving.

image

Open the location where you saved your SSDT project. You should see a folder called  Sandbox. Inside you’ll see a .mdf and .ldf file for your database. You can detach it and reattach it, or backup and restore it, to another server when you’re ready to move it.

There’s a few things about the snapshots I’d like to mention. There’s other ways to use then instead of just with Schema Compare. You can pass it the latest .dacpac file from your project to another developer who can create a new project based on your snapshot. You can open .dacpacs created in one version of Visual Studio in another; 2012 to 2010 for example. And you can also open it to see what it contains.

A .dacpac file is like a compressed folder. If you open it you can extract the files to a new directory. Inside you’ll see some .xml files with configuration information; database settings for example. You’ll also see one model.sql file that has a create object for all objects in your database. There’s no INSERT statements, though.

image

Finally, the SSDT team announced an update last Friday. There’s some new stuff there, like opening VS 2012 and only creating one instance on LocalDB instead of one for each project. Check it out on their blog. And Google/Bing for SSDT, there’s a lot of good information out there.

SQL Server Data Tools – A Correction

I’m still putting my final post on SSDT together but I need to clarify a few things that I wrote in my Introduction first. I stated that you couldn’t download SQL LocalDB, that it came with SSDT, which you could download. That is not true. With the release of SQL 2012 Microsoft has added the LocalDB as an Express Edition. The documentation says that it is a lightweight, fast, zero configuration database that runs in user-mode. It was specifically designed for developers. I’ll test LocalDB but I have to build an environment for it first.

Also there are other ways of installing SSDT. I mentioned that you can download it from Microsoft, and I said how it’s a feature when installing SQL Server 2012. I just realized it’s also an option when installing Visual Studio 2012.

Finally, the SSDT Team announced that they are releasing an update for both Visual Studio 2010 and Visual Studio 2012, on September 14th. You can read more here.

You can read more about LocalDB on Books On-Line. And you can download it here.

Sorry for any confusion.

SQL Server Data Tools – A Little Deeper

In my last post I wrote about some of the functionality of the new SQL Server Data Tools (SSDT), the replacement for Business Intelligence Development Studio (BIDS). Today I’m going to look at a few things I didn’t cover before.

Today’s first point is that you’re not tied to Visual Studio 2010, you can also use the new 2012 version if you prefer. SSDT will still open VS 2010 even if both are installed. To use VS 2012 you’ll have to open it manually. But everything else is there. Today’s demos will be in VS 2012.

In my last post I created a new local database based on an existing database attached to my local instance. I also said that wasn’t the only way to create a localdb. Back in VS (either version) create a new project, choose Other Languages > SQL Server > SQL Server Data Project. After VS finishes creating the project you’ll see the LocalDB running in the SQL Object Explorer with a database that has the same name as your just created project. You might see other databases if you’ve created other database projects, and you might also see a second LocalDB if you created any in a different VS version. You can always disconnect any server if it becomes too distracting.

While LocalDB is not a real SQL instance you still can control the properties of the server and any database you create. To see the server properties right click on LocalDB and choose Properties. The properties will open, though the properties here seem mostly to be read only. But you can use sp_configure to change them. For the database properties right click on the project name in the Solution Explorer and choose Properties. The project page opens as in the screenshot below. You can change the Target Option to any SQL 2005, 2008, 2012, or Azure. You can change database settings, like the collate option, by clicking the Database Settings button.image

Now you need to create your database objects, and again there’s a few ways you can go. You can import an existing schema from another database,  a .dacpac snapshot created by another project, or a .sql script. These options would come in handy if you want to create a local sandbox with a copy of a production database. You can grab the scripts from your source control (you ARE using source control, right?), or maybe there’s a .dacpac file that can be shared among developers. To import  right click the project name in the Solution Explorer, chose Import, then the import method.

But maybe you’re building a demo database from scratch and you don’t have any scripts created yet. You can build the objects inside the project. Again right click the project name and this time select Add, then the type of object you’re going to create. In my case I’m going to create a table; creating other objects are similar.

When you create the table you’ll give it a name. After the dialog screen closes the table designer will open in a new window. The window is split, with the T-SQL script in one half and the visual designer n another. You can swap them left to right, top to bottom, whatever you’re comfortable with. By default it will create an Id column as the primary key but you can change that. Go ahead and add columns to your table, either in the T-SQL side or the designer side. The cool thing is that, wherever you make your change, it’s made automagically on the other side.

You can add indexes, keys, constraints, and triggers by right clicking the object type then Add New. Build and deploy your new objects by pressing F5. image

I’ll have one more post on using SSDT projects.

SQL Server Data Tools – An Introduction

SQL Server Data Tools (SSDT) was introduced as a replacement to the old Business Intelligence Development Studio that’s been around since SQL 2005. But it does more than just create Integration Service or Analysis Service projects. Microsoft has taken the old BIDS and combined it with the Data parts of Visual Studio for a whole new tool; SSDT.

SSDT is a new way to develop SQL database objects. It ties into Visual Studio 2010 and now developers can work in a familiar environment. SSDT can be installed as a feature of a SQL 2012 installation or as a separate download from Microsoft. If you’ve already installed VS 2010 Professional or above you’ll need to upgrade to with SP1 first, if you haven’t already. If you’re not running VS 2010 SSDT will install a VS 2010 shell for your use.

Working Online

Working against an online database is done very similar to working in Management Studio. Open SSDT, then open SQL Server Object Explorer and create a connection to your server. Once you’ve done that you can view databases and their objects. This is very similar to what you see in SSMS. To open a new query window click the New Query icon (right button in SQL Object Explorer). The query window is a slimmed down SSMS query window, but the same functionality is there; there’s buttons on the toolbar for Estimated and Actual Execution plans or viewing the results in a grid or text. You’ll see your results and messages in separate tabs on the bottom, or like VS you can switch where the code and results display. Intellisense is available in the query pane. The main difference is that you execute your code with Ctrl + Shift + E instead of F5. The little green Execute arrow is still there if you rather use that method.

image

Working Offline

My favorite features come when you work in disconnected mode.When you’re disconnected you’re working in a local sandbox, completely separated from other users. You can work on tuning that stored procedure while not affecting any application.

There’s a few ways to do this, but I found the easiest is to right click on the database you want to create a copy of, then choose “Create New Project”. You’ll get the screen below where you can set your project options. One of the most helpful is the last one, on file structure. You can organize your project by schema, by object, by both, or by neither. I personally prefer both.

image

After you click Start SSDT will create the objects in your new project. In the Solution Explorer are all the objects created and sorted as you chose when you created the project. You’ll also see a new entry in the SQL Object Explorer pane, called LocalDB\ProjectName. You’ll also notice that the database doesn’t appear to have any objects yet. Press F5 to build and deploy your project, refresh the SQL Object Explorer and voila! There they all are.

image

Note that this only creates the objects. It doesn’t populate the tables.But that’s a piece of cake. Back in SQL Object Explorer, go to your original source database, right click on the table that has the data you want, and choose View Data. A new query window will open showing the records just like SSMS does. You can change the number of records returned as well, though I don’t see a way of placing a filter on the results. Anyway, if you click on the Script button a new query window will open with an INSERT statement for each record in the results. Change the connection to the LocalDB database,  run the query, and congratulate yourself. You’ve just populated your local table! You can query the LocalDB, update or delete records, whatever you need, without affecting anyone else.

image

First a word about that LocalDB database you just created. Don’t confuse it with a database on your local instance. You may not even have one. What SSDT uses is a database it creates that doesn’t need SQL. If you look at the properties of the LocalDB instance it will say “Microsoft SQL Express Edition”, but this isn’t the Express Edition available as a download.It doesn’t run as a service, and as far as I can see it’s only available when SSDT is running. If you drill into the directory where you created your project you’ll spot a Sandbox folder, and inside will be a .mdf and .ldf file for your database. There are some warning about using LocalDB, mainly that it doesn’t support all SQL features. I know FILESTREAM is one.You’ll probably want to check on BOL for others.

Snapshots and Schema Compare

My next favorite feature is the ability to create snapshots of your project and to compare schemas between databases.

Snapshots are a representation of your database project at a given moment. Back in Solution Explorer, right click on the project name and select Snapshot Project. SSDT will create a file with the .dacpac extension. The default name is the database with a timestamp but you can call it anything you like. You can create snapshots anytime, at different stages of your development. And that’s where Schema Compare really shines.

Let’s say you connect to a database and create a new database project. And you create a snapshot before you start development. While developing you make changes to existing objects or create new ones. Now you want to create a change script, but maybe you forgot what objects you touched. And you can’t go back and compare to your source database as maybe it has changed as well from other users. You can still use Schema Compare and use the snapshot files you created as a source or target. Schema Compare will work the same way it has against “real” databases, showing you the differences in the schemas. You can either create a script to run or click the Update button to apply your changes. You can filter by schema or object if you don’t want to compare large projects.

image

There’s more to SSDT than just what I’ve written here. I’ll try to write another post on SSDT going a bit deeper. In the meantime, check out SSDT in Books Online, or just Google it. This is a very handy tool for SQL developers to add to their arsenal.

Updating DimDate in AdventureWorksDW2012

I’ve finally gotten around to playing with the new BI features that were released with SQL Server 2012 (much more on that later). I’m using some older samples and packages I wrote in earlier edition, then rewriting them in 2012. One of the minor issues I’ve run into is that the dates in the AdventureWorks sample databases for 2012 don’t match some of my packages or walkthroughs. I totally failed on some that were hitting the DimDate dimension table in AdventureWorksDW2012. I probably could have tracked down earlier versions of the samples but I didn’t want to have multiple copies of the same database on my development server. This post is just a script to add new dates to the existing date dimension table.

Most of it is pretty straight forward. I just use different date functions to return parts of a date, like year, month, day, etc. I use a CASE statement to set the fiscal year and semester fields using the same logic that exists with the current data (fiscal year starts on July 1st, first six months of year is 1st calendar semester and 2nd fiscal semester, second six months is 2nd calendar semester and 1st fiscal semester). I use a couple of joins to the existing table to get the Spanish and French  day and month names. Then I loop through a date range, inserting the data.

Notice that I’ve only ran this against the SQL 2012 version. But it should run with minor modifications, on other versions. Also note that this is meant for testing data. You’re on your own if you screw up production data.

 DECLARE @StartDate    DATE
 DECLARE @EndDate    DATE
 SET @StartDate = '1/1/2000'
 SET @EndDate = '12/31/2004'

WHILE @StartDate <= @EndDate
 BEGIN
 INSERT INTO AdventureWorksDW2012.dbo.DimDate
 SELECT  CONVERT(INTEGER, CONVERT(CHAR(10),  @StartDate, 112)) AS DateKey
 , @StartDate AS FullDateAlternateKey
 , DATEPART(dw, @StartDate) AS DayNumberOfWeek
 , DATENAME(dw, @StartDate) AS EnglishDayNameOfWeek
 , DayNames.SpanishDayNameOfWeek
 , DayNames.FrenchDayNameOfWeek
 , DAY (@StartDate) AS DayNumberOfMonth
 , DATENAME(dayofyear, @StartDate) AS DayNumberOfYear
 , DATENAME(week, @StartDate) AS WeekNumberOfYear
 , DATENAME(mm, @StartDate) AS EnglishMonthName
 , MonthNames.SpanishMonthName
 , MonthNames.FrenchMonthName
 , MONTH(@StartDate) AS MonthNumberOfYear
 , DATENAME(quarter, @StartDate) AS CalendarQuarter
 , YEAR(@StartDate) AS CalendarYear
 , CASE
 WHEN DATENAME(quarter, @StartDate) <= 2 THEN 1 ELSE 2
 END AS CalendarSemester
 , CASE
 WHEN MONTH(@StartDate) IN (7, 8, 9) THEN 1
 WHEN MONTH(@StartDate) IN (10, 11, 12) THEN 2
 WHEN MONTH(@StartDate) IN (1, 2, 3) THEN 3
 WHEN MONTH(@StartDate) IN (4, 5, 6) THEN 4
 END AS FiscalQuarter
 , CASE
 WHEN DATEPART(m, @StartDate) >= 7 THEN YEAR(@StartDate) + 1 ELSE YEAR(@StartDate)
 END AS FiscalYear
 , CASE
 WHEN DATENAME(quarter, @StartDate) >= 3 THEN 1 ELSE 2
 END AS FiscalSemester
 FROM
 (
 SELECT DISTINCT DayNumberOfWeek, SpanishDayNameOfWeek, FrenchDayNameOfWeek
 FROM AdventureWorksDW2012.dbo.DimDate
 ) AS DayNames,
 (
 SELECT DISTINCT MonthNumberOfYear, SpanishMonthName, FrenchMonthName
 FROM AdventureWorksDW2012.dbo.DimDate
 ) AS MonthNames
 WHERE DayNames.DayNumberOfWeek = DATEPART(dw, @StartDate)
 AND MonthNames.MonthNumberOfYear = DATEPART(mm, @StartDate)

SET @StartDate = DATEADD(dd, 1, @StartDate)
 END
 

Microsoft SQL Server 2012 T-SQL Fundamentals – A Review

I’ve just picked up a copy of Itzik Ben-Gan’s latest book, Microsoft SQL Server 2012 T-SQL Fundamentals, published by Microsoft Press. If you’ve ever researched anything about SQL you’re probably already familiar with the author. Mr Ben-Gan is well-respected in the SQL field and is one of the top experts. He has written or co-written many of the must-have books going back through previous versions of SQL. His book, Microsoft SQL Server 2012 High-Performance T-SQL Using Window Functions was published earlier this year.

This is a book for anyone who works with T-SQL, at any skill level, though you’ll get more from it if you already have at least a bit of SQL experience. The first chapter goes through some of the basics, like creating tables and constraints, but not in great depth. It also discusses normalization, set theory, the relational model, and explains what a data warehouse is. Since this edition covers SQL 2012 he also describes Windows Azure SQL Database, the cloud based version of SQL.

Where the book really shines is from the second chapter on. Chapter 2 is all about simple queries against a single table. He goes through the SELECT statement syntax and how SQL processes a query. He explains how to work with different data types; all the built-in functions available for string manipulation and the different date and time functions. Chapter 3 goes into joins; what the difference is between an inner and outer join and when you want to use each. By Chapter 5 you’re getting into sub-queries and Common Table Expressions. The rest of the book covers so much more; locking and concurrency, data modification, cursors and temp tables, variables, and on and on.

And since this is the SQL 2012 edition he covers the new T-SQL features. For instance, when he writes about using the TOP keyword in a SELECT statement he also describes the new OFFSET and FETCH function and how they’re useful in limiting the number of records. The section on IDENTITY is matched by one on the new SEQUENCE object. The new Windows functions are described and demoed here as well.

This edition is similar to earlier editions of T-SQL Fundamentals. At the end of each chapter are a list of exercises that covers what you just learned. This is followed by the solutions, where you get not just the answer but a further explanation of why this is the right solution. If multiple solutions are available each is explained.

I highly recommend this book to everyone who works with SQL Server. It’s not just for SQL 2012, as most of what the book covers is also valid against earlier versions. I plan on using this as a reference again and again.

OFFSET – FETCH in SQL Server 2012

Most of us are familiar with using the TOP keyword in a T-SQL query. Basically, TOP will return a specified percentage or number of records. For instance, to see the top 100 sales orders by total price you may run a query similar to this one.

SELECT TOP 100 SalesOrderID, SalesOrderDetailID, ProductID, OrderQty, UnitPrice, LineTotal
FROM AdventureWorks2012.Sales.SalesOrderDetail
ORDER BY LineTotal DESC;

In SQL Server 2012 there’s a new function we can use, OFFSET – FETCH. You can get the same results as above using this new functionality;

SELECT SalesOrderID, SalesOrderDetailID, ProductID, OrderQty, UnitPrice, LineTotal
FROM AdventureWorks2012.Sales.SalesOrderDetail
ORDER BY LineTotal DESC
OFFSET 0 ROWS FETCH NEXT 100 ROWS ONLY;

When you use OFFSET you specify the starting position. To begin at the top (or bottom you start at 0, which I did above. But you can start anywhere. To see the next hundred records you’d say OFFSET 99. You can use a variable for OFFSET, but OFFSET must be an integer that is 0 or greater.

FETCH NEXT is the number of records to return. In my case I’m only returning 100 rows but you can specify any valid number. It can come from a variable or a subquery.

The syntax has a few options. You can state ROW instead of ROWS, SQL will take either. That means OFFSET 1 ROW is just as valid as OFFSET 1 ROWS.  Also NEXT and FIRST are the same, use either one in your statement.

There are some differences between TOP and OFFSET – FETCH.

  • The ORDER BY clause is required for OFFSET, not for TOP. But you can sort in either direction.
  • FETCH is not required. If you want to skip the first 100 records just state OFFSET 99;
  • FETCH doesn’t support WITH TIES or PERCENT.  You can approximate the percentage by using a subquery but you may not get the same number of records as using TOP n PERCENT
SELECT SalesOrderID, SalesOrderDetailID, ProductID, OrderQty, UnitPrice, LineTotal
FROM AdventureWorks2012.Sales.SalesOrderDetail
ORDER BY LineTotal DESC
OFFSET 0 ROWS FETCH NEXT (SELECT COUNT(*) / 10 FROM AdventureWorks2012.Sales.SalesOrderDetail) ROWS ONLY;

So, play around with OFFSET – FETCH.

Error when saving Tabular Project in Visual Studio 2010

This is just a quick post about an error I received when trying to save a Tabular Project in Visual Studio 2010 and a simple fix that worked for me.

So this weekend I thought I’d get some early morning studying in. I just Visualizing Data with Microsoft Power Viewstarted reading Visualizing Data with Microsoft Power View, and that’s where I’m getting my examples and data from. I had already set up my virtual environment to run SQL 2012 PowerPivot and SharePoint 2010. Excel 2010 is also installed with the PowerPivot add-on, both on the host and virtual server. Visual Studio 2010 with SP1 is also installed on the host. I’ve restored the needed sample databases. And I created the first PowerPivot workbook from the examples in the book. The next step then was to create a Tabular Project using SQL Server Data Tools. Sounds simple enough.

So I opened my local copy of Visual Studio 2010 and selected New Project. I opened the Business Intelligence templates and chose Analysis Services >> Import From Power Pivot. I imported data from an Excel file that I had created in an earlier step. I walked through the exercise, saving my work at different points. Then I went to close my solution for the day. That’s when I got the following error; “The operation could not be completed. No such interface supported”.

image

OK, maybe I missed installing something. Since everything was ready in my virtual environment I thought I’d try the same steps there. This time I opened SQL Server Data Tools, which then opened the VS2010 shell with just the Business Intelligence templates. I repeated my steps to create a new Tabular Project. And when I went to save the project I got the same error.

I tested a few other project types back in VS 2010 on the host and reproduce the error. The problem only seemed to be with the Tabular Project templates under Business Intelligence > Analysis Services.  I couldn’t save any solutions when I used these 3 project types.

The error message isn’t really helpful, so I did a web search. I didn’t find any matches specifically for Tabular Projects, but there were a number of hits for different software and different versions. Some said the cause was corrupted Windows .dll files and recommended a reinstall of Windows, kind of drastic. Some hits said to remove certain registry keys, none of which exist on my computer. Other hits were similar.

Finally an old post popped up, from an earlier beta version. It said to make sure that the option to save new projects on creation was selected. Could it really be that simple? Easy enough to test. I checked my settings under Tools >> Options, and there, under the Projects and Solutions >> General, was the option. I checked it and tried the steps again.  And guess what? It worked! Now I could save my new Tabular BI solutions.

image

So if you run into the same situation, where you can’t save a Tabular Project solution, make sure “Save new project when created” is checked. If that still doesn’t work then good luck! 

Using FileTables in SQL Server 2012

This post is about working with FileTables in SQL Server 2012. I’m not going to go over FileTables in general or how to create them as there are many blogs walking through the procedure and setting up the prerequisites; I’ve put links to some of them in the Resources section at the bottom of this page.

I’m using a virtual server running the Developer Edition of SQL 2012 on Windows Server 2008 R2 called VMBI2012, hosted in Virtual Box. That’s not a requirement and you can duplicate everything here on the same box, but I wanted to point out an issue with certain file types when trying to access them directly. Also you don’t need the Developer Edition of SQL, Microsoft says that FileTables are supported in all editions.

The only part you may need to add is the Semantic database. It’s not an option when you install SQL but look for the SemanticLanguageDatabase.msi package on your installation media or download from Microsoft here. After you run the package attach the database (it’s placed in C:\Program Files\Microsoft Semantic Language Database\ by default but you can move it). Then just register it;

EXEC sp_fulltext_semantic_register_language_statistics_db @dbname = N’semanticsdb’;

OK, so I’ve configured my server to use Filestreaming, and I added the semantic database. I created a database that contains a FileTable called Documents and also set up a full text catalog. The script I used is as follows, if you want to follow along at home;

CREATE DATABASE DenaliDemoDB
ON  PRIMARY
(
NAME = 'DenaliDemoDB_data',
FILENAME = 'C:\SQL2012\DATA\DenaliDemoDB_data.mdf',
SIZE = 512000KB ,
MAXSIZE = UNLIMITED,
FILEGROWTH = 102400KB
),
FILEGROUP FSSecondary CONTAINS FILESTREAM
(
Name = FileTableData,
FILENAME = 'C:\SQL2012\FileTables\Data'
)
LOG ON
(
NAME = 'DenaliDemoDB_log',
FILENAME = 'C:\SQL2012\DATA\DenaliDemoDB_log.ldf',
SIZE = 102400KB,
MAXSIZE = 2048GB,
FILEGROWTH = 25600KB
)
WITH FILESTREAM
(
NON_TRANSACTED_ACCESS = FULL,
DIRECTORY_NAME = 'MyScripts'
);

USE DenaliDemoDB;

CREATE TABLE dbo.Documents AS FILETABLE
WITH
(
FILETABLE_DIRECTORY = 'DocumentsFT',
FILETABLE_COLLATE_FILENAME = database_default
);

CREATE FULLTEXT CATALOG Documents_Catalog WITH ACCENT_SENSITIVITY = ON;

CREATE FULLTEXT INDEX ON dbo.Documents
(
name
LANGUAGE 1033
STATISTICAL_SEMANTICS,
file_type
LANGUAGE 1033
STATISTICAL_SEMANTICS,
file_stream
TYPE COLUMN file_type
LANGUAGE 1033
STATISTICAL_SEMANTICS
)
KEY INDEX PK__Document__5A5B77D5962E5D8F
ON Documents_Catalog
WITH CHANGE_TRACKING AUTO, STOPLIST = SYSTEM;

There’s a few things to point out here. First, notice the name of the KEY INDEX. This is the name SQL assigned when I created the FileTable, your name will most likely be different. You can always rename it before you create the catalog. Second, that for each column I’m declaring STATISTICS_SEMANTICS. This is to take advantage of the semantic searching, but it’s optional. If you create it in SSMS it’s just a checkbox, like so.

image

After that I created 4 .txt files called text1.txt through text4.txt. The content in each is simple:

  1. This is text one. Magic
  2. This is text 2. French Roast
  3. This is text 3. French Roast and Columbian
  4. This is text four. Magic and Wizards

If you use T-SQL to query the table you’ll see 4 records, one for each file. If you browse to the shared folder you’ll see the same files. However if you try to open any of them you get this error . imageAccording to Books Online that’s because FileTables don’t support memory-mapped files. Paint files are another. However you probably don’t want users editing files directly on the server anyway. Instead use another computer and map a drive to the shared folder and edit it that way. I used my host machine to map to the vm’s shared director to edit text4.txt. Now it says “What happens behind the scenes when we manipulate this file inside SQL?”

Now we’re set to work with the FileTable. First, to find the name of the shared directory use the FILETABLEROOTPATH function with the name of the FileTable as a parameter.

SELECT FILETABLEROOTPATH('dbo.Documents')

image

Use the GetFileNamespacePath method of the file_stream column to return the path of each file in the FileTable. It takes two parameters. The first is is_full_path which returns the file’s path, with 0 (the default) returning the relative path and 1 returning the UNC path. The second is @option, where 0 (the default) shows the NetBIOS name, 1 returns the server name, and 2 returns the complete path.

SELECT file_stream.GetFileNamespacePath(1, 2) as FileLocation
FROM dbo.Documents

image

Finally, I can use the full text catalog I set up earlier to search the file_stream column. If I search for the word “french” I should get back text2.txt and text3.txt.

SELECT file_stream.GetFileNamespacePath() as FileLocation
FROM dbo.Documents
WHERE CONTAINS(file_stream, 'french')

image

Semantic Search builds on the capabilities of the full text catalog. Not only can you search for matches but also by key phrases. Using the SEMANTICKEYPHRASETABLE function can show documents by searching for a keyphrase and the score of a likely match.

SELECT FILETABLEROOTPATH('dbo.Documents') + '\' + d.name AS FilePath, s.keyphrase, s.score
FROM dbo.Documents AS d
INNER JOIN SEMANTICKEYPHRASETABLE(dbo.Documents, *) AS s
ON d.path_locator = s.document_key
WHERE s.keyphrase LIKE '%r%'
ORDER BY s.score DESC;

image

SEMANTICSSIMILARITYDETAILSTABLE compares two documents and shows the similarity between the two…

DECLARE @Document1 hierarchyid;
DECLARE @Document2 hierarchyid;

SET @Document1 = (SELECT path_locator FROM dbo.documents WHERE name = 'text2.txt');
SET @Document2 = (SELECT path_locator FROM dbo.documents WHERE name = 'text3.txt');

SELECT *
FROM SEMANTICSIMILARITYDETAILSTABLE
(
dbo.Documents,
file_stream, @Document1,
file_stream, @Document2
)
ORDER BY score DESC;

image

Finally, SEMANTICSIMILARITYTABLE can be used to compare all documents to a single file.

DECLARE @Document3 hierarchyid;

SET @Document3 = (SELECT path_locator FROM dbo.documents WHERE name = 'text1.txt');

SELECT s.source_column_id, s.matched_column_id, FILETABLEROOTPATH('dbo.Documents') + '\' + d.name AS FilePath, s.score
FROM dbo.documents AS d
INNER JOIN SEMANTICSIMILARITYTABLE
(
dbo.Documents,
file_stream, @Document3
) AS s
ON d.path_locator = s.matched_document_key
ORDER BY score DESC;

image

References

To get more information on FileTables or Semantic Searches start with Books Online

FilesTables in Books Online
http://msdn.microsoft.com/en-us/library/ff929144.aspx

Semantic Search on BOL
http://msdn.microsoft.com/en-us/library/gg492075.aspx

Bob Beauchemin has some very informative posts on filestreaming and filetables
http://sqlskills.com/BLOGS/BOBB/category/Filestream-Storage.aspx

Finally, here’s a few posts that walk through the process of setting up file tables.

Beyond Relational – File Table in SQL Server 2012 – Great flexibility to manage filestream data
http://beyondrelational.com/modules/24/syndicated/389/Posts/11829/file-table-in-sql-server-2012-great-flexibility-to-manage-filestream-data.aspx

SQL Server Central
http://www.sqlservercentral.com/blogs/sqlservernotesfromthefield/2012/01/03/sql-server-2012-filetable-part-1/