<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Eric Fickes &#187; database</title>
	<atom:link href="http://ericfickes.com/category/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://ericfickes.com</link>
	<description>Design minded Internet Programmer</description>
	<lastBuildDate>Fri, 28 Oct 2011 04:14:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Use INFORMATION_SCHEMA to find data about your database</title>
		<link>http://ericfickes.com/2011/02/find-out-data-about-your-database-with-system-views/</link>
		<comments>http://ericfickes.com/2011/02/find-out-data-about-your-database-with-system-views/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 21:29:23 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[internets]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[data discovery]]></category>
		<category><![CDATA[db2]]></category>
		<category><![CDATA[information_schema]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[rdmbs]]></category>
		<category><![CDATA[schemata]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[sql-92]]></category>
		<category><![CDATA[SQLSERVER]]></category>
		<category><![CDATA[system tables]]></category>
		<category><![CDATA[system views]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=2062</guid>
		<description><![CDATA[Have you ever been in this situation?  You&#8217;re the new developer for a database powered application and you have to figure out what tables contain a specific column?  Or maybe you&#8217;ve got a legacy database that needs it&#8217;s stored procedures &#8230; <a href="http://ericfickes.com/2011/02/find-out-data-about-your-database-with-system-views/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Have you ever been in this situation?  You&#8217;re the new developer for a database powered application and you have to figure out what tables contain a specific column?  Or maybe you&#8217;ve got a legacy database that needs it&#8217;s stored procedures and user defined functions cleaned up?  Or perhaps you&#8217;ve been assigned a unique database inventory task that requires you to report on the structure of your database, rather than the contents of your database.  Besides cracking open your favorite SQL editor and clicking through your database by hand, you might find the data you need from your database&#8217;s INFORMATION_SCHEMA System View.</p>
<p>All of the RDMS systems I&#8217;ve worked with have built in System Views ( or tables ) that contain data about the structure and contents of your database.  These views are there to help you with non data tasks.  For the sake of this post we&#8217;re only going mention the INFORMATION_SCHEMA, which is the system view I use most in SQL Server, and MySQL.  For your reference, here are links to INFORMATION_SCHEMA docs for four popular RDMS systems.</p>
<ol>
<li>SQL Server &#8211; <a title="SQL Server INFORMATION_SCHEMA" href="http://msdn.microsoft.com/en-us/library/ms186778.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms186778.aspx</a></li>
<li>MySQL &#8211; <a title="MySQL INFORMATION_SCHEMA" href="http://dev.mysql.com/doc/refman/5.5/en/information-schema.html" target="_blank">http://dev.mysql.com/doc/refman/5.5/en/information-schema.html</a></li>
<li>Oracle &#8211; <a title="Oracle INFORMATION_SCHEMA" href="http://download.oracle.com/docs/cd/E17952_01/refman-5.0-en/information-schema.html" target="_blank">http://download.oracle.com/docs/cd/E17952_01/refman-5.0-en/information-schema.html</a></li>
<li>DB2 &#8211; <a title="DB2 INFORMATION_SCHEMA" href="http://www.tar.hu/sqlbible/sqlbible0100.html" target="_blank">http://www.tar.hu/sqlbible/sqlbible0100.html</a></li>
</ol>
<p>If you work with SQL Server, you&#8217;ll be happy to know you can see all of the System Views in Management Studio by default.</p>
<div id="attachment_2065" class="wp-caption aligncenter" style="width: 419px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/AdventureWorks-SystemViews.png" rel="lightbox[2062]"><img class="size-full wp-image-2065" title="AdventureWorks-SystemViews" src="http://ericfickes.com/wp-content/uploads/2011/02/AdventureWorks-SystemViews.png" alt="SQL Server Management Studio shows all System Views" width="409" height="461" /></a><p class="wp-caption-text">System Views in the AdventureWorks database</p></div>
<p>If you work with MySQL, you can see the INFORMATION_SCHEMA database in MySQL Workbench, but you&#8217;ll have to enable it in Preferences first.</p>
<div id="attachment_2066" class="wp-caption aligncenter" style="width: 610px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/mysql-ShowMetadataSchemata.png" rel="lightbox[2062]"><img class="size-medium wp-image-2066   " title="mysql-ShowMetadataSchemata" src="http://ericfickes.com/wp-content/uploads/2011/02/mysql-ShowMetadataSchemata-300x146.png" alt="" width="600" height="293" /></a><p class="wp-caption-text">Edit &gt; Preferences &gt; SQL Editor &gt; Check &#39;Show Metadata Schemata&#39;</p></div>
<p>Now for the MySQL users, turning on the Metadata Schemata is <em>not</em> required in order to query those system tables.  Assuming your user has appropriate permissions, you can always fire up Workbench and fire a query like this.</p>
<pre class="brush: sql; title: ; notranslate">
SELECT *
FROM INFORMATION_SCHEMA.Columns
WHERE Column_Name LIKE '%user%'
</pre>
<div id="attachment_2069" class="wp-caption aligncenter" style="width: 432px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/mysql-has-info-schemas.png" rel="lightbox[2062]"><img class="size-full wp-image-2069" title="mysql-has-info-schemas" src="http://ericfickes.com/wp-content/uploads/2011/02/mysql-has-info-schemas.png" alt="" width="422" height="469" /></a><p class="wp-caption-text">MySQL has INFORMATION_SCHEMA tables too!</p></div>
<p>Now that you know about INFORMATION_SCHEMA, you should be happy to know this is part of the SQL-92 standard.  Which means whatever sql scripts you write in MySQL, will most likely work with SQL Server as well.  So go ahead and start querying your database, you&#8217;ll probably find uses for the INFORMATION_SCHEMA in your daily life really quickly.</p>
<p>I&#8217;m going to assume you&#8217;ve got the general idea here, so here are a few sql scripts that I&#8217;ve used over the years.  The first three are informational queries, and the last three are stored procedures that generate .NET or Coldfusion code based off of a table name.</p>
<p><strong>Find all tables with the column EmployeeID</strong></p>
<pre class="brush: sql; title: ; notranslate">
SELECT	*
FROM	INFORMATION_SCHEMA.COLUMNS
WHERE	COLUMN_NAME LIKE 'EmployeeID'
</pre>
<div id="attachment_2072" class="wp-caption aligncenter" style="width: 478px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/1-which-tables-contain-columnName.png" rel="lightbox[2062]"><img class="size-full wp-image-2072" title="1-which-tables-contain-columnName" src="http://ericfickes.com/wp-content/uploads/2011/02/1-which-tables-contain-columnName.png" alt="" width="468" height="236" /></a><p class="wp-caption-text">Which tables have an EmployeeID column?</p></div>
<p><strong>How many tables have the word employee in the name</strong></p>
<pre class="brush: sql; title: ; notranslate">
SELECT	*
FROM	INFORMATION_SCHEMA.TABLES
WHERE	LOWER(TABLE_NAME) LIKE '%employee%'
</pre>
<div id="attachment_2073" class="wp-caption aligncenter" style="width: 483px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/2-which-tables-are-named.png" rel="lightbox[2062]"><img class="size-full wp-image-2073" title="2-which-tables-are-named" src="http://ericfickes.com/wp-content/uploads/2011/02/2-which-tables-are-named.png" alt="" width="473" height="183" /></a><p class="wp-caption-text">How many %employee% tables are in the DB?</p></div>
<p><strong>How many SPROCs and UDFs does our database contain?</strong></p>
<pre class="brush: sql; title: ; notranslate">
SELECT	s.SPROCs, f.UDFs
FROM
(
	SELECT	COUNT(ROUTINE_NAME) AS 'SPROCs', NULL AS 'UDFs'
	FROM	INFORMATION_SCHEMA.ROUTINES
	WHERE	ROUTINE_TYPE = 'PROCEDURE'
) AS s,
(
	SELECT	NULL AS 'SPROCs', COUNT(ROUTINE_NAME) AS 'UDFs'
	FROM	INFORMATION_SCHEMA.ROUTINES
	WHERE	ROUTINE_TYPE = 'FUNCTION'
) AS f
</pre>
<div id="attachment_2076" class="wp-caption aligncenter" style="width: 159px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/3-how-many-sprocs-udfs.png" rel="lightbox[2062]"><img class="size-full wp-image-2076" title="3-how-many-sprocs-udfs" src="http://ericfickes.com/wp-content/uploads/2011/02/3-how-many-sprocs-udfs.png" alt="" width="149" height="65" /></a><p class="wp-caption-text">How many stored procedures and user defined functions are in the DB?</p></div>
<p>The stored procedures listed below all accept an incoming table name, then reads data from INFORMATION_SCHEMA to generate code for the specified database table.  Instead of listing the raw SQL, I&#8217;m just showing a sample result and allowing you to download the raw SQL sprocs.</p>
<p><strong>Generate C# ADO.NET to VO Stored Procedure</strong></p>
<div id="attachment_2077" class="wp-caption aligncenter" style="width: 583px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/4-create-sproc-generateGetTableCode.png" rel="lightbox[2062]"><img class="size-full wp-image-2077" title="4-create-sproc-generateGetTableCode" src="http://ericfickes.com/wp-content/uploads/2011/02/4-create-sproc-generateGetTableCode.png" alt="" width="573" height="458" /></a><p class="wp-caption-text">Build custom C# ADO.NET to custom VO fillerup code</p></div>
<p><a title="Download 'Generate C# ADO.NET to VO' Stored Procedure" href="http://ericfickes.com/code/INFO_SCHEMA/DotNet-generateGetTableCode.sql" target="_blank">download stored procedure</a></p>
<p><a title="Download 'Generate C# ADO.NET to VO' Stored Procedure" href="http://ericfickes.com/code/INFO_SCHEMA/DotNet-generateGetTableCode.sql" target="_blank"></a><br />
<strong>Generate .NET VO class Stored Procedure</strong></p>
<p><a href="http://ericfickes.com/wp-content/uploads/2011/02/5-create-sproc-generateTableVoClass.png" rel="lightbox[2062]"><img class="aligncenter size-full wp-image-2078" title="5-create-sproc-generateTableVoClass" src="http://ericfickes.com/wp-content/uploads/2011/02/5-create-sproc-generateTableVoClass.png" alt="Generate C# VO class for DB table name" width="315" height="454" /></a></p>
<p><a title="Download 'Generate .NET VO class' stored procedure" href="http://ericfickes.com/code/INFO_SCHEMA/DotNet-generateTableVoClass.sql" target="_blank">download stored procedure</a></p>
<p><strong>Generate Coldfusion CFFunction with CFQUERY Stored Procedure</strong></p>
<div id="attachment_2079" class="wp-caption aligncenter" style="width: 502px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/6-create-sproc-MakeSelectFromDbTable.png" rel="lightbox[2062]"><img class="size-full wp-image-2079" title="6-create-sproc-MakeSelectFromDbTable" src="http://ericfickes.com/wp-content/uploads/2011/02/6-create-sproc-MakeSelectFromDbTable.png" alt="" width="492" height="276" /></a><p class="wp-caption-text">Generate Coldfusion CFFunction with CFQUERY for specified table name</p></div>
<p><a title="Download 'Generate Coldfusion CFFunction with CFQUERY' stored procedure" href="http://ericfickes.com/code/INFO_SCHEMA/Coldfusion-MakeSelectFromDbTable.sql" target="_blank">download stored procedure</a></p>
<p>I use these System Views almost daily, and there are a lot of scenarios where my job would take a lot longer to do if I didn&#8217;t know about this information.  The INFORMATION_SCHEMA also feeds my passion to build code that makes code, just love it.  Hopefully this nickel tour was enough information to help somebody out.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2011/02/find-out-data-about-your-database-with-system-views/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
<enclosure url="http://ericfickes.com/code/INFO_SCHEMA/DotNet-generateGetTableCode.sql" length="17232" type="audio/mpeg" />
		</item>
		<item>
		<title>An aggregate may not appear in the set list of an UPDATE statement</title>
		<link>http://ericfickes.com/2011/02/an-aggregate-may-not-appear-in-the-set-list-of-an-update-statement/</link>
		<comments>http://ericfickes.com/2011/02/an-aggregate-may-not-appear-in-the-set-list-of-an-update-statement/#comments</comments>
		<pubDate>Mon, 21 Feb 2011 06:43:00 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[internets]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1998</guid>
		<description><![CDATA[Ever seen the error &#8220;An aggregate may not appear in the set list of an UPDATE statement&#8221; when working with SQL Server?  I ran into this one recently after trying to put a COUNT in an UPDATE statement.  I was rewriting &#8230; <a href="http://ericfickes.com/2011/02/an-aggregate-may-not-appear-in-the-set-list-of-an-update-statement/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Ever seen the error &#8220;An aggregate may not appear in the set list of an UPDATE statement&#8221; when working with SQL Server?  I ran into this one recently after trying to put a COUNT in an UPDATE statement.  I was rewriting some legacy code to use a stored procedure, and it turned out to be the perfect case for a <a title="Read about Temporary Tables on MSDN" href="http://msdn.microsoft.com/en-us/library/ms177399.aspx" target="_blank">Temporary Table</a>.</p>
<p>Instead of boring you with a work scenario, let&#8217;s take a simpler one that uses the <a title="Download Microsoft's AdventureWorks database from this site so you can play along at home" href="http://msftdbprodsamples.codeplex.com/releases/view/37109" target="_blank">AdventureWorks</a> database.  This example will create a list of sales people, total order count for each person, and store this list a single table variable to be used as the final data table.</p>
<p>Should be three simple steps right?</p>
<h2>1. Create @Table variable</h2>
<pre class="brush: sql; title: ; notranslate">
DECLARE @SalesPeople TABLE
(
  EmployeeID int NOT NULL,
  SalesPersonID int NOT NULL,
  FullName varchar(200) NOT NULL,
  Title varchar(200) NOT NULL,
  sales_count int NULL default 0
)
</pre>
<h2>2. INSERT sales people into @Table</h2>
<pre class="brush: sql; title: ; notranslate">
-- HACKISH : Match SalesPersonID to EmployeeID, and fill @SalesPeople
INSERT INTO @SalesPeople
( EmployeeID, SalesPersonID, FullName, Title )
SELECT	e.EmployeeID, sp.SalesPersonID,
		c.FirstName + ' ' + c.LastName as FullName,
		e.Title
FROM	Sales.SalesPerson sp,
		HumanResources.Employee e,
		Person.Contact c
WHERE	sp.SalesPersonID = e.EmployeeID
AND		e.ContactID = c.ContactID
</pre>
<h2>3. UPDATE @Table with COUNT</h2>
<pre class="brush: sql; title: ; notranslate">
UPDATE	@SalesPeople
SET
	sales_count = COUNT( soh.SalesOrderID )
FROM	@SalesPeople sp, Sales.SalesOrderHeader soh
WHERE EXISTS (
	SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
AND	sp.SalesPersonID = soh.SalesPersonID
</pre>
<div id="attachment_2004" class="wp-caption alignnone" style="width: 775px"><a style="font-weight: normal;" href="http://ericfickes.com/wp-content/uploads/2011/02/aggregate-error.png" rel="lightbox[1998]"><img title="aggregate-error" src="http://ericfickes.com/wp-content/uploads/2011/02/aggregate-error.png" alt="" width="765" height="187" /></a><p class="wp-caption-text">Not COUNT allowed in an UPDATE SET statement</p></div>
<p>The third step is where the original error comes in, so let&#8217;s update this to four steps and see how a Table Variable gets through this.</p>
<h2>1 &amp; 2 &#8211; Repeat from above</h2>
<h2>3. Create Table Variable of order counts</h2>
<pre class="brush: sql; title: ; notranslate">
SELECT	soh.SalesPersonID, COUNT( soh.SalesOrderID ) AS sales_count
INTO	#SalesOrderCounts
FROM	Sales.SalesOrderHeader soh
WHERE EXISTS (
	SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
GROUP BY soh.SalesPersonID
</pre>
<h2>4. Update @Table with order counts</h2>
<pre class="brush: sql; title: ; notranslate">
UPDATE	@SalesPeople
SET		sales_count = tmp.sales_count
FROM	@SalesPeople sp, #SalesOrderCounts tmp
WHERE	sp.SalesPersonID = tmp.SalesPersonID
</pre>
<p>And here&#8217;s the full script from start to finish with the table variable in use.</p>
<pre class="brush: sql; title: ; notranslate">
-- Master table of sales people
DECLARE @SalesPeople TABLE
(
  EmployeeID int NOT NULL,
  SalesPersonID int NOT NULL,
  FullName varchar(200) NOT NULL,
  Title varchar(200) NOT NULL,
  sales_count int NULL default 0
)

-- Match SalesPersonID to EmployeeID, and fill @SalesPeople
INSERT INTO @SalesPeople
( EmployeeID, SalesPersonID, FullName, Title )
SELECT	e.EmployeeID, sp.SalesPersonID,
		c.FirstName + ' ' + c.LastName as FullName,
		e.Title
FROM	Sales.SalesPerson sp, HumanResources.Employee e, Person.Contact c
WHERE	sp.SalesPersonID = e.EmployeeID
AND		e.ContactID = c.ContactID

-- put sales counts into the other kind of #tableVariable
SELECT	soh.SalesPersonID, COUNT( soh.SalesOrderID ) AS sales_count
INTO	#SalesOrderCounts
FROM	Sales.SalesOrderHeader soh
WHERE EXISTS (
	SELECT DISTINCT SalesPersonID FROM @SalesPeople WHERE SalesPersonID = soh.SalesPersonID
)
GROUP BY soh.SalesPersonID

-- Update our master @table with data from #tableVariable
UPDATE	@SalesPeople
SET		sales_count = tmp.sales_count
FROM	@SalesPeople sp, #SalesOrderCounts tmp
WHERE	sp.SalesPersonID = tmp.SalesPersonID

-- dump the results
SELECT	FullName, Title, sales_count
FROM	@SalesPeople

-- cleanup
drop table #SalesOrderCounts
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2011/02/an-aggregate-may-not-appear-in-the-set-list-of-an-update-statement/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Eleven Coldfusion-ish tips from the field</title>
		<link>http://ericfickes.com/2011/02/eleven-coldfusion-ish-tips-from-the-field/</link>
		<comments>http://ericfickes.com/2011/02/eleven-coldfusion-ish-tips-from-the-field/#comments</comments>
		<pubDate>Sun, 20 Feb 2011 08:34:49 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[adobe]]></category>
		<category><![CDATA[coldfusion]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[asc]]></category>
		<category><![CDATA[cfml]]></category>
		<category><![CDATA[cfscript]]></category>
		<category><![CDATA[chr]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[listcontains]]></category>
		<category><![CDATA[listfind]]></category>
		<category><![CDATA[listhasvalue]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[parameterized query]]></category>
		<category><![CDATA[special character]]></category>
		<category><![CDATA[sproc]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1965</guid>
		<description><![CDATA[I&#8217;ve had this running list of Coldfusion tips on my wall for the last few years and it&#8217;s time to get these online.  All of the items in this list came from Coldfusion projects over the last few years, but &#8230; <a href="http://ericfickes.com/2011/02/eleven-coldfusion-ish-tips-from-the-field/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had this running list of Coldfusion tips on my wall for the last few years and it&#8217;s time to get these online.  All of the items in this list came from Coldfusion projects over the last few years, but a good portion of these could easily be considered tips for server programmers.  I definitely run into the same items when programming Asp.NET.</p>
<p>There is no rhyme or reason here, just some things I felt need to be repeated.</p>
<h2>1. <a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_m-r_14.html" target="_blank">PreserveSingleQuotes</a>()</h2>
<p>This one came in really handy on a project requiring large text files to be imported into a MySQL database.  I used Coldfusion to upload and read the files into large INSERT chunks using <a href="http://dev.mysql.com/doc/refman/5.5/en/insert.html" target="_blank">MySQL&#8217;s multi &#8211; row INSERT syntax</a>. Code built the VALUES portion of the SQL, then I just fed the data into a function for insertion.</p>
<pre class="brush: sql; title: ; notranslate">
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
</pre>
<pre class="brush: coldfusion; title: ; notranslate">
	&lt;cfquery name=&quot;insert_data&quot; result=&quot;insert_result&quot; DATASOURCE=&quot;#request.dsn#&quot; USERNAME=&quot;#request.dbuser#&quot; PASSWORD=&quot;#request.dbpswd#&quot;&gt;
	INSERT INTO table
	( column1, column2, column3, column4, column5, column6, column7 )
	VALUES
	#PreserveSingleQuotes( insert_values )#
	&lt;/cfquery&gt;
</pre>
<h2>2. What if GENERATED_KEY doesn&#8217;t work?</h2>
<p>If you&#8217;re using MySQL and the GENERATED_KEY property of your cfquery objects isn&#8217;t populating, you can use <a href="http://dev.mysql.com/doc/refman/5.5/en/information-functions.html#function_last-insert-id" target="_blank">LAST_INSERT_ID</a> instead.</p>
<pre class="brush: sql; title: ; notranslate">SELECT LAST_INSERT_ID();</pre>
<h2>3. Use ArrayAppend when building strings</h2>
<p>Classic performance tuning tip for just about any programming language.  Here I&#8217;ll give a Coldfusion example and keep it dead simple.  If you ever have to concatenate strings in code, user ArrayAppend instead.  Here are two loops that do the same thing.  If you run this code, you should notice loop1 takes forever, and loop2 is smoking fast.</p>
<p><strong>slow&#8230;..</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfscript&gt;
xx				= 100000;
insertString	= &quot;&quot;;

// do the loop
while( xx &gt; 0 ) {
	insertString &amp;= xx &amp; &quot; &quot;;
	xx--;
}

WriteOutput( insertString);
&lt;/cfscript&gt;
</pre>
<p><strong>FAST!</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfscript&gt;
xx				= 1000000;
insertArray		= ArrayNew(1);
// do the loop
while( xx &gt; 0 ) {
	ArrayAppend( insertArray, xx &amp; &quot; &quot; );
	xx--;
}

WriteOutput( ArrayToList( insertArray, &quot; &quot; ) );
ArrayClear( insertArray );
&lt;/cfscript&gt;
</pre>
<h2>4. If CSV, then CHR</h2>
<p>This one is simple, if you find yourself creating CSV or any other text file, use special characters when dealing with single and double quotes, etc.</p>
<ul>
<li>chr(9) = Tab</li>
<li>chr(34) = &#8221; double quote</li>
<li>chr(39) = &#8216; single quote</li>
</ul>
<p>And if you&#8217;re not sure of the correct code for the character you&#8217;re looking to use, just wrap that character in <a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=functions_a-b_25.html" target="_blank">ASC()</a> and <a href="http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7ecf.html" target="_blank">WriteOutput</a> to the page.</p>
<h2>5. Use CFMail with GMail</h2>
<p>This is a no brainer, but with how difficult sending email can be with <em><a title="I love .NET, but it's a pain in the butt sometimes" href="http://www.google.com/search?sourceid=chrome&amp;ie=UTF-8&amp;q=send+email+with+ASP.NET" target="_blank">other languages</a></em>, I&#8217;m mentioning it here.</p>
<p><strong>Application.cfc</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfscript&gt;
	APPLICATION.mail.server		= &quot;smtp.gmail.com&quot;;
	APPLICATION.mail.port		= &quot;465&quot;;
	APPLICATION.mail.ssl		= true;
	APPLICATION.mail.user		= &quot;gmailAccount&quot;;
	APPLICATION.mail.pswd		= &quot;gmailPasssword&quot;;
&lt;/cfscript&gt;
</pre>
<p><strong>Emailer.cfm</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfmail to=&quot;work@ericfickes.com&quot;
		bcc=&quot;&quot;
		from=&quot;web@master.com&quot;
		subject=&quot;sending mail is easy with Coldfusion&quot;
		server=&quot;#application.mail.server#&quot;
		useSSL=&quot;#application.mail.ssl#&quot;
		port=&quot;#application.mail.port#&quot;
		username=&quot;#application.mail.user#&quot;
		password=&quot;#application.mail.pswd#&quot;&gt;
#emailBody#
&lt;/cfmail&gt;
</pre>
<h2>6. CFSCRIPT doesn&#8217;t know NULL?</h2>
<p>Another tip from the land of importing and exporting data.  While working with query objects in CFScript, for some reason I could never accurately detect for NULL values.  I tried all sorts of detection schemes and ended up just writing a hacky fail safe.  Please, if you have a better suggestion for *easy* NULL detection in CFSCript, add it in the comments below.</p>
<p><strong>utils.cfc</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;!---  Simple value getter with try / catch to get around NULL values
This function originated in a script where we always needed a &quot; &quot; even if
the value from the database was null.
---&gt;
&lt;cffunction name=&quot;qryGetString&quot; access=&quot;public&quot; returntype=&quot;string&quot;&gt;

&lt;cfargument name=&quot;data&quot; type=&quot;string&quot;&gt;

&lt;cftry&gt;
	&lt;cfscript&gt;
	return #data# &amp; &quot; &quot;;
	&lt;/cfscript&gt;

	&lt;cfcatch type=&quot;Any&quot;&gt;
		&lt;cfreturn &quot; &quot;/&gt;
	&lt;/cfcatch&gt;
&lt;/cftry&gt;

&lt;/cffunction&gt;
</pre>
<p><strong>Exporter.cfm</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
// largeish loop
tab = chr(9);
for( xx = 1; xx &lt;= queryObj.RecordCount; xx++ )
{
	// start the row
	this_row = 	utils_cfc.qryGetString( queryObj.FirstName[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.MiddleName[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.LastName[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.Suffix[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.MedicalTitle[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.email[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_phone[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_fax[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_name[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_address1[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_address2[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_city[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_state[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.practice_zipcode[xx] ) &amp; tab &amp;
			utils_cfc.qryGetString( queryObj.hospital_affiliation[xx] ) &amp; tab;

	// do stuff with the data
}
</pre>
<h2>7. How I find list items</h2>
<h2><span style="font-size: 16px; color: #444444; line-height: 24px;">Ever notice the different behavior in the Coldfusion ListFind commands?  I ended up writing my own ListHasValue function in order to find exact pattern matching in a list.  I had a list of role ids in a list, and just couldn&#8217;t get the built in functions to tell me when my id was in the list without also matching on other ids.  This one makes sense when you run some code.</span></h2>
<p>The top of this sample as my custom ListHasValue() command, and the lower half does three simple loops counting from 1 to 100, and using ListFind, ListContains, and ListHasValue for number checking against the same list.</p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;!---
	Use this to do an exact pattern check for a value in a list.
	This is useful inside of a loop checking numbers against a number list.

	EX :

	list = &quot;91, 92&quot;

	if you loop from 1 - 100, ListFind and related CF functions will match
	on 1, 2, and 9.  Not just 91 and 92

	Use this when you're looping and you ONLY want to match on 91, or 92

---&gt;
&lt;cffunction name=&quot;ListHasValue&quot; access=&quot;public&quot; returntype=&quot;boolean&quot;&gt;

	&lt;cfargument name=&quot;list&quot; required=&quot;yes&quot; type=&quot;string&quot;&gt;
	&lt;cfargument name=&quot;value&quot; required=&quot;yes&quot; type=&quot;any&quot;&gt;

	&lt;cfscript&gt;
		// clean up to be safe
		list = trim( toString( list ) );

		// check to see if we have a *possible* match
		position = ListContains( list, value ) ;

		if( position &gt; 0 )
		{
			// NOTE : KEEP THE TRIM AND TOSTRING
			found_value = trim( toString( ListGetAt( list, position ) ) );

			if( Compare( value, found_value ) == 0 )
			{
				return true;
			}
		}

		// no match for you!
		return false;
	&lt;/cfscript&gt;

&lt;/cffunction&gt;

&lt;cfscript&gt;
list	= &quot;1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 99&quot;;
xx		= 1;

WriteOutput( &quot;List = &quot; &amp; list &amp; &quot;&lt;hr&gt;&quot;);

while( xx &lt; 100 ) {
	if( ListContains( list, xx ) &gt; 0 )
	{
		WriteOutput( &quot;ListContains found &quot; &amp; xx &amp; &quot;&lt;br&gt;&quot; );
	}
    xx++;
}

WriteOutput(&quot;&lt;hr /&gt;&quot;);

xx = 1;
while( xx &lt; 100 ) {
	// ListFind
	if( ListFind( list, xx ) &gt; 0 )
	{
		WriteOutput( &quot;ListFind found &quot; &amp; xx &amp; &quot;&lt;br&gt;&quot; );
	}
    xx++;
}

WriteOutput(&quot;&lt;hr /&gt;&quot;);

xx = 1;
while( xx &lt; 100 ) {
    // Eric's ListHasValue
	if( ListHasValue( list, xx ) )
	{
		WriteOutput( &quot;ListHasValue found &quot; &amp; xx &amp; &quot;&lt;br&gt;&quot; );
	}
	xx++;
}
&lt;/cfscript&gt;
</pre>
<p>If you run this code on your Coldfusion server, you should notice the following results.  ListContains matches single digits from the loop that do not really exist in the list.  ListFind only finds the number 1?  And finally, my function does exactly what I needed it to do.  Tell me when a specific number exists in a list.</p>
<div id="attachment_1980" class="wp-caption alignnone" style="width: 240px"><a href="http://ericfickes.com/wp-content/uploads/2011/02/cf-list-find1.png" rel="lightbox[1965]"><img class="size-full wp-image-1980" title="cf-list-find" src="http://ericfickes.com/wp-content/uploads/2011/02/cf-list-find1.png" alt="CF ListFind function comparison" width="230" height="559" /></a><p class="wp-caption-text">Coldfusion ListFind functions don&#39;t always behave how I want them to</p></div>
<p><span style="font-size: 16px; color: #444444; line-height: 24px;"><br />
</span></p>
<h2>8. Make PDFs faster</h2>
<p>This could easily be it&#8217;s own topic, but I&#8217;ll say one thing about making PDFs faster with CFDocument.  Only put final content between &lt;cfdocument&gt; and &lt;/cfdocument&gt;.  That is, if you have any processing code, cfqueries, cfloops, inside of your cfdocument tag, your cfml page is running slower than it needs to be.  Here&#8217;s a simple example of one of my cfml pages that has only final content in the cfdocument tags.</p>
<p>The key to this example is moving all of my content creation code into an external file, then including at the top of my page. I always do a check for my main PDF_BODY variable, and then spit out my PDF document.</p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfinclude template=&quot;code/export_pdf_codefile.cfm&quot;&gt;

&lt;cfif PDF_BODY NEQ &quot;&quot;&gt;

    &lt;cfdocument	name=&quot;provider_profile&quot;
                format=&quot;PDF&quot;
                pagetype=&quot;A4&quot;
                mimetype=&quot;application/pdf&quot;
                orientation=&quot;portrait&quot;
                margintop=&quot;0&quot;
                marginbottom=&quot;0.2&quot;
                marginleft=&quot;0.2&quot;
                marginright=&quot;0.2&quot;
                &gt;
        &lt;cfoutput&gt;#PDF_BODY#&lt;/cfoutput&gt;
    &lt;/cfdocument&gt;

    &lt;!--- send directly to client ---&gt;
    &lt;cfheader name=&quot;Content-Disposition&quot; value=&quot;attachment; filename=#filename#&quot;&gt;
    &lt;cfcontent type=&quot;application/pdf&quot; variable=&quot;#provider_profile#&quot;&gt;

&lt;cfelse&gt;
	No PDF content found
&lt;/cfif&gt;
</pre>
<h2>9. Use parameterized queries</h2>
<p>This is a tip for all server side programmers whether you use Coldfusion, ASP, JSP, PBJ.  Use parameterized queries when doing any database interaction.  It&#8217;s too easy not to use, and you get protection from SQL Injection, as well as enforcing proper data types when speaking to your database.  This is something all server programmers should do regardless of your language, the sample below is for Coldfusion.</p>
<p><strong>BAD</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfquery name=&quot;tblInsert&quot; datasource=&quot;myDb&quot;&gt;
INSERT INTO	myTable
( col1, col2, col3, col4 )
VALUES
( '#Form.field1#', '#Form.field2#', '#Form.field3#' )
&lt;/cfquery&gt;
</pre>
<p><strong>GOOD</strong></p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfset val1 = Form.field1&gt;
&lt;cfset val2 = Form.field1&gt;
&lt;cfset val3 = Form.field1&gt;

&lt;cfquery name=&quot;tblInsert&quot; datasource=&quot;myDb&quot;&gt;
INSERT INTO	myTable
( col1, col2, col3, col4 )
VALUES
(
    &lt;cfqueryparam cfsqltype=&quot;cf_sql_varchar&quot; value=&quot;#val1#&quot; /&gt;,
    &lt;cfqueryparam cfsqltype=&quot;cf_sql_varchar&quot; value=&quot;#val2#&quot; /&gt;,
    &lt;cfqueryparam cfsqltype=&quot;cf_sql_varchar&quot; value=&quot;#val3#&quot; /&gt;
)
&lt;/cfquery&gt;
</pre>
<h2>10. Where&#8217;d the time go?</h2>
<p>For the good programmers already using parameterized queries, ever insert a timestamp into your database and find out the date is correct, but the time is always 12:00:00?  Take a closer look at the cfsqltype in your cfqueryparam, I had this exact problem and here&#8217;s what happened.</p>
<p>Using <strong>cf_sql_date does</strong> not include the full date and timestamp, just the date.</p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfqueryparam cfsqltype=&quot;cf_sql_date&quot; value=&quot;#paymentDate#&quot; /&gt;
</pre>
<p>&nbsp;</p>
<p>Using <strong>cf_sql_timestamp</strong> includes the full date and timestamp I was looking for.</p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfqueryparam cfsqltype=&quot;cf_sql_timestamp&quot; value=&quot;#paymentDate#&quot; /&gt;
</pre>
<h2>11. Stored Procedures are a little different</h2>
<p>This last one isn&#8217;t much of a tip, but more of a reminder to myself.  I do so much database work that stored procedures are just queries to me, but not so to Coldfusion and the CFQuery tag.  If you want to get data from a stored procedure, you need to use the <a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_r-s_22.html" target="_blank">CFStoredproc</a> tag.  Here&#8217;s a sample of passing one argument into a stored procedure, and how to get the resulting data.</p>
<pre class="brush: coldfusion; title: ; notranslate">
&lt;cfstoredproc	datasource=&quot;myDb&quot; procedure=&quot;GetDataFaster&quot;&gt;

	&lt;cfprocparam type=&quot;in&quot; cfsqltype=&quot;cf_sql_integer&quot; value=&quot;#inputVar#&quot; /&gt;

	&lt;!--- specify sproc result here, cfstoredproc res != returned recordset ---&gt;
	&lt;cfprocresult name = sprocResult&gt;

&lt;/cfstoredproc&gt;

&lt;cfreturn #sprocResult.ColumnFromQuery#&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2011/02/eleven-coldfusion-ish-tips-from-the-field/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Today I took 30 minutes to save hours</title>
		<link>http://ericfickes.com/2011/01/today-i-took-30-minutes-to-save-hours/</link>
		<comments>http://ericfickes.com/2011/01/today-i-took-30-minutes-to-save-hours/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 04:21:41 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[bat]]></category>
		<category><![CDATA[batch]]></category>
		<category><![CDATA[batch file]]></category>
		<category><![CDATA[cli]]></category>
		<category><![CDATA[cmd]]></category>
		<category><![CDATA[command line]]></category>
		<category><![CDATA[cursor]]></category>
		<category><![CDATA[DECLARE]]></category>
		<category><![CDATA[del]]></category>
		<category><![CDATA[exec]]></category>
		<category><![CDATA[fetch]]></category>
		<category><![CDATA[fetch_status]]></category>
		<category><![CDATA[information_schema]]></category>
		<category><![CDATA[kill]]></category>
		<category><![CDATA[restore]]></category>
		<category><![CDATA[sproc]]></category>
		<category><![CDATA[sqlcmd]]></category>
		<category><![CDATA[sys]]></category>
		<category><![CDATA[tsql]]></category>
		<category><![CDATA[workflow]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1861</guid>
		<description><![CDATA[UPDATE &#8211; 1/6/2011 : Since writing this post I have bundled this entire reset process into a single windows batch file. In addition to restoring my database, I also have to remove temp files from my server, so it was &#8230; <a href="http://ericfickes.com/2011/01/today-i-took-30-minutes-to-save-hours/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong> </strong><strong>UPDATE &#8211; </strong><strong>1/6/2011 : </strong>Since writing this post I have bundled this entire reset process into a single <a title="How to use windows batch files" href="http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/batch.mspx?mfr=true" target="_blank">windows batch file</a>. In addition to restoring my database, I also have to remove temp files from my server, so it was time to go command line with all of this.</p>
<p>Here the contents of my batch file.</p>
<pre class="brush: plain; title: ; notranslate">
### DELETE TEMP FILES
del /q /f /s Z:\_FavoriteClient\_GIT_\webapp\invoices\* /f /s

### RESTORE DATABASE
sqlcmd -S &quot;localhost&quot; -i Z:\_FavoriteClient\_GIT_\sql\restoreBackup.sql
</pre>
<p>To use this in a .bat file, copy and paste the text into Notepad, then save as &#8220;restore.bat&#8221;.  Remember, you will need to wrap your filename.bat with quotes, otherwise Notepad will save the file as filename.bat.txt.</p>
<p>* The contents of restoreBackup.sql is listed below.</p>
<hr style="color: efefef;" size="1" noshade="noshade" />One of my favorite pastimes as a programmer is writing code that writes code, aka workflow automation.  It&#8217;s not something I do on every project, but I&#8217;ve been doing a lot of SQL Server development recently, and I&#8217;ve been having a lot of fun using the <a title="SQL 2008 RS Information Schema Views (Transact-SQL)" href="http://msdn.microsoft.com/en-us/library/ms186778.aspx" target="_blank">INFORMATION_SCHEMA views</a> to <a title="Use TSQL to generate Actionscript VO classes" href="http://ericfickes.posterous.com/tsql-sproc-generatetablevoclass" target="_blank">build VO classes</a>, forms representing tables, or just finding what tables have a specific column.  Today I took a 30 minute trip to the <a title="SQL 2008 SYS tables" href="http://msdn.microsoft.com/en-us/library/ms187406.aspx" target="_blank">SYS side</a> and wrote a tsql script that will save me hours.</p>
<p>The web application I&#8217;m working on is a payment processor made up of five steps.  In order to test I have to setup multiple sales for multiple clients, then log in as three different admin users to push the transactions through the system.  This gets my test transactions to the proper testable state.  The process of setting up testable transactions takes over 30 clicks, and once I hit step 3 of the wizard, my test transactions are completed in a way that I have to re-setup the test data ( 20 GOTO 10 ).</p>
<p>I thought about a handful of options, and ended up going with this solution.</p>
<ol>
<li>Setup all test transactions in web application by hand ( get the data ready )</li>
<li>Take a full database backup ( freeze the data )</li>
<li>Use TSQL script to drop all connections to my app&#8217;s db, then restore the database to the &#8220;testable&#8221; state from step 1 ( reset the DB )</li>
</ol>
<p>Here is my TSQL script</p>
<pre class="brush: sql; title: ; notranslate">
DECLARE @sessID int,
		@dbName varchar(50),
		@userName varchar(50),
		@backupFile varchar(200)

SET @dbName		= 'DA413'	-- your database name
SET @userName	= 'DA413'	-- sql user account to look for
SET @backupFile = 'D:\DB\Backup\DA413.bak' -- path to SQL backup file

-- use a cursor to store all session_ids
DECLARE session_cursor CURSOR
FOR
	SELECT	session_id
	FROM	sys.dm_exec_sessions
	WHERE	original_login_name = @userName

	-- open cursor and grab first row
	OPEN session_cursor
	FETCH NEXT FROM session_cursor INTO @sessID

	-- loop through session_ids
	WHILE @@FETCH_STATUS = 0
	BEGIN

		-- kill it
		-- using EXEC because the sproc kill does not like @variables
		EXEC('kill ' + @sessID)

		-- get the next session_id
		FETCH NEXT FROM session_cursor INTO @sessID
	END

-- cursor cleanup
CLOSE session_cursor
DEALLOCATE session_cursor

-- restore backup
USE master
	RESTORE DATABASE DA413
	FROM DISK = @backupFile
GO
</pre>
<p>A few notes about this script :</p>
<ul>
<li>I only need to disconnect my web application&#8217;s database user, not any user</li>
<li>I&#8217;m using the latest full backup, and not a specific database snapshot</li>
<li>The SPROC <a title="TSQL KILL sproc" href="http://technet.microsoft.com/en-us/library/ms173730.aspx" target="_blank">kill</a> doesn&#8217;t like @variables as input, so use <a title="TSQL EXEC" href="http://msdn.microsoft.com/en-us/library/ms188332.aspx" target="_blank">EXEC</a></li>
<li>Your web server doesn&#8217;t know the user was disconnected, so you&#8217;ll have to log back into your application.</li>
<li>If you use this technique, be sure to RERUN your backup if you add anything to the database ( EX : new table column, stored procedure, etc )</li>
<li>If you&#8217;re not the only person connected to this database, make sure you don&#8217;t disconnect anybody else using the same database name</li>
</ul>
<p>This solution is perfect for me because I have full control over my code, database, and server.  It&#8217;s also great because I can test my application, run a single sql script, and 10 seconds later I can test my application again.  While this solution is perfect for me, it&#8217;s probably best used as reference for others.  However, this technique of rolling back the database could be applied to <em>any</em> software application using SQL Server for it&#8217;s datasource.</p>
<p>Hope this helps somebody.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2011/01/today-i-took-30-minutes-to-save-hours/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use sys.dm_exec_sessions to disconnect SQL user connections</title>
		<link>http://ericfickes.com/2011/01/use-sys-dm_exec_sessions-to-disconnect-sql-user-connections/</link>
		<comments>http://ericfickes.com/2011/01/use-sys-dm_exec_sessions-to-disconnect-sql-user-connections/#comments</comments>
		<pubDate>Tue, 04 Jan 2011 03:18:00 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[internets]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://ericfickes.posterous.com/use-sysdmexecsessions-to-disconnect-sql-user</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 -- init vars DECLARE &#8230; <a href="http://ericfickes.com/2011/01/use-sys-dm_exec_sessions-to-disconnect-sql-user-connections/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><!--[CDATA[</p>
<div class="data type-sql"-->
<table cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>
<pre class="line_numbers"><span id="L1">1</span>
<span id="L2">2</span>
<span id="L3">3</span>
<span id="L4">4</span>
<span id="L5">5</span>
<span id="L6">6</span>
<span id="L7">7</span>
<span id="L8">8</span>
<span id="L9">9</span>
<span id="L10">10</span>
<span id="L11">11</span>
<span id="L12">12</span>
<span id="L13">13</span>
<span id="L14">14</span>
<span id="L15">15</span>
<span id="L16">16</span>
<span id="L17">17</span>
<span id="L18">18</span>
<span id="L19">19</span>
<span id="L20">20</span>
<span id="L21">21</span>
<span id="L22">22</span>
<span id="L23">23</span>
<span id="L24">24</span>
<span id="L25">25</span>
<span id="L26">26</span>
<span id="L27">27</span>
<span id="L28">28</span>
<span id="L29">29</span>
<span id="L30">30</span>
<span id="L31">31</span>
<span id="L32">32</span>
<span id="L33">33</span>
<span id="L34">34</span>
<span id="L35">35</span>
<span id="L36">36</span></pre>
</td>
<td width="100%">
<div class="highlight">
<pre>
<div id="LC1" class="line"><span class="c1">-- init vars</span></div>
<div id="LC2" class="line"><span class="k">DECLARE</span> <span class="o">@</span><span class="n">sessID</span> <span class="nb">int</span><span class="p">,</span></div>
<div id="LC3" class="line">		<span class="o">@</span><span class="n">dbName</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">50</span><span class="p">),</span></div>
<div id="LC4" class="line">		<span class="o">@</span><span class="n">userName</span> <span class="nb">varchar</span><span class="p">(</span><span class="mi">50</span><span class="p">)</span></div>
<div id="LC6" class="line"><span class="k">SET</span> <span class="o">@</span><span class="n">dbName</span>	<span class="o">=</span> <span class="s1">'DA413'</span>	<span class="c1">-- your database name</span></div>
<div id="LC7" class="line"><span class="k">SET</span> <span class="o">@</span><span class="n">userName</span>	<span class="o">=</span> <span class="s1">'DA413'</span>	<span class="c1">-- sql user account to look for</span></div>
<div id="LC10" class="line"><span class="c1">-- use a cursor to store all session_ids</span></div>
<div id="LC11" class="line"><span class="k">DECLARE</span> <span class="n">session_cursor</span> <span class="k">CURSOR</span></div>
<div id="LC12" class="line"><span class="k">FOR</span></div>
<div id="LC13" class="line">	<span class="k">SELECT</span>	<span class="n">session_id</span></div>
<div id="LC14" class="line">	<span class="k">FROM</span>	<span class="n">sys</span><span class="p">.</span><span class="n">dm_exec_sessions</span></div>
<div id="LC15" class="line">	<span class="k">WHERE</span>	<span class="n">original_login_name</span> <span class="o">=</span> <span class="o">@</span><span class="n">userName</span></div>
<div id="LC17" class="line">	<span class="c1">-- open cursor and grab first row</span></div>
<div id="LC18" class="line">	<span class="k">OPEN</span> <span class="n">session_cursor</span></div>
<div id="LC19" class="line">	<span class="k">FETCH</span> <span class="k">NEXT</span> <span class="k">FROM</span> <span class="n">session_cursor</span> <span class="k">INTO</span> <span class="o">@</span><span class="n">sessID</span></div>
<div id="LC21" class="line">	<span class="c1">-- loop through session_ids</span></div>
<div id="LC22" class="line">	<span class="n">WHILE</span> <span class="o">@@</span><span class="n">FETCH_STATUS</span> <span class="o">=</span> <span class="mi">0</span></div>
<div id="LC23" class="line">	<span class="k">BEGIN</span></div>
<div id="LC25" class="line">		<span class="c1">-- kill it</span></div>
<div id="LC26" class="line">		<span class="c1">-- using EXEC because the sproc kill does not like @variables</span></div>
<div id="LC27" class="line">		<span class="k">EXEC</span><span class="p">(</span><span class="s1">'kill '</span> <span class="o">+</span> <span class="o">@</span><span class="n">sessID</span><span class="p">)</span></div>
<div id="LC29" class="line">		<span class="c1">-- get the next session_id</span></div>
<div id="LC30" class="line">		<span class="k">FETCH</span> <span class="k">NEXT</span> <span class="k">FROM</span> <span class="n">session_cursor</span> <span class="k">INTO</span> <span class="o">@</span><span class="n">sessID</span></div>
<div id="LC31" class="line">	<span class="k">END</span></div>
<div id="LC33" class="line"><span class="c1">-- cursor cleanup</span></div>
<div id="LC34" class="line"><span class="k">CLOSE</span> <span class="n">session_cursor</span></div>
<div id="LC35" class="line"><span class="k">DEALLOCATE</span> <span class="n">session_cursor</span></div>
</pre>
</div>
</td>
</tr>
</tbody>
</table>
<p><a href="http://ericfickes.posterous.com/use-sysdmexecsessions-to-disconnect-sql-user">Permalink</a></p>
<p>| <a href="http://ericfickes.posterous.com/use-sysdmexecsessions-to-disconnect-sql-user#comment">Leave a comment</a></p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2011/01/use-sys-dm_exec_sessions-to-disconnect-sql-user-connections/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>TSQL : How to find all database tables ending with an UPPERCASE X</title>
		<link>http://ericfickes.com/2010/12/tsql-how-to-find-all-database-tables-ending-with-an-uppercase-x/</link>
		<comments>http://ericfickes.com/2010/12/tsql-how-to-find-all-database-tables-ending-with-an-uppercase-x/#comments</comments>
		<pubDate>Wed, 08 Dec 2010 20:28:59 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[internets]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[tsql]]></category>

		<guid isPermaLink="false">http://ericfickes.posterous.com/tsql-how-to-find-all-database-tables-ending-w</guid>
		<description><![CDATA[1 2 3 4 5 6 7 8 9 10 -- Give me all tables ending with an UPPERCASE X SELECT table_name, SUBSTRING( table_name, LEN(table_name), 1 ) as &#39;end&#39; FROM INFORMATION_SCHEMA.TABLES -- SELECT ascii(&#39;X&#39;) = 88 -- SELECT ascii(&#39;x&#39;) = &#8230; <a href="http://ericfickes.com/2010/12/tsql-how-to-find-all-database-tables-ending-with-an-uppercase-x/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><![CDATA[
<div class="data type-sql">
<table cellspacing="0" cellpadding="0">
<tr>
<td>
<pre class="line_numbers"><span rel="#L1" id="L1">1</span>
<span rel="#L2" id="L2">2</span>
<span rel="#L3" id="L3">3</span>
<span rel="#L4" id="L4">4</span>
<span rel="#L5" id="L5">5</span>
<span rel="#L6" id="L6">6</span>
<span rel="#L7" id="L7">7</span>
<span rel="#L8" id="L8">8</span>
<span rel="#L9" id="L9">9</span>
<span rel="#L10" id="L10">10</span>
</pre>
</td>
<td width="100%">
<div class="highlight">
<pre />
<div class="line" id="LC1"><span class="c1">-- Give me all tables ending with an UPPERCASE X</span></div>
<div class="line" id="LC2"></div>
<div class="line" id="LC3"><span class="k">SELECT</span>	<span class="k">table_name</span><span class="p">,</span> <span class="k">SUBSTRING</span><span class="p">(</span> <span class="k">table_name</span><span class="p">,</span> <span class="n">LEN</span><span class="p">(</span><span class="k">table_name</span><span class="p">),</span> <span class="mi">1</span> <span class="p">)</span> <span class="k">as</span> <span class="s1">&#39;end&#39;</span></div>
<div class="line" id="LC4"></div>
<div class="line" id="LC5"><span class="k">FROM</span>	<span class="n">INFORMATION_SCHEMA</span><span class="p">.</span><span class="n">TABLES</span></div>
<div class="line" id="LC6"></div>
<div class="line" id="LC7"><span class="c1">-- SELECT ascii(&#39;X&#39;) = 88</span></div>
<div class="line" id="LC8"><span class="c1">-- SELECT ascii(&#39;x&#39;) = 120</span></div>
<div class="line" id="LC9"><span class="k">WHERE</span>	<span class="n">ASCII</span><span class="p">(</span> <span class="k">SUBSTRING</span><span class="p">(</span> <span class="k">table_name</span><span class="p">,</span> <span class="n">LEN</span><span class="p">(</span><span class="k">table_name</span><span class="p">),</span> <span class="mi">1</span> <span class="p">)</span> <span class="p">)</span> <span class="o">=</span> <span class="mi">88</span></div>
<div class="line" id="LC10"></div>
</pre>
</div>
</td>
</tr>
</table></div>
</p>
<p><a href="http://ericfickes.posterous.com/tsql-how-to-find-all-database-tables-ending-w">Permalink</a> </p>
<p>	| <a href="http://ericfickes.posterous.com/tsql-how-to-find-all-database-tables-ending-w#comment">Leave a comment</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/12/tsql-how-to-find-all-database-tables-ending-with-an-uppercase-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Endless Mural wins FWA site of the day</title>
		<link>http://ericfickes.com/2010/11/endless-mural-wins-fwa-site-of-the-day/</link>
		<comments>http://ericfickes.com/2010/11/endless-mural-wins-fwa-site-of-the-day/#comments</comments>
		<pubDate>Sat, 20 Nov 2010 06:55:25 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[art]]></category>
		<category><![CDATA[award]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[cool]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[asp.net]]></category>
		<category><![CDATA[azure]]></category>
		<category><![CDATA[beauty of the web]]></category>
		<category><![CDATA[branden hall]]></category>
		<category><![CDATA[cloud]]></category>
		<category><![CDATA[endlessmural]]></category>
		<category><![CDATA[FixDBLib]]></category>
		<category><![CDATA[generative art]]></category>
		<category><![CDATA[html5]]></category>
		<category><![CDATA[ie9]]></category>
		<category><![CDATA[joshua davis]]></category>
		<category><![CDATA[okapi]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1750</guid>
		<description><![CDATA[I&#8217;m ecstatic to announce the Endless Mural HTML5 project has won the prestigious FWA Site of the day award.  While this isn&#8217;t the first project I&#8217;ve worked on that has won the FWA, it is the first NON-Flash, HTML5 and &#8230; <a href="http://ericfickes.com/2010/11/endless-mural-wins-fwa-site-of-the-day/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div id="attachment_1822" class="wp-caption aligncenter" style="width: 610px"><a title="Endless Mural wins FWA SOTD, and we didn't even use Flash!" href="http://www.thefwa.com/site/the-endless-mural" target="_blank"><img class="size-full wp-image-1822 " style="border: 0px initial initial;" title="Endless Mural &gt; FWA site of the day" src="http://ericfickes.com/wp-content/uploads/2010/11/endlessmural-fwa.png" alt="FWA Site of the day &gt; Nov 22 2010" width="600" height="395" /></a><p class="wp-caption-text">Endless Mural wins FWA for HTML5</p></div>
<p>I&#8217;m ecstatic to announce the Endless Mural HTML5 project has won the prestigious <a title="Endless Mural wins the FWA Site of the day award" href="http://www.thefwa.com/site/the-endless-mural" target="_blank">FWA Site of the day award</a>.  While this isn&#8217;t the first project I&#8217;ve worked on that has <a title="Wiretree.com has kicked out a handful of FWAs.  Because they're that good" href="http://thefwa.com/profile/wiretree" target="_blank">won the FWA</a>, it is the first NON-Flash, HTML5 and ASP.NET project that has.  No Flash, and we still got the FWA site of the day, SWEET!.</p>
<h1>I need a SQL Ninja</h1>
<div>
<p>On Friday July 23rd I got the chance to skateboard with my good buddy and personal hero <a title="Joshua Davis Studios" href="http://joshuadavis.com" target="_blank">Joshua Davis</a>.  I feel lucky being able to say we&#8217;ve actually been skating together for a few years now, but this was certainly my favorite session we&#8217;ve had so far.  We started out at <a title="Broomfield Skatepark pictures on sk8colorado.blogspot.com" href="http://sk8colorado.blogspot.com/2010/07/broomfield-skatepark.html" target="_blank">Broomfield&#8217;s new park</a> because I had to show Josh the new mini bowl.</p>
<div id="attachment_1762" class="wp-caption aligncenter" style="width: 235px"><a href="http://ericfickes.com/wp-content/uploads/2010/11/josh-eric-skate-checkin.jpg" rel="lightbox[1750]"><img class="size-medium wp-image-1762" title="Joshua Davis and Eric Fickes" src="http://ericfickes.com/wp-content/uploads/2010/11/josh-eric-skate-checkin-225x300.jpg" alt="skate or fry" width="225" height="300" /></a><p class="wp-caption-text">Two hot dogs ( http://yfrog.com/n3hs5j )</p></div>
<p>After Broomfield we made a quick stop for lunch, and then on to the Denver Skatepark in downtown.  It was great showing Josh the lines at my hometown skateparks, as well as a few radical maneuvers.</p>
<p>Now that Josh had seen my non-frontside airs, it was time to wrap things up.  As we were saying our goodbyes I decided to ask about work.  Normally I don&#8217;t talk about work at the skatepark, but I was thinking about going indie again, and figured what the heck.</p>
<p>Turns out Josh was about to start a project for <a title="Endless Mural &gt; Part of the IE9 launch project 'Beauty of the Web'" href="http://www.beautyoftheweb.com" target="_blank">Microsoft</a> ( WHAT?!?! ) and he need to &#8220;find a SQL Ninja&#8221; ( DOUBLE WHAT?!?! ).  I&#8217;ve been actively working with MS SQL Server since version 6.5, so I let him know he was looking at his sql ninja.  Josh was interested, but gave me the &#8220;just cause we&#8217;re bros, doesn&#8217;t put you on the team&#8221;.</p>
<h1>HTML5 drawing tool in one night</h1>
<p>Driving home from the skatepark I called my wife super giddy.  &#8221;Honey, I may be going indie sooner than we planned&#8221;.  I gave her the rundown of the potential project Josh and I just spoke about, and let her know I had some homework to do.  That night I went home and built out a distant cousin of the endless mural project.</p>
<div style="clear: both;">
<div id="attachment_1781" class="wp-caption alignleft" style="width: 243px"><a href="http://ericfickes.com/wp-content/uploads/2010/11/efdraw-html5.png" rel="lightbox[1750]"><img class="size-medium wp-image-1781" title="EFDRAW" src="http://ericfickes.com/wp-content/uploads/2010/11/efdraw-html5-233x300.png" alt="You can draw with HTML5" width="233" height="300" /></a><p class="wp-caption-text">HTML5 drawing tool powered by ASPX and MySQL</p></div>
<div id="attachment_1782" class="wp-caption alignright" style="width: 244px"><a href="http://ericfickes.com/wp-content/uploads/2010/11/efdraw-skull.png" rel="lightbox[1750]"><img class="size-medium wp-image-1782" title="draw save and share" src="http://ericfickes.com/wp-content/uploads/2010/11/efdraw-skull-234x300.png" alt="sloppy html5 drawing" width="234" height="300" /></a><p class="wp-caption-text">HTML5 drawing tool powered by ASPX and MySQL</p></div>
</div>
</div>
<p><a title="I built an HTML5 drawing tool overnight" href="http://fickii.com/efdraw/" target="_blank">EFDRAW</a> is a really simple HTML5 drawing tool powered by ASP.NET and MySQL.  It has most of the features of the mural ( draw, save, replay, share ), but this was only a proof of concept.  This was my first dive into HTML5 development, and it&#8217;s pretty sweet.</p>
<p><object id="stU0hSREFIR1FfQFVcU1tYV1dV" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="data" value="http://www.screentoaster.com/swf/STPlayer.swf" /><param name="allowFullScreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="flashvars" value="video=stU0hSREFIR1FfQFVcU1tYV1dV" /><param name="src" value="http://www.screentoaster.com/swf/STPlayer.swf" /><param name="allowfullscreen" value="true" /><embed id="stU0hSREFIR1FfQFVcU1tYV1dV" type="application/x-shockwave-flash" width="425" height="344" src="http://www.screentoaster.com/swf/STPlayer.swf" flashvars="video=stU0hSREFIR1FfQFVcU1tYV1dV" allowscriptaccess="always" allowfullscreen="true" data="http://www.screentoaster.com/swf/STPlayer.swf"></embed></object></p>
<p>If you&#8217;re interested in HTML5 drawing tools, feel free to play around with <a title="I bult an HTML5 drawing tool overnight" href="http://fickii.com/efdraw/" target="_blank">EFDRAW</a>, view source, help yourself.</p>
<h1>There&#8217;s one thing&#8230;. Azure</h1>
<p>A few days after building EFDRAW my band <a href="http://twitter.com/#!/thecompilers/status/20575513102" target="_blank">The Compilers</a> played at Ignite Denver 7.  Right before starting our first set my phone rings and it&#8217;s Josh!  OMG I think, this is either the &#8220;you got it&#8221; or the &#8220;sorry bud, we&#8217;ll skate again&#8221; call.  I decide to take the call even though we were locked and loaded, standing on stage with our gear waiting for the house music to go down.  I answer and it&#8217;s Josh, but not the usual hyperactive Josh I&#8217;m accustomed to.  I ask about the gig and he says &#8220;Well, there&#8217;s one thing.  We have to use Azure&#8221;.</p>
<p>I let him know I&#8217;ve worked with other cloud platforms already, just not Microsoft&#8217;s.  So we talk a little more and Josh passes the phone to Branden so we can talk 0s and 1s.  After talking to Branden &#8220;mega brain&#8221; Hall for a few minutes he asks if I can do this.  I tell him yes, he says yes, I get excited, he gets excited.  Branden passed me back to Josh and I&#8217;m in shock at this point.</p>
<h1>Now the boring stuff</h1>
<p>So that&#8217;s the story of how I landed the Endless Mural gig, now the boring technical details.</p>
<p>The drawing portion of the mural was built by <a title="Branden Hall's personal website" href="http://waxpraxis.org" target="_blank">Branden Hall</a> of <a title="Automata Studios :: Branden Hall's software company" href="http://automatastudios.com" target="_blank">Automata Studios</a>.  I did the backend which is made up of ASP.NET ( C# ), SQL Azure, and Windows Azure.  We&#8217;re using Azure blob storage to save and serve up the PNGs created at the mural.  To access SQL server, I wrote a super lightweight data access library using all native .NET.</p>
<p>For the most part the backend was very much like every other .NET SQL Server project I build, but Azure did introduce a few gotchas.</p>
<ol>
<li>The publishing and management of your cloud site mainly goes through <a title="Windows Azure control panel" href="http://windows.azure.com/" target="_blank">http://windows.azure.com/</a>.</li>
<li>You can deploy your site from Visual Studio which proved to be immensely helpful after my Azure deploy package grew beyond 100 MB.</li>
<li>You can access SQL Azure directly from SQL 2008+ management tools.</li>
<li>You can not FTP single files up to the cloud, only the full ball of wax.</li>
<li>You can still use web.config for configuration storage, but Azure also has it&#8217;s own version of web.config.</li>
<li>If you need to edit your settings after deploying, store those settings in your Azure service config, not web.config</li>
<li>SQL Azure requires all tables to use clustered indexes</li>
<li>SQL Azure has it&#8217;s own TSQL restrictions ( not many, but be aware )</li>
<li>On average, doing a full republish of an Azure site took a full hour.</li>
</ol>
<p>I could probably ramble on and on about Azure, but I&#8217;ll cut it short.  If you happen to have any questions about Azure feel free to hit me up or leave a comment.  I would also like to say that I <em>know</em> Microsoft is and has been actively improving Azure by the day.  The state of Azure today is most likely even better than when we built the mural, so my experiences may not be your own.</p>
<h1 style="font-family: Georgia, 'Bitstream Charter', serif; color: #000000; line-height: 1.5em; font-size: 2.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 20px; margin-left: 0px; font-weight: normal;">The toolbox</h1>
<ul>
<li>Windows Azure SDK</li>
<li>Windows Azure Platform Kit June 2010</li>
<li>Windows Azure Tools for Visual Studio ( v1.2 )</li>
<li>Microsoft Seadragon Ajax library</li>
<li>Microsoft SQL Server 2008 R2</li>
<li>Microsoft SQL Azure</li>
<li>ASP.NET 4 ( C# )</li>
<li>Windows Azure</li>
<li>Azure Storage Explorer</li>
</ul>
<p>Here is the <a title="The Endless Mural toolbox" href="http://automatastudios.com/the-endless-mural-toolbox/" target="_blank">toolbox that Branden used on the client side</a>.</p>
<h1>Hotlinks from the server guy</h1>
<ul>
<li><a title="Developoing and Deploying with SQL Azure" href="http://social.technet.microsoft.com/wiki/contents/articles/developing-and-deploying-with-sql-azure.aspx" target="_blank">Developing and Deploying with SQL Azure</a></li>
<li><a title="SQL Azure T-SQL reference" href="http://msdn.microsoft.com/en-us/library/ee336281.aspx" target="_blank">SQL Azure DB T-SQL reference</a></li>
<li><a title="Azure w/Intellitrace" href="http://blogs.msdn.com/b/jnak/archive/2010/06/07/using-intellitrace-to-debug-windows-azure-cloud-services.aspx" target="_blank">Azure w/Intellitrace</a></li>
<li><a title="How to upload download Page Blobs ( Windows Azure )" href="http://blogs.msdn.com/b/windowsazurestorage/archive/2010/04/11/using-windows-azure-page-blobs-and-how-to-efficiently-upload-and-download-page-blobs.aspx" target="_blank">Using Windows Azure Page Blobs .. how to upload / download blobs</a></li>
<li><a title="Azure Deep Dive : Working with configuration" href="http://azure.snagy.name/blog/?p=176" target="_blank">Windows Azure Deep Dive: Working with Configuration</a></li>
<li><a title="Getting started with Seadragon AJAX" href="http://www.seadragon.com/developer/ajax/getting-started/" target="_blank">Seadragon Ajax &#8211; Getting Started</a></li>
<li><a title="Creating Deep Zoom content" href="http://www.silverlight.net/learn/whitepapers/deep-zoom-tools/" target="_blank">Creating Content : Deep Zoom Tools</a></li>
<li><a title="Deep Zoom Blog" href="http://blogs.msdn.com/b/lutzg/" target="_blank">Deep Zoom Blog</a></li>
<li><a title="How to create a Twitter TWEET button" href="http://dev.twitter.com/pages/tweet_button" target="_blank">Twitter TWEET Button</a></li>
</ul>
<h1>It&#8217;s a wrap</h1>
<p>This project was the most concentrated five weeks I&#8217;ve had in quite some time.  I still wonder if we were only given five weeks because this was an HTML5 project.  Either way, the mural team made some magic and now you can too.  If you&#8217;re like me and just want to doodle, <a href="http://endlessmural.com/" target="_blank">go make some art at the mural</a>.  If you&#8217;re a developer interested in HTML5 and Javascript programming, go check out <a title="OKAPI.js, the javascript behind endlessmural.com" href="http://www.okapijs.org/" target="_blank">the javascript library okapi.js</a> which Branden Hall recently open sourced.</p>
<p>Also be sure to visit the magicians, I mean artists, who made the amazing patterns you see when using the mural.  I&#8217;m a life long doodler, but can&#8217;t art myself out of a paper bag.</p>
<div>
<h2 style="text-align: center;"><a title="Evgeny Kiselev" href="http://www.ekiselev.com" target="_blank">Evgeny Kiselev</a></h2>
<div id="attachment_1839" class="wp-caption aligncenter" style="width: 310px"><a href="http://ericfickes.com/wp-content/uploads/2010/11/evgeny-kiselev.jpg" rel="lightbox[1750]"><img class="size-medium wp-image-1839 " title="evgeny-kiselev" src="http://ericfickes.com/wp-content/uploads/2010/11/evgeny-kiselev-300x158.jpg" alt="Evgeny Kiselev - www.ekiselev.com" width="300" height="158" /></a><p class="wp-caption-text">Evgeny Kiselev - www.ekiselev.com</p></div>
</div>
<div>
<h2 style="text-align: center;"><a title="Guilherme Marconi" href="http://brain.marconi.nu" target="_blank">Guilherme Marconi</a></h2>
<div id="attachment_1840" class="wp-caption aligncenter" style="width: 310px"><a href="http://ericfickes.com/wp-content/uploads/2010/11/guilherme-marconi.jpg" rel="lightbox[1750]"><img class="size-medium wp-image-1840" title="guilherme-marconi" src="http://ericfickes.com/wp-content/uploads/2010/11/guilherme-marconi-300x168.jpg" alt="Guilherme Marconi - brain.marconi.nu" width="300" height="168" /></a><p class="wp-caption-text">Guilherme Marconi - brain.marconi.nu</p></div>
</div>
<div>
<h2 style="text-align: center;"><a title="Joshua Davis" href="http://www.joshuadavis.com" target="_blank">Joshua Davis</a></h2>
<div id="attachment_1841" class="wp-caption aligncenter" style="width: 310px"><a href="http://ericfickes.com/wp-content/uploads/2010/11/joshua-davis.jpg" rel="lightbox[1750]"><img class="size-medium wp-image-1841" title="joshua-davis" src="http://ericfickes.com/wp-content/uploads/2010/11/joshua-davis-300x225.jpg" alt="Joshua Davis - www.joshuadavis.com" width="300" height="225" /></a><p class="wp-caption-text">Joshua Davis - www.joshuadavis.com</p></div>
</div>
<div>
<h2 style="text-align: center;"><a title="Matt Lyon" href="http://www.c8six.com" target="_blank">Matt Lyon</a></h2>
<div id="attachment_1842" class="wp-caption aligncenter" style="width: 287px"><a href="http://ericfickes.com/wp-content/uploads/2010/11/matt-lyon.jpg" rel="lightbox[1750]"><img class="size-medium wp-image-1842" title="matt-lyon" src="http://ericfickes.com/wp-content/uploads/2010/11/matt-lyon-277x300.jpg" alt="Matt Lyon - www.c8six.com" width="277" height="300" /></a><p class="wp-caption-text">Matt Lyon - www.c8six.com</p></div>
<p>And lastly I put up a photo album on Facebook of all my camera phone pictures from the trip.  <a title="camera phone pictures from my trip to SF to launch endlessmural.com" href="http://www.facebook.com/album.php?aid=220650&amp;id=500552652&amp;l=fe921f2bc4" target="_blank">Check out the endlessmural photo album</a>.</p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/11/endless-mural-wins-fwa-site-of-the-day/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Selecting random ids using TOP and a CTE</title>
		<link>http://ericfickes.com/2010/09/selecting-random-ids-using-top-and-a-cte/</link>
		<comments>http://ericfickes.com/2010/09/selecting-random-ids-using-top-and-a-cte/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 05:43:49 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[tsql]]></category>
		<category><![CDATA[common table expression]]></category>
		<category><![CDATA[cte]]></category>
		<category><![CDATA[mssqlserver]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[SQLSERVER]]></category>
		<category><![CDATA[top]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1675</guid>
		<description><![CDATA[While testing visualizations in a Flex application, I needed to do some underlying data cleanup in SQL Server.  One of my tasks was to manually update an entity table and set the status column to one of three possibilities.  Status &#8230; <a href="http://ericfickes.com/2010/09/selecting-random-ids-using-top-and-a-cte/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>While testing visualizations in a Flex application, I needed to do some underlying data cleanup in SQL Server.  One of my tasks was to manually update an entity table and set the status column to one of three possibilities.  Status group A and B both needed to be roughly 20% of my tables total record count, and status group C would be the remaining rows that weren&#8217;t touched by status A or status B.  Oh and there&#8217;s one more thing, the ids in each status group can not be in sequential order, they have to be random.</p>
<p>At first I thought no sweat.  My dataset is still small ( only 2000 rows ), so if we want uber control I could do the math and generate my id lists by hand.  Yes, hand crafting is possible and under a deadline that kind of logic almost makes sense.  However, I already know the table I&#8217;m working with will grow in the future, and I&#8217;ll probably have to do this data update again, so why not do this right?  While playing around with different select statements I had a &#8220;EUREKA!&#8221; moment.  <a title="TSQL's TOP operator on MSDN" href="http://msdn.microsoft.com/en-us/library/ms189463.aspx" target="_blank">SQL Server&#8217;s TOP operator</a> supports PERCENT, not just number.  I couldn&#8217;t believe it.  I use TOP at least once a week and I always forget about TOP PERCENT.  Since I already know how to select random rows via <a title="SQL Server's Common Table Expressions are super helpful" href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" target="_blank">CTE</a>, it  was time to put it all together.</p>
<p>Before giving you the final SQL, here are the important parts to be familiar with.  Also, for the sake of example I&#8217;m using the <a title="Download the AdventureWorks database from Codeplex" href="http://msftdbprodsamples.codeplex.com/releases/view/37109" target="_blank">AdventureWorks database</a> so you can play along at home.</p>
<h2><a title="MSSQL's TOP operator on MSDN" href="http://msdn.microsoft.com/en-us/library/ms189463.aspx" target="_blank">TOP PERCENT</a></h2>
<p>If you just need 50% of the rows in a table, but you&#8217;re not concerned about the sequence returned, you can fire this query.  This will give you a sequential listing of ProductIDs</p>
<pre class="brush: sql; title: ; notranslate">
SELECT TOP 50 PERCENT ProductID
FROM Production.Product
ORDER BY ProductID
</pre>
<p>Which will look something like this.</p>
<p><a href="http://ericfickes.com/wp-content/uploads/2010/09/SELECT-TOP-20-PERCENT.png" rel="lightbox[1675]"><img class="aligncenter size-full wp-image-1682" title="SELECT TOP 20 PERCENT" src="http://ericfickes.com/wp-content/uploads/2010/09/SELECT-TOP-20-PERCENT.png" alt="SQL's TOP operator returns rows sequentially" width="274" height="255" /></a></p>
<h2><a title="A Common Table Expression can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query..." href="http://msdn.microsoft.com/en-us/library/ms190766.aspx" target="_blank">COMMON TABLE EXPRESSION</a></h2>
<p>Now let&#8217;s say you want to randomly pull all rows from a table.  This can be achieved using this CTE.</p>
<pre class="brush: sql; title: ; notranslate">
WITH data( ProductID ) AS (
	SELECT	ProductID
	FROM	Production.Product
)
SELECT	ProductID
FROM	data
ORDER BY NEWID()
</pre>
<p>Which will look like this</p>
<p><a href="http://ericfickes.com/wp-content/uploads/2010/09/SELECT-RANDOM-CTE.png" rel="lightbox[1675]"><img class="aligncenter size-full wp-image-1689" title="SELECT RANDOM DATA using CTE" src="http://ericfickes.com/wp-content/uploads/2010/09/SELECT-RANDOM-CTE.png" alt="Common Table Expressions in SQLSERVER are super helpful" width="271" height="381" /></a></p>
<p>If you&#8217;re looking to <a title="Select random value from preset list - tSQL, CTE" href="http://ericfickes.posterous.com/tsql-select-a-random-value-using-cte" target="_blank">randomly select values from a pre-determined list, see my CTE sample here</a>.</p>
<p>So now that you&#8217;ve seen TOP PERCENT and CTE in action, it&#8217;s time to put these together and solve my initial task of creating randomly selected groups of ids, of a percent size.</p>
<p>RANDOMLY SELECT TOP PERCENT</p>
<p>Putting it all together, here is the query I used to create my first status group.</p>
<pre class="brush: sql; title: ; notranslate">
WITH data( ProductID ) AS (
	SELECT	ProductID
	FROM	Production.Product
)
SELECT TOP 20 PERCENT ProductID
FROM	data
ORDER BY NEWID()
</pre>
<p>Which gives me a dataset that is 20% of all rows in Production.Product, and the ids are in random order.</p>
<p>And there you have it.  Randomly selecting a percent sized data set from a table in SQL Server.  The SQL here is really pretty simple, but for some reason I always forget TOP PERCENT.  I&#8217;m hoping this post will help me remember TOP PERCENT, and maybe even help somebody else with some TSQL.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/09/selecting-random-ids-using-top-and-a-cte/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use SQL to insert a label in front of a DataBound list</title>
		<link>http://ericfickes.com/2010/06/use-sql-to-insert-a-label-in-front-of-a-databound-list/</link>
		<comments>http://ericfickes.com/2010/06/use-sql-to-insert-a-label-in-front-of-a-databound-list/#comments</comments>
		<pubDate>Thu, 10 Jun 2010 02:35:27 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[combobox]]></category>
		<category><![CDATA[DataBind]]></category>
		<category><![CDATA[DataBound]]></category>
		<category><![CDATA[union]]></category>
		<category><![CDATA[viewstate]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1473</guid>
		<description><![CDATA[Here&#8217;s a clever little solution I would like to add to the book of &#8216;get it done&#8217;.  While this particular example uses ASP.NET controls, this concept really applies to any language that supports DataBinding to a control. The base concept &#8230; <a href="http://ericfickes.com/2010/06/use-sql-to-insert-a-label-in-front-of-a-databound-list/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a clever little solution I would like to add to the book of &#8216;get it done&#8217;.  While this particular example uses ASP.NET controls, this concept really applies to any language that supports DataBinding to a control.</p>
<p>The base concept is using your knowledge of SQL&#8217;s UNION operator to add a temp value to the beginning of a list of data from a sql query.  In the past I&#8217;ve done this countless times via code, and recently I didn&#8217;t have the time to do this, so I updated the query to a UNION, and I was good to go.</p>
<p>Now I&#8217;m not selling this as a &#8216;best practice&#8217;, but I do consider this one more reason why it&#8217;s good to know SQL.</p>
<p><strong>Problem</strong> : Using a SQLDataSource to populate a DropDown component, how do you inject a spacer value in position 0?  EX : &#8220;- select value -&#8221;</p>
<p><strong>Solution</strong> : Inject your spacer value in your SelectCommand via sql&#8217;s UNION operator</p>
<h3>ComboBox</h3>
<pre class="brush: csharp; title: ; notranslate">
&lt;asp:DropDownList runat=&quot;server&quot; ID=&quot;meter_manufacturer_dd&quot; DataSourceID=&quot;sql_meterManufacturer&quot; DataTextField=&quot;Manufacturer&quot; /&gt;
</pre>
<h2>DataSource</h2>
<pre class="brush: sql; title: ; notranslate">
&lt;asp:SqlDataSource runat=&quot;server&quot; ID=&quot;sql_meterManufacturer&quot;
    SelectCommand=&quot;
    SELECT '- Choose Manufacturer -' as Manufacturer
    UNION
    SELECT DISTINCT Manufacturer FROM Smart_Meter_DEF&quot;
    /&gt;</pre>
<p>What this solution gets you.</p>
<p>1. Your spacer value shows up in position 0 ( because the first character is &#8211; and not alphanumeric )</p>
<p style="text-align: center;"><a href="http://ericfickes.com/wp-content/uploads/2010/06/dropdown1.png" rel="lightbox[1473]"><img class="size-full wp-image-1474 aligncenter" title="DataBound ComboBox with spacer injected via SQL" src="http://ericfickes.com/wp-content/uploads/2010/06/dropdown1.png" alt="SQL is your friend" width="187" height="180" /></a></p>
<p style="text-align: center;">2. Auto ViewState caching ( EG : going straight .NET solution, .NET handles persisting your dropdown selection between postbacks )</p>
<p style="text-align: center;"><a href="http://ericfickes.com/wp-content/uploads/2010/06/dropdown2.png" rel="lightbox[1473]"><img class="size-full wp-image-1475 aligncenter" title="DataBound ComboBox retains selection between Postbacks" src="http://ericfickes.com/wp-content/uploads/2010/06/dropdown2.png" alt="Injecting a value via SQL eliminates need for custom ViewState handling" width="189" height="185" /></a></p>
<p>* in this sample, the connectionString for the SqlDataSource is set in code.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/06/use-sql-to-insert-a-label-in-front-of-a-databound-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What if you want to PIVOT against a text column?</title>
		<link>http://ericfickes.com/2010/04/what-if-you-want-to-pivot-against-a-text-column/</link>
		<comments>http://ericfickes.com/2010/04/what-if-you-want-to-pivot-against-a-text-column/#comments</comments>
		<pubDate>Sat, 01 May 2010 05:04:27 +0000</pubDate>
		<dc:creator>Eric Fickes</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[tips and tricks]]></category>
		<category><![CDATA[tsql]]></category>
		<category><![CDATA[coalesce]]></category>
		<category><![CDATA[dynamic sql]]></category>
		<category><![CDATA[exec]]></category>
		<category><![CDATA[pivot]]></category>
		<category><![CDATA[quotename]]></category>
		<category><![CDATA[SQLSERVER]]></category>
		<category><![CDATA[table variable]]></category>

		<guid isPermaLink="false">http://ericfickes.com/?p=1435</guid>
		<description><![CDATA[If you&#8217;ve ever worked with or researched SQL Server&#8217;s PIVOT function, you probably noticed most of the samples pivot against an id column.  Typically an int column like EmployeeID, or StoreID.  That&#8217;s fine and dandy, but what happens when you &#8230; <a href="http://ericfickes.com/2010/04/what-if-you-want-to-pivot-against-a-text-column/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve ever worked with or researched SQL Server&#8217;s <a title="Using PIVOT and UNPIVOT functions" href="http://msdn.microsoft.com/en-us/library/ms177410.aspx" target="_blank">PIVOT function</a>, you probably noticed most of the samples pivot against an id column.  Typically an int column like EmployeeID, or StoreID.  That&#8217;s fine and dandy, but what happens when you want to PIVOT against a varchar column?  If you&#8217;ve been in this need you know this is a bit of a task.</p>
<p>I had this need on an app recently and built a little dynamic sql action that does just this.  The example below however, uses the the <a title="DatabaseLog table in the AdventureWorks DB" href="http://msdn.microsoft.com/en-us/library/ms124872.aspx" target="_blank">DatabaseLog</a> table in the <a title="Download AdventureWorks sample databases for free" href="http://msdn.microsoft.com/en-us/library/ms124501(v=SQL.100).aspx" target="_blank">AdventureWorks sample database</a> to return a count of Events logged for each Schema.  Before jumping into the PIVOT, here&#8217;s a simple query that gives you the same information, all Schemas, Events, and Event counts.</p>
<pre class="brush: sql; title: ; notranslate">
SELECT      [Schema], [Event], COUNT( [Event] ) AS 'event_count'
FROM        DatabaseLog
GROUP BY    [Schema], [Event]
ORDER BY    [Schema]
</pre>
<p>Running this query should give you a long result looking something like this.</p>
<p><a href="http://ericfickes.com/wp-content/uploads/2010/04/regular_count_query.png" rel="lightbox[1435]"><img class="aligncenter size-full wp-image-1442" title="Regular COUNT query" src="http://ericfickes.com/wp-content/uploads/2010/04/regular_count_query.png" alt="Data is there, format isn't nice like PIVOT" width="409" height="394" /></a></p>
<p>While this query returns the same information to you, I don&#8217;t like this format as much as using PIVOT.  This query result is long and requires a bit of manipulation to get into a readable format.</p>
<p>Now let&#8217;s have a look at retrieving the same information using the PIVOT function.</p>
<pre class="brush: sql; title: ; notranslate">
/*
Example of a dynamic PIVOT against a varchar column from the Adventureworks database

References :
PIVOT &amp; UNPIVOT function

http://msdn.microsoft.com/en-us/library/ms177410.aspx

AdventureWorks sample Databases

http://msdn.microsoft.com/en-us/library/ms124501(v=SQL.100).aspx

AdventreWorks.DatabaseLog

http://msdn.microsoft.com/en-us/library/ms124872.aspx

*/

USE AdventureWorks

-- populate temp Event table
SELECT DISTINCT [Event] as 'Event'
INTO	#events
FROM	DatabaseLog

-- this var will hold a comma delimited list of [Event]
DECLARE	@eventList nvarchar(max)

-- create a flattened [Event], list for the PIVOT statement
SELECT	@eventList = COALESCE( @eventList + ', ', '') + CAST( QUOTENAME( [Event] ) AS VARCHAR(1000) )
FROM	#events
ORDER BY [Event]

-- drop table var since our data now lives in @eventList
DROP TABLE #events

-- this var will hold the dynamic PIVOT sql
DECLARE @pvt_sql nvarchar(max)

-- NOTE : we're using dynamic sql here because PIVOT
-- does not support sub SELECT in the 'FOR Event IN ( )'
-- part of the query.
-- If we don't use dynamic SQL here, the PIVOT function
-- requires you to hard code each 'Event'
-- Using SELECT * here so the [Event] columns are auto included
SET @pvt_sql = 'SELECT	*
                FROM
                (
                    SELECT	[Event], [Schema]
                    FROM	DatabaseLog
                ) AS data
                PIVOT
                (
                    COUNT( Event )
                    FOR Event IN
                    ( ' + @eventList + ' )
                ) AS pvt'

-- run the query
EXEC sp_executesql @pvt_sql
</pre>
<p>Assuming you have the AdventureWorks database installed on your server, running this sql should give you a result looking something like this.</p>
<div id="attachment_1439" class="wp-caption aligncenter" style="width: 597px"><a href="http://ericfickes.com/wp-content/uploads/2010/04/dynamic_pivot_dblog.png" rel="lightbox[1435]"><img class="size-full wp-image-1439" title="Schema Event counts" src="http://ericfickes.com/wp-content/uploads/2010/04/dynamic_pivot_dblog.png" alt="Dynamic PIVOT on text column Event" width="587" height="143" /></a><p class="wp-caption-text">Show all Schemas and count of each Event type</p></div>
<p>This query result was truncated to fit in this post, but just know the query above creates a column for every Event in the Databaselog table.</p>
<p>A quick explanation of what&#8217;s happening in this sql</p>
<ol>
<li>First you fill a table variable ( #events ) with all Events from DatabaseLog</li>
<li>Next create a comma delimited list of the Events inside of the table variable</li>
<li>Drop the table variable now that we&#8217;ve got our delimited list of Events</li>
<li>Build the PIVOT statement as a string so you can inject the Events list</li>
<li>Fire the dynamic SQL via EXEC</li>
</ol>
<p>Dynamic SQL is something that comes in handy from time to time, but I do my best to only use it if I absolutely have to.  In this case we&#8217;re using it because the PIVOT function does not allow sub SELECT statements.  This is also why we create a specially formatted delimited list of Events prior to building the dynamic sql.</p>
<p>So there you have it, one example of using PIVOT against a varchar column instead of an integer column.  Also, this is a pretty good example of a dynamic PIVOT since it&#8217;s pretty simple.  I hope this makes sense, and if you have any suggestions of better techniques, I&#8217;d love to hear it.</p>
]]></content:encoded>
			<wfw:commentRss>http://ericfickes.com/2010/04/what-if-you-want-to-pivot-against-a-text-column/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

