<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://scottonwriting.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Scott On Writing.NET : SQL</title><link>http://scottonwriting.net/sowblog/archive/tags/SQL/default.aspx</link><description>Tags: SQL</description><dc:language>en</dc:language><generator>CommunityServer 2007 (Build: 20423.869)</generator><item><title>Searching SQL Server Stored Procedure and Trigger Text</title><link>http://scottonwriting.net/sowblog/archive/2011/09/30/searching-sql-server-stored-procedure-and-trigger-text.aspx</link><pubDate>Fri, 30 Sep 2011 16:16:55 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:196934</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=196934</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2011/09/30/searching-sql-server-stored-procedure-and-trigger-text.aspx#comments</comments><description>&lt;p&gt;While I like to consider myself a web developer, every now and then I have to put on my DBA hat to address some SQL related issue. This little script has saved my butt more than once. It searches the text of triggers, UDFs, stored procedures and views for a particular substring, returning the name and type of those database objects that match.&lt;/p&gt;  &lt;pre class="brush: sql;"&gt;DECLARE @Search varchar(255)
SET @Search='text_to_search'

SELECT DISTINCT
    o.name AS Object_Name,o.type_desc
    FROM sys.sql_modules        m 
        INNER JOIN sys.objects  o ON m.object_id=o.object_id
    WHERE m.definition Like '%'+@Search+'%'
    ORDER BY 2,1&lt;/pre&gt;

&lt;p&gt;The above script is one of many in my “bag of scripts” I’ve collected over the years. This particular gem was snagged from Stackoverflow: &lt;a href="http://stackoverflow.com/questions/674623/how-to-find-a-text-inside-sql-server-procedures-triggers/674872#674872"&gt;How to find a text inside SQL Server procedures / triggers?&lt;/a&gt;&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=196934" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/SQL/default.aspx">SQL</category></item><item><title>Removing Gaps and Duplicates from a Numeric Column in Microsoft SQL Server</title><link>http://scottonwriting.net/sowblog/archive/2011/01/18/removing-gaps-and-duplicates-in-a-numeric-column-in-microsoft-sql-server.aspx</link><pubDate>Tue, 18 Jan 2011 17:35:28 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:176495</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=176495</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2011/01/18/removing-gaps-and-duplicates-in-a-numeric-column-in-microsoft-sql-server.aspx#comments</comments><description>&lt;p&gt;Here’s the scenario: you have a database table with an integral numeric column used for sort order of some other non-identifying purpose. Let’s call this column &lt;strong&gt;SortOrder&lt;/strong&gt;. There are a many rows in this table. Every row &lt;em&gt;should&lt;/em&gt; have a unique, sequentially increasing value in its &lt;strong&gt;SortOrder&lt;/strong&gt; column, but this may not be the case – there may be gaps and/or duplicate values in this column.&lt;/p&gt;  &lt;p&gt;For example, consider a table with the following schema and data:&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="135" align="center"&gt;         &lt;p align="center"&gt;&lt;strong&gt;EmployeeID&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="131"&gt;         &lt;p align="center"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;         &lt;p align="center"&gt;&lt;strong&gt;SortOrder&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;1&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Scott&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;1&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;2&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Jisun&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;8&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;3&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Alice&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;7&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;4&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Sam&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;7&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;5&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Benjamin&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;3&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;6&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Aaron&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;9&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;7&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Alexis&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;4&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;8&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Barney&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;5&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;9&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Jim&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;5&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Note how the &lt;strong&gt;SortOrder &lt;/strong&gt;column has some gaps and duplicates. Ideally, the &lt;strong&gt;SortOrder&lt;/strong&gt; column values for these nine rows would be 1, 2, 3, …, 9, but this isn’t the case. Instead, the current values (in ascending order) are: 1, 3, 4, 5, 5, 7, 7, 8, 9.&lt;/p&gt;  &lt;p&gt;Our task is to take the existing &lt;strong&gt;SortColumn&lt;/strong&gt; values and get them into the ideal format. That is, after our modifications, the table’s data should look like so:&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="135" align="center"&gt;         &lt;p align="center"&gt;&lt;strong&gt;EmployeeID&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="131"&gt;         &lt;p align="center"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;         &lt;p align="center"&gt;&lt;strong&gt;SortOrder&lt;/strong&gt;&lt;/p&gt;       &lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;1&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Scott&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;1&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;2&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Jisun&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;8&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;3&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Alice&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;6&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;4&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Sam&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;7&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;5&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Benjamin&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;2&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;6&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Aaron&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;9&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;7&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Alexis&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;3&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;8&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Barney&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;4&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="136" align="center"&gt;9&lt;/td&gt;        &lt;td valign="top" width="131"&gt;Jim&lt;/td&gt;        &lt;td valign="top" width="132" align="center"&gt;5&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;Note how now there are now no gaps or duplicates in &lt;strong&gt;SortOrder&lt;/strong&gt;.&lt;/p&gt;  &lt;h2&gt;The Solution: Ranking Functions, Multi-Table UPDATE Statements and Common Table Expressions (CTEs)&lt;/h2&gt;  &lt;p&gt;Microsoft SQL Server 2005 &lt;a href="http://www.4guysfromrolla.com/webtech/010406-1.shtml"&gt;added a number of ranking functions&lt;/a&gt; that simplify assigning ranks to query results, such as associating a sequentially increasing row number with each record returned from a query or assigning a rank to each result. For example, the following query – which uses SQL Server’s &lt;strong&gt;&lt;a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx"&gt;ROW_NUMBER() function&lt;/a&gt;&lt;/strong&gt; – returns the records from the &lt;strong&gt;Employees&lt;/strong&gt; table with a sequentially increasing number associated with each record:&lt;/p&gt;  &lt;pre class="brush: sql;"&gt;SELECT Name, 
       SortOrder, 
       ROW_NUMBER() OVER (ORDER BY SortOrder) AS NoGapsNoDupsSortOrder
FROM Employees&lt;/pre&gt;

&lt;p&gt;The above query would return the following results. Note how the data is sorted by &lt;strong&gt;SortOrder&lt;/strong&gt;. There’s also a new, materialized column (&lt;strong&gt;NoGapsNoDupsSortOrder&lt;/strong&gt;) that returns sequentially increasing values.&lt;/p&gt;

&lt;table border="0" cellspacing="0" cellpadding="2" width="403"&gt;&lt;tbody&gt;
    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;
        &lt;p align="center"&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/p&gt;
      &lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;
        &lt;p align="center"&gt;&lt;strong&gt;SortOrder&lt;/strong&gt;&lt;/p&gt;
      &lt;/td&gt;

      &lt;td valign="top" width="207" align="center"&gt;
        &lt;p align="center"&gt;&lt;strong&gt;NoGapsNoDupsSortOrder&lt;/strong&gt;&lt;/p&gt;
      &lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Scott&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;1&lt;/td&gt;

      &lt;td valign="top" width="208" align="center"&gt;1&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Benjamin&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;3&lt;/td&gt;

      &lt;td valign="top" width="209" align="center"&gt;2&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Alexis&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;4&lt;/td&gt;

      &lt;td valign="top" width="210" align="center"&gt;3&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Barney&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;5&lt;/td&gt;

      &lt;td valign="top" width="210" align="center"&gt;4&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Jim&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;5&lt;/td&gt;

      &lt;td valign="top" width="210" align="center"&gt;5&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Alice&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;7&lt;/td&gt;

      &lt;td valign="top" width="210" align="center"&gt;6&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Sam&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;7&lt;/td&gt;

      &lt;td valign="top" width="210" align="center"&gt;7&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Jisun&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;8&lt;/td&gt;

      &lt;td valign="top" width="210" align="center"&gt;8&lt;/td&gt;
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td valign="top" width="102"&gt;Aaron&lt;/td&gt;

      &lt;td valign="top" width="92" align="center"&gt;9&lt;/td&gt;

      &lt;td valign="top" width="210" align="center"&gt;9&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;&lt;/table&gt;

&lt;p&gt;What we need to do now is take the value in &lt;strong&gt;NoGapsNoDupsSortOrder&lt;/strong&gt; and assign it to the &lt;strong&gt;SortOrder&lt;/strong&gt; column. If we had the above results in a separate table we could perform such an &lt;strong&gt;UPDATE&lt;/strong&gt;, as SQL Server makes it possible to update records in one database table with data from another table. (See &lt;a href="http://scottonwriting.net/sowblog/archive/2010/07/13/howto-update-records-in-a-database-table-with-data-from-another-table-ms-sql-server.aspx"&gt;HOWTO: Update Records in a Database Table With Data From Another Table&lt;/a&gt;.) &lt;/p&gt;

&lt;p&gt;While the results in the above grid are not in a table (but are rather the results from a query), the good news is that we can treat those results as if they were results in another table using a &lt;strong&gt;&lt;a href="http://www.4guysfromrolla.com/webtech/071906-1.shtml"&gt;Common Table Expression (CTE)&lt;/a&gt;&lt;/strong&gt;. CTEs, which were introduced in SQL Server 2005, can be thought of as a one-off view; that is, a view that is created, defined, and used in a single SQL statement.&lt;/p&gt;

&lt;p&gt;Putting it all together, we end up with the following &lt;strong&gt;UPDATE&lt;/strong&gt; statement:&lt;/p&gt;

&lt;pre class="brush: sql;"&gt;WITH OrderedResults(EmployeeId, NoGapNoDupSortOrder) AS 
(
    SELECT EmployeeId, 
              ROW_NUMBER() OVER (ORDER BY SortOrder) AS NoGapNoDupSortOrder
    FROM Employees
)
UPDATE Employees
    SET SortOrder = OrderedResults.NoGapNoDupSortOrder
FROM OrderedResults
WHERE Employees.EmployeeId = OrderedResults.EmployeeId AND 
       Employees.SortOrder &amp;lt;&amp;gt; OrderedResults.NoGapNoDupSortOrder&lt;/pre&gt;

&lt;p&gt;The above query starts by defining a CTE named &lt;strong&gt;OrderedResults&lt;/strong&gt; that returns two column values: &lt;strong&gt;EmployeeId&lt;/strong&gt; and &lt;strong&gt;NoGapNoDupSortOrder&lt;/strong&gt;. It then updates the &lt;strong&gt;Employees&lt;/strong&gt; table, setting its &lt;strong&gt;SortOrder&lt;/strong&gt; column value to the &lt;strong&gt;NoGapNoDupSortOrder&lt;/strong&gt; value where the &lt;strong&gt;Employees&lt;/strong&gt; table’s &lt;strong&gt;EmployeeId&lt;/strong&gt; value matches the &lt;strong&gt;OrderedResults&lt;/strong&gt; CTEs &lt;strong&gt;EmployeeId&lt;/strong&gt; value (and where the &lt;strong&gt;SortOrder&lt;/strong&gt; does not equal the &lt;strong&gt;NoGapNoDupSortOrder&lt;/strong&gt;).&lt;/p&gt;

&lt;p&gt;For more information on CTEs, ranked results, and updating one table (&lt;strong&gt;Employees&lt;/strong&gt;) with data from another table or CTE (&lt;strong&gt;OrderedResults&lt;/strong&gt;), check out the following resources:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href="http://www.4guysfromrolla.com/webtech/010406-1.shtml"&gt;Returning Ranked Results with Microsoft SQL Server&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://www.4guysfromrolla.com/webtech/071906-1.shtml"&gt;Common Table Expressions (CTE) in Microsoft SQL Server&lt;/a&gt;&lt;/li&gt;

  &lt;li&gt;&lt;a href="http://scottonwriting.net/sowblog/archive/2010/07/13/howto-update-records-in-a-database-table-with-data-from-another-table-ms-sql-server.aspx"&gt;HOWTO: Update Records in a Database Table With Data From Another Table (MS SQL Server)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Happy Programming!&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=176495" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/SQL/default.aspx">SQL</category></item><item><title>HOWTO: Update Records in a Database Table With Data From Another Table (MS SQL Server)</title><link>http://scottonwriting.net/sowblog/archive/2010/07/13/howto-update-records-in-a-database-table-with-data-from-another-table-ms-sql-server.aspx</link><pubDate>Tue, 13 Jul 2010 04:01:00 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:166719</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=166719</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2010/07/13/howto-update-records-in-a-database-table-with-data-from-another-table-ms-sql-server.aspx#comments</comments><description>
&lt;p&gt;SQL's &lt;b&gt;UPDATE&lt;/b&gt; statement makes it easy to update one or more records in a database table. The most common &lt;b&gt;UPDATE&lt;/b&gt; statement pattern assigns static or parameterized values to one or more columns:&lt;/p&gt;

&lt;pre class="brush: sql"&gt;UPDATE MyTable SET
    Column1 = Value1,
    Column2 = Value2,
    ...
WHERE PrimaryKeyColumn = SomeValue&lt;/pre&gt;
&lt;p&gt;But what if you need to update values in one database table from values in another database table? There are a couple ways this can be done. One way is to use a subquery:&lt;/p&gt;

&lt;pre class="brush: sql"&gt;UPDATE MyTable SET
    Column1 = (SELECT SomeColumn FROM SomeOtherTable WHERE SuchAndSuch = ThisAndThat),
    Column2 = (SELECT SomeOtherColumn FROM SomeOtherTable WHERE SuchAndSuch = ThisAndThat),
    ...
WHERE PrimaryKeyColumn = SomeValue&lt;/pre&gt;

&lt;p&gt;Alternatively, you can use a &lt;b&gt;JOIN&lt;/b&gt; statement in your &lt;b&gt;UPDATE&lt;/b&gt; clause. The precise syntax seems to vary a bit based on the database system. For Microsoft SQL Server, use the following syntax:&lt;/p&gt;

&lt;pre class="brush: sql"&gt;UPDATE MyTable SET
    Column1 = SomeOtherTable.SomeColumn,   
    Column2 = SomeOtherTable.SomeOtherColumn,
    ...
FROM MyTable
    INNER JOIN SomeOtherTable ON
        MyTable.SuchAndSuch = SomeOtherTable.ThisAndThat
WHERE PrimaryKeyColumn = SomeValue&lt;/pre&gt;

&lt;p&gt;Bear in mind that updating data in one table with data from another table may be a sign of bad database design. I've seen developers use these approaches to duplicate data across multiple tables so that it's "easier" or "quicker" to write a query to read the data, but such actions are (usually) folly. Data duplication may make your &lt;b&gt;SELECT&lt;/b&gt; queries easier to write, but you can very easily end up with data integrity issues. &lt;/p&gt;
&lt;p&gt;So when might you need to copy data from one table to another? Consider a help desk application that allows support staff to create support tickets, where each ticket in the system exists as a record in the &lt;b&gt;Tickets&lt;/b&gt; table. A ticket may have dozens of data points. To ease data entry, let's say that administrative users can create "ticket templates," which pre-define the values for a number of ticket data points that are stored in a table named &lt;b&gt;TicketTemplates&lt;/b&gt;. Applying a template to an existing ticket would require that the existing record in the &lt;b&gt;Tickets&lt;/b&gt; table have its non-set data points assigned to the corresponding default values of the selected template. Presume that the &lt;b&gt;Tickets&lt;/b&gt; table has a &lt;b&gt;TicketTemplateId&lt;/b&gt; field that indicates what template is to be applied to the ticket. Once this value has been assigned, the following &lt;b&gt;UPDATE&lt;/b&gt; statement could be executed to copy the template's pre-defined values to the non-assigned data points in the &lt;b&gt;Tickets&lt;/b&gt; table:&lt;/p&gt;


&lt;pre class="brush: sql"&gt;UPDATE Tickets SET
    PriorityId = ISNULL(Tickets.PriorityId, TicketTemplates.PriorityId),
    CategoryId = ISNULL(Tickets.CategoryId, TicketTemplates.CategoryId),
    IsPublic = ISNULL(Tickets.IsPublic, TicketTemplates.IsPublic),
    ...
FROM Tickets
    INNER JOIN TicketTemplates ON
        Tickets.TicketTemplateId = TicketTemplates.TicketTemplateId
WHERE Tickets.TicketId = @TicketId&lt;/pre&gt;

&lt;p&gt;Happy Programming!&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=166719" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/SQL/default.aspx">SQL</category></item></channel></rss>