<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>SQL</title>
        <link>http://blogs.sftsrc.com/stuart/category/14.aspx</link>
        <description>SQL</description>
        <language>en-US</language>
        <copyright>Stuart Thompson</copyright>
        <managingEditor>stuart.thompson@sftsrc.com</managingEditor>
        <generator>Subtext Version 1.9.5.0</generator>
        <item>
            <title>SqlDependency - Creating a Smarter Cache </title>
            <link>http://blogs.sftsrc.com/stuart/archive/2007/06/13/42.aspx</link>
            <description>&lt;p&gt;I recently got the chance to learn about a technology that has interested me for quite some time: SQL dependencies.  The premise of my encounter with &lt;a href="http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqldependency(VS.80).aspx"&gt;SqlDependency&lt;/a&gt; was to create a smarter cache.  The project I'm currently working on was employing a sliding expiration policy to invalidate the contents of a cache of database lookup values and I proposed some research be conducted into using SQL dependencies to automatically invalidate the cache items when the corresponding database contents were changed.  I had experimented with the SQL dependency classes when .NET 2.0 was first released using a test SQL 2000 database.  The example I was using employed a polling strategy to keep the contents of an ASP.NET web cache item fresh.  While the technology was cool, I was less than enamored by the fact that the web server was polling the database for changes.  It felt a little too similar to setting a sliding expiration value of 5 seconds on the cache and pinging the database that way. &lt;/p&gt;
&lt;p&gt;I was, however, aware of the fact that SQL 2005 notifications could be used to invalidate the contents of a cache without the need for polling.  With this in mind, I set about investigating the possibilities of SQL dependencies using .NET 2.0 and SQL 2005.  While this certainly isn't a new topic, I found that very little good documentation exists on the topic and decided to write this article to hopefully bring some clarity to others who might wish to take advantage of the technology. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;When is this a good solution?&lt;/strong&gt;&lt;br /&gt;
The gap that this solution is attempting to fill is the short window where data can be stale with a sliding expirationon cache items.  If the data in the database has changed but the sliding expiration on the cache has not yet expired then the values in the cache will be out of date but still considered valid and hence returned to the UI.  Using SQL 2005 notifications will invalidate the contents of the cache &lt;em&gt;almost&lt;/em&gt; as soon as the database contents are changed.  You will need to determine for your application how long data in the cache can be stale before the usefulness of the application is compromised.  If the sliding expiration of cache items can be shortened to compensate satisfactorily then that may provide a "better" solution that SQL dependencies.  However, if near real-time cache invalidation is desirable then SQL dependencies using notifications could provide a good solution. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;How does the solution work?&lt;/strong&gt;&lt;br /&gt;
The basic mechanics of the solution involve a cache object, a rowset within SQL (I'll simplify for now and just say a table), a trigger and a notification queue.  There are many moving parts under the scenes that make the technology come together, but that covers the basic list of participants.  In simplified terms, a rowset is read from the database and an object representation of that data is added to a cache.  A SQL dependency is established upon that rowset and a callback method is registered for that dependency such that when the rowset changes the callback method will be invoked.  A trigger is used within the database to detect when the rowset changes, and a notification queue within SQL Server is used to broker a message back to cause the callback method to be invoked.  The callback method invalidates the contents of the cache.  The next time the data is requested from the cache, the contents are found to be invalid and the process starts again. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Creating a Sample Application&lt;/strong&gt;&lt;br /&gt;
This example uses Visual Studio 2005 and SQL Server 2005 to set up a sample database and two-tier web application running on the same machine to demonstrate use of the SqlDependency class as part of a caching strategy. &lt;/p&gt;
&lt;p&gt;Let's start by creating a sample database called SqlDependExample.  Add a table to the database called Color.  This table has two columns: ColorID, and ColorName.  It will represent a simple lookup table that contains a list of colors within your application.  Make the ColorID column an int that does not allow nulls, the primary key and set (Is Identity) to Yes.  Make the ColorName column a varchar(100) that does not allow nulls.  Add a couple of rows of data to the table with the following values: 1   Red 2   Blue 3   Green 4   Yellow &lt;/p&gt;
&lt;p&gt;Create a login on the SQL Server called SqlDependExampleUser that uses SQL Server Authentication, and choose an appropriate password.  Add a user called SqlDependExampleUser to the SqlDependExample database for the SqlDependExampleUser login and grant the db_datareader, db_datawriter, and db_ddladmin roles. &lt;/p&gt;
&lt;p&gt;Now we need to configure the database to be able to use the service broker and grant some additional permissions to the user we created in order that they can create and manage objects necessary for the notification strategy to work.  The following script performs each of the necessary steps to configure the database and grant the required permissions.  It is important to note that the first step requires SQL Server to have an exclusive lock on the database while performing the query, so be sure to close out any unused connections in the management studio; including the dialog that was used to create the Color table. &lt;/p&gt;
&lt;p&gt;Open a query window and paste the following script: &amp;amp;nbps;&lt;strong&gt;(be sure that the selected database is SqlDependExample, not master)&lt;/strong&gt;&lt;br /&gt;
ALTER DATABASE SqlDependExample SET ENABLE_BROKER&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
GRANT CREATE PROCEDURE TO [SqlDependExampleUser];&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
GRANT CREATE SERVICE TO [SqlDependExampleUser];&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
GRANT CREATE QUEUE TO [SqlDependExampleUser];&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
GRANT REFERENCES ON CONTRACT::[http://schemas.microsoft.com/SQL/Notifications/PostQueryNotification] TO [SqlDependExampleUser];&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
GRANT SUBSCRIBE QUERY NOTIFICATIONS TO [SqlDependExampleUser];&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
GRANT CONTROL ON SCHEMA::[dbo] TO [SqlDependExampleUser];&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
GRANT IMPERSONATE ON USER::DBO TO [SqlDependExampleUser];&lt;br /&gt;
GO &lt;/p&gt;
&lt;p&gt;Execute the script to enable the service broker for the SqlDependExample database and grant the necessary permissions to SqlDependExampleUser. &lt;/p&gt;
&lt;p&gt;Now, open Visual Studio and create a new project (File -&amp;gt; New Project).  Select Class Library as the project type, enter SqlDependExample as the solution name, and enter SqlDependExampleCache as the project name.  Add a class to the project called CacheProvider and paste in the following code:&lt;br /&gt;
using System;&lt;br /&gt;
using System.Collections.Specialized;&lt;br /&gt;
using System.Data;&lt;br /&gt;
using System.Data.SqlClient;&lt;br /&gt;
&lt;br /&gt;
namespace SqlDependExampleCache {&lt;br /&gt;
&lt;br /&gt;
    public class CacheProvider {&lt;br /&gt;
&lt;br /&gt;
        /// &lt;summary&gt;&lt;/summary&gt;&lt;br /&gt;
        /// Reads the list of colors from the cache.&lt;br /&gt;
        /// &lt;br /&gt;
        /// &lt;returns&gt;&lt;/returns&gt;&lt;br /&gt;
        public StringCollection GetColorList() {&lt;br /&gt;
            if (this.colors == null) {&lt;br /&gt;
                this.colors = this.ReadColorList();&lt;br /&gt;
            }&lt;br /&gt;
            return this.colors;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        /// &lt;summary&gt;&lt;/summary&gt;&lt;br /&gt;
        /// Reads the list of colors from the database.&lt;br /&gt;
        /// &lt;br /&gt;
        /// &lt;returns&gt;&lt;/returns&gt;A &lt;see cref="System.Collections.Specialized.StringCollection"&gt;&lt;/see&gt;that contains the list of colors.&lt;br /&gt;
        private StringCollection ReadColorList() {&lt;br /&gt;
            string connStr = "Database=SqlDependExample;Server=.;User ID=SqlDependExampleUser;Password=supersecret;";&lt;br /&gt;
            StringCollection colors = new StringCollection();&lt;br /&gt;
&lt;br /&gt;
            using (SqlConnection conn = new SqlConnection(connStr)) {&lt;br /&gt;
                conn.Open();&lt;br /&gt;
                using (SqlCommand cmd = new SqlCommand("SELECT ColorID, ColorName from dbo.Color", conn)) {&lt;br /&gt;
                    SqlDependency.Start(connStr);&lt;br /&gt;
                    SqlDependency sqlDependency = new SqlDependency(cmd);&lt;br /&gt;
                    sqlDependency.OnChange += new OnChangeEventHandler(sqlDependency_OnChange);&lt;br /&gt;
                    using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)) {&lt;br /&gt;
                        while (reader.Read()) {&lt;br /&gt;
                            colors.Add(reader["ColorName"].ToString());&lt;br /&gt;
                        }&lt;br /&gt;
                    }&lt;br /&gt;
                }&lt;br /&gt;
            }&lt;br /&gt;
&lt;br /&gt;
            return colors;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        /// &lt;summary&gt;&lt;/summary&gt;&lt;br /&gt;
        /// Raised when the underlying rowset changes. Invalidates the list of colors.&lt;br /&gt;
        /// &lt;br /&gt;
        void sqlDependency_OnChange(object sender, SqlNotificationEventArgs e) {&lt;br /&gt;
            this.colors = null;&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        /// &lt;summary&gt;&lt;/summary&gt;&lt;br /&gt;
        /// Internal list of colors.&lt;br /&gt;
        /// &lt;br /&gt;
        private StringCollection colors;&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
} &lt;/p&gt;
&lt;p&gt;Now add a new web site to the solution named SqlDependExampleWeb.  Add it as a file system web site underneath the solution directory we created earlier that was called SqlDependExample.  Open the property pages for the web site, select the References tab and add the SqlDependExampleCache project as a reference to the project.  Open the Default.aspx page and add the following bulleted list definition between the main div tags in the page:&lt;br /&gt;
&lt;asp:bulletedlist id="ddlColorList" runat="server" /&gt;&lt;/p&gt;
&lt;p&gt;Now open the code-behind file for the page and paste the following code as the contents of the class: using System;&lt;br /&gt;
using System.Collections.Specialized;&lt;br /&gt;
using System.Web;&lt;br /&gt;
using System.Web.UI.WebControls;&lt;br /&gt;
&lt;br /&gt;
using SqlDependExampleCache;&lt;br /&gt;
&lt;br /&gt;
public partial class _Default : System.Web.UI.Page {&lt;br /&gt;
&lt;br /&gt;
    protected void Page_Load(object sender, EventArgs e) {&lt;br /&gt;
&lt;br /&gt;
        CacheProvider cp = new CacheProvider();&lt;br /&gt;
        StringCollection colors = cp.GetColorList();&lt;br /&gt;
&lt;br /&gt;
        foreach (string color in colors) {&lt;br /&gt;
            ddlColorList.Items.Add(new ListItem(color));&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Add a breakpoint to line 50 of the CacheProvider class (this.colors = null), select the SqlDependExampleWeb as the StartUp project and hit F5 to begin debugging.  Answer yes that a new web.config should be created to enable debugging when prompted and the web site should run, displaying the default.aspx page.  The four colors Red, Blue, Green, and Yellow that we added to the Color table earlier should be displayed in a bulleted list.  Now open the Color database table in SQL Management Studio and add a new row for the color Purple.  You should notice that the breakpoint on line 50 of CacheProvider is active.  This is because the SQL dependency on the Color table has fired and raised the OnChange callback event.  Allow execution to continue and the full list of five colors will be displayed in the page. &lt;/p&gt;
&lt;p&gt;This short demonstration should give a good idea of the mechanism that is being employed.  The cache is vastly oversimplified and documentation is lacking simply for the brevity of the example.  However, the principal holds for a larger application that a rowset can be monitored by SQL server and a callback method invoked when that rowset changes thus prompting some action. &lt;/p&gt;
&lt;p&gt;Some important points to note here are that the query syntax for rowsets that can be monitored is quite restrictive.  The query used in this example was:&lt;br /&gt;
SELECT ColorID, ColorName from dbo.Color &lt;/p&gt;
&lt;p&gt;The table name is qualified both with the owner and table name.  This is required for the SQL dependency to work correctly with the row set.  A number of additional restrictions apply to the queries that can be used with SQL dependencies.&lt;/p&gt;&lt;img src="http://blogs.sftsrc.com/stuart/aggbug/42.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stuart Thompson</dc:creator>
            <guid>http://blogs.sftsrc.com/stuart/archive/2007/06/13/42.aspx</guid>
            <pubDate>Wed, 13 Jun 2007 22:56:15 GMT</pubDate>
            <comments>http://blogs.sftsrc.com/stuart/archive/2007/06/13/42.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://blogs.sftsrc.com/stuart/comments/commentRss/42.aspx</wfw:commentRss>
        </item>
        <item>
            <title>.NET CLR in SQL Server 2005</title>
            <link>http://blogs.sftsrc.com/stuart/archive/2007/05/04/22.aspx</link>
            <description>&lt;p&gt;Of recent interest to me has been the ability to deploy user-defined managed functions, stored procedures, and other objects to SQL from within Visual Studio 2005. With the .NET CLR in SQL Server 2005, these managed functions can be written and deployed from within Visual Studio, even as part of an automated build process. In wanting to investigate this further, I've decided to write a few articles on how this technique is used and on how it may be useful to you. When I first heard about the ability to (essentially) deploy .NET assemblies into SQL Server and then have SQL Server make managed calls to them for use in queries I was skeptical, thinking this was yet another lost solution embarking upon a life quest for a problem to solve. However, several examples have since been drawn to my attention regarding the usefulness of this solution in exposing features of the richer .NET languages for use in advanced queries. &lt;/p&gt;
&lt;p&gt;The first of these areas is that of regular expressions. Earlier this year, I came across an article describing how calls to the .NET 2.0 RegEx classes could be wrapped in a managed function for use in the where clause of a SQL query. My ears (eyes) perked up at this, for the idea of using regular expressions to match data in queries could add a massive amount of power to my SQL toolbox. Even better, because the call would be to the .NET classes, we already know that I'd be able to pre-compile the expressions if needed as that is a feature of that implementation. The rest of this article walks through a simple example for creating a C# Database Project in Visual Studio 2005, deploying a managed function developed in that project, and then writing a query to use that function for RegEx matching within the WHERE clause. For those interested in more information, the original article can be found here: &lt;a href="http://msdn.microsoft.com/msdnmag/issues/07/02/SQLRegex/default.aspx"&gt;http://msdn.microsoft.com/msdnmag/issues/07/02/SQLRegex/default.aspx&lt;/a&gt;. &lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Creating a C# Database Project in Visual Studio &lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;First, open Visual Studio, then select File-&amp;gt;New-&amp;gt;Project. In the New Project dialog, expand the Visual C# node and select Database, then choose SQL Server Project from the list of templates. After the project is created, a dialog named Add Database Reference will ask for the specification of a database connection to work with for this project. This connection is used to deploy the project to SQL Server. Select a connection from the dialog or create a new one by clicking Add New Reference. I'll assume for the brevity of this article that you either have a SQL database or can create a new one and that you can establish a database connection to it. Next, right-click the new SQL Server Project and select Add-&amp;gt;New Item. Choose User-Defined Function from the Add New Item dialog and name it whatever you like; I used the class name RegexUtilities.cs in the test project I wrote for this article. This will add a snippet of code in the class that is a template for the function. Replace the entire contents of the code window with the following: &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System; &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Data; &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Data.SqlClient; &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Data.SqlTypes; &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; System.Text.RegularExpressions; &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;using&lt;/span&gt; Microsoft.SqlServer.Server; &lt;/span&gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;partial&lt;/span&gt; &lt;span style="COLOR: blue"&gt;class&lt;/span&gt; &lt;span style="COLOR: teal"&gt;UserDefinedFunctions&lt;/span&gt; { &lt;/span&gt;&lt;br /&gt;
 &lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;[Microsoft.SqlServer.Server.&lt;span style="COLOR: teal"&gt;SqlFunction&lt;/span&gt;] &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;public&lt;/span&gt; &lt;span style="COLOR: blue"&gt;static&lt;/span&gt; &lt;span style="COLOR: blue"&gt;bool&lt;/span&gt; Match(&lt;span style="COLOR: teal"&gt;SqlChars&lt;/span&gt; searchString, &lt;span style="COLOR: teal"&gt;SqlString&lt;/span&gt; regexPattern) { &lt;/span&gt;&lt;br /&gt;
      &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: teal"&gt;Regex&lt;/span&gt; regex = &lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: teal"&gt;Regex&lt;/span&gt;(regexPattern.Value, &lt;span style="COLOR: teal"&gt;RegexOptions&lt;/span&gt;.Compiled | &lt;span style="COLOR: teal"&gt;RegexOptions&lt;/span&gt;.IgnorePatternWhitespace | &lt;span style="COLOR: teal"&gt;RegexOptions&lt;/span&gt;.Singleline); &lt;/span&gt;&lt;br /&gt;
      &lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;return&lt;/span&gt; regex.IsMatch(&lt;span style="COLOR: blue"&gt;new&lt;/span&gt; &lt;span style="COLOR: blue"&gt;string&lt;/span&gt;(searchString.Value)); &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;} &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The code here is a very simple wrapper around the Regex.IsMatch method and is adapted from the source code supplied by David Banister is his &lt;a href="http://msdn.microsoft.com/msdnmag/issues/07/02/SQLRegex/default.aspx"&gt;original article&lt;/a&gt;. I've cut a couple of things down for simplicity. Two parameters are passed to the method named Match: the string to search and the regex pattern to use in the match. The first line of the method creates a new compiled Regex object from the supplied pattern. The second line of codes returns a Boolean that indicates whether or not the supplied searchString was matched. First build the project with Ctrl+Shift+B and then right-click over the project in the Solution Explorer pane and select Deploy. This deploys the managed function to the SQL server you specified in the database connection for the project. &lt;/p&gt;
&lt;p&gt;Now it's time to test the function out in a query. First, let's take the Social Security Number example and check that everything made it across OK. Open SQL Server Management Studio and connect to your database, then open a New Query window. Now type the following into the new query window: &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;select&lt;/span&gt; dbo&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;Match&lt;span style="COLOR: gray"&gt;(&lt;/span&gt; N&lt;span style="COLOR: red"&gt;'123-45-6789'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; N&lt;span style="COLOR: red"&gt;'^\d{3}-\d{2}-\d{4}$'&lt;/span&gt; &lt;span style="COLOR: gray"&gt;) &lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;You should receive the following error: &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Courier New"&gt;Msg 6263, Level 16, State 1, Line 1 &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 8pt; FONT-FAMILY: Courier New"&gt;Execution of user code in the .NET Framework is disabled. Enable "clr enabled" configuration option. &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;This is because we have not yet enabled a state within SQL server that is disabled by default for security reasons. CLR code obviously wields great power and, as such, the ability to execute that code within the SQL process gives any would be malicious code a very nice process under which to run, and in an authenticated context. To run our example then, we must first instruct SQL server to allow CLR code to execute. This is achieved by running the following SQL within a query window: &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: maroon"&gt;sp_configure&lt;/span&gt; &lt;span style="COLOR: red"&gt;'clr enabled'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; 1 &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;go &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; COLOR: blue; FONT-FAMILY: Courier New"&gt;reconfigure &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;go &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;A confirmation message should indicate that the 'clr enabled' option was changed from 0 to 1. It indicates that RECONFIGURE should be run to install the change, hence the second SQL statement in the snippet above. Now we are ready to execute our Social Security Number example again. This time you should receive a single result for (No column name) with a value of 1. This indicates a match and was reported as a literal with no column name because of the structure of the statement we ran. Change part of the statement's left parameter to be an invalid social security number and re-run the example to see that a 0 is received (indicating false) this time instead. &lt;/p&gt;
&lt;p&gt;Now the real purpose of this article was the use of Regex in a WHERE clause and for this pick a table that contains a string column. Now construct a SQL statement similar to the following: &lt;/p&gt;
&lt;p&gt;&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;select&lt;/span&gt;    dbo&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;Match&lt;span style="COLOR: gray"&gt;(&lt;/span&gt;C&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;[First Name]&lt;span style="COLOR: gray"&gt;,&lt;/span&gt; N&lt;span style="COLOR: red"&gt;'^\w{8}$'&lt;/span&gt;&lt;span style="COLOR: gray"&gt;),&lt;/span&gt; C&lt;span style="COLOR: gray"&gt;.&lt;/span&gt;[First Name] &lt;/span&gt;&lt;br /&gt;
&lt;span style="FONT-SIZE: 10pt; FONT-FAMILY: Courier New"&gt;&lt;span style="COLOR: blue"&gt;from&lt;/span&gt;    C Customer &lt;/span&gt;&lt;/p&gt;
&lt;p&gt;The statement above displays two columns of data, the first containing the result of the regular expression call, the second containing the customer first name. The regular expression in this example has been simplified to match words of eight characters in length (an oversimplification of the actual syntax, I know, but sufficient for the purposes of this article). Any customers with a first name of eight characters in length will be accompanied by a 1 in the left-hand column, the others with a 0. &lt;/p&gt;
&lt;p&gt;That's it for this article. A quick demonstration of how managed functions in SQL Server 2005 can open the power of the .NET class library for use within SQL.&lt;/p&gt;&lt;img src="http://blogs.sftsrc.com/stuart/aggbug/22.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Stuart Thompson</dc:creator>
            <guid>http://blogs.sftsrc.com/stuart/archive/2007/05/04/22.aspx</guid>
            <pubDate>Fri, 04 May 2007 17:52:20 GMT</pubDate>
            <comments>http://blogs.sftsrc.com/stuart/archive/2007/05/04/22.aspx#feedback</comments>
            <slash:comments>12</slash:comments>
            <wfw:commentRss>http://blogs.sftsrc.com/stuart/comments/commentRss/22.aspx</wfw:commentRss>
        </item>
    </channel>
</rss>