<?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>SQL Treeo</title>
	<atom:link href="http://www.sqltreeo.com/wp/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sqltreeo.com/wp</link>
	<description>Jakub Dvorak&#039;s blog and stuff</description>
	<lastBuildDate>Thu, 16 May 2013 19:23:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<atom:link rel='hub' href='http://www.sqltreeo.com/wp/?pushpress=hub'/>
		<item>
		<title>SQLTreeo &#8211; Migration of extended properties to local storage</title>
		<link>http://www.sqltreeo.com/wp/sqltreeo-migration-of-extended-properties-to-local-storage/</link>
		<comments>http://www.sqltreeo.com/wp/sqltreeo-migration-of-extended-properties-to-local-storage/#comments</comments>
		<pubDate>Thu, 28 Mar 2013 08:25:38 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[database grouping]]></category>
		<category><![CDATA[folders]]></category>
		<category><![CDATA[sql objects]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[treeo]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1664</guid>
		<description><![CDATA[New SQLTreeo version introduced local storage option when your folders are stored to file system as structure of files and folders. This option was added because many users had issues with storing their folders in extended properties of SQL objects (mostly because of permission issues). Users who need to swap to local file system, migration ...]]></description>
			<content:encoded><![CDATA[<p>New SQLTreeo version introduced local storage option when your folders are stored to file system as structure of files and folders. This option was added because many users had issues with storing their folders in extended properties of SQL objects (mostly because of permission issues). </p>
<p>Users who need to swap to local file system, migration of extended properties to local storage is necessary.<br />
Here is one approach how you can do it:</p>
<ul>
<li>Export your current folders from extended properties to CSV file</li>
<li>Run batch file which creates local storage based on CSV file</li>
</ul>
<h2>Exporting your extended properties in CSV</h2>
<p>Use following script to export extended properties from all databases on your server. It dynamically executes SELECT statement against system tables and inserts results in #allfiles temporary table. All records are retrieved at the end of the scripts and <strong>must be exported as CSV directly from SSMS result grid</strong>. Save this CSV file for next step (as e.g. folders.csv). <strong>Important &#8211; keep your file in 1250/1252 encoding not in UTF-8.</strong></p>
<code class="code">DECLARE @sqlOneDb VARCHAR(1024)
DECLARE @sql VARCHAR(1024)

IF (OBJECT_ID('tempdb..#allfiles') IS NOT NULL)
	DROP TABLE #allfiles

CREATE TABLE #allfiles (foldername VARCHAR(512), filename VARCHAR(512))

--select all objects which have extended property Virtual_Folder (are in some folder) (? sign will be replaced by database name)
SET @sqlOneDb = 'INSERT INTO #allfiles
					SELECT DISTINCT
						''Databases/?/'' + 
						CASE 	
							WHEN type = ''U'' THEN ''Tables''
							WHEN type = ''V'' THEN ''Views''
							WHEN type = ''P'' THEN ''StoredProcedures''
							WHEN type = ''FN'' THEN ''ScalarValuedFunctions''
							WHEN type IN (''TF'',''IF'') THEN ''TableValuedFunctions''
						END + ''/'' + CONVERT (varchar(255), p.value) as ''folder'',
						SCHEMA_NAME(o.schema_id) + ''.'' + o.Name + ''.sqltreeo'' as ''file''
		
					FROM sys.objects o
					JOIN sys.extended_properties p ON p.major_id = o.object_id

					WHERE type in (''U'',''V'',''P'',''IF'',''TF'',''FN'')
						  AND p.Name = ''VirtualFolder''
					UNION ALL	
					SELECT 
						''Databases Virtual Folders/'' + CONVERT(VARCHAR(255), p.Value),
						''?.sqltreeo'' 
					FROM sys.extended_properties p
						WHERE class_desc = ''DATABASE'' AND name = ''VirtualFolder'' '					  					  

SET @sql = 'USE [?]; ' + @sqlOneDb

-- run select for all databases on the server
EXECUTE master.sys.sp_MSforeachdb @sql

-- table is filled with all objects which belongs to any fodler
-- in format &lt;database_name&gt;/&lt;folder_path&gt;/&lt;file_name&gt;.sqltreeo
SELECT * FROM #allfiles</code>
<h2>Running batch file which creates local storage</h2>
<p>Simply save following loop in e.g. import.bat. Please read carefully instructions below script. </p>
<code class="code">for /f &quot;tokens=1,2 delims=;&quot; %%a in (folders.csv) do (
    md &quot;%1\%2\%%a&quot;
	echo. 2&gt;&quot;%1\%2\%%a\%%b&quot;
)</code>
<p>Loop in batch takes all records from &#8220;folders.csv&#8221; (in same path as your batch file), creates folders and empty files which represents SQLTreeo local storage.<br/><br />
<strong>%1</strong> is first batch parameter and represents your desired root for local storage such as &#8220;C:\SQLTreeoStorage&#8221;</br><br />
<strong>%2</strong> is second batch parameter and represents server name for which you&#8217;re creating local storage (which you&#8217;ve exported extended properties from) (e.g. &#8220;DBSERVER&#8221;). In case of local connection use your computer name.</br><br />
Usage of import.bat is following:</p>
<code class="code">import.bat &quot;c:\SQLTreeoStorage&quot; &quot;DBServer&quot;</code>
<p>Now your local storage should be created at entered path (&#8220;C:\SQLTreeoStorage&#8221;). You can go to SSMS menu Tools->SQLTreeo and set this path as root for local storage. After SSMS restart you should see your folders.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/database-grouping/" title="database grouping" rel="tag">database grouping</a>, <a href="http://www.sqltreeo.com/wp/tag/folders/" title="folders" rel="tag">folders</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-objects/" title="sql objects" rel="tag">sql objects</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/treeo/" title="treeo" rel="tag">treeo</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sqltreeo-migration-of-extended-properties-to-local-storage/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>New SQLTreeo Version</title>
		<link>http://www.sqltreeo.com/wp/new-sqltreeo-version/</link>
		<comments>http://www.sqltreeo.com/wp/new-sqltreeo-version/#comments</comments>
		<pubDate>Wed, 20 Mar 2013 17:58:31 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[folder structure]]></category>
		<category><![CDATA[grouping]]></category>
		<category><![CDATA[sql objects]]></category>
		<category><![CDATA[SQLTreeo]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1647</guid>
		<description><![CDATA[We are glad to announce that new SQLTreeo SSMS Add-In was published. List of new features/enhancements are following: Local storage support &#8211; SQLTreeo stored folder structure to extended properties of SQL objects. Now it&#8217;s possible to store folder structure in filesystem. See SSMS Tools->SQLTreeo menu. Few bugs fixed Few performance issues addressed Download new version ...]]></description>
			<content:encoded><![CDATA[<p>We are glad to announce that new SQLTreeo SSMS Add-In was published. </p>
<p>List of new features/enhancements are following:</p>
<ul>
<li><strong>Local storage support</strong> &#8211; SQLTreeo stored folder structure to extended properties of SQL objects. Now it&#8217;s possible to store folder structure in filesystem. See SSMS Tools->SQLTreeo menu.</li>
<li><strong>Few bugs fixed</strong></li>
<li><strong>Few performance issues addressed</strong></li>
</ul>
<p>Download new version <a href="http://www.sqltreeo.com/wp/download-current/">here</a>.<br />
In case of any issues, contact us at our support email.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/database/" title="Database" rel="tag">Database</a>, <a href="http://www.sqltreeo.com/wp/tag/folder-structure/" title="folder structure" rel="tag">folder structure</a>, <a href="http://www.sqltreeo.com/wp/tag/grouping/" title="grouping" rel="tag">grouping</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-objects/" title="sql objects" rel="tag">sql objects</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sqltreeo/" title="SQLTreeo" rel="tag">SQLTreeo</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/new-sqltreeo-version/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>You don&#8217;t like SQLTreeo price</title>
		<link>http://www.sqltreeo.com/wp/you-dont-like-sqltreeo-price/</link>
		<comments>http://www.sqltreeo.com/wp/you-dont-like-sqltreeo-price/#comments</comments>
		<pubDate>Thu, 25 Oct 2012 14:29:35 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[SQLTreeo]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[SSMS Add-In]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1476</guid>
		<description><![CDATA[Today is the first day when we turned SQLTreeo into a commercial product and your emails continue to flood into our mailbox, with the overall message that you don’t like price. We are listening and we want to respond. SQLTreeo is not an entirely business-driven project and we are able to change the price, and ...]]></description>
			<content:encoded><![CDATA[<p>Today is the first day when we turned SQLTreeo into a commercial product and your emails continue to flood into our mailbox, with the overall message that you don’t like price. We are listening and we want to respond. SQLTreeo is not an entirely business-driven project and we are able to change the price, and we have. Check our website for the new one. Thanks for all your feedback and we hope you will like the new pricing.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/download/" title="download" rel="tag">download</a>, <a href="http://www.sqltreeo.com/wp/tag/sqltreeo/" title="SQLTreeo" rel="tag">SQLTreeo</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms-add-in/" title="SSMS Add-In" rel="tag">SSMS Add-In</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/you-dont-like-sqltreeo-price/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>SQLTreeo now supports SQL Server 2012</title>
		<link>http://www.sqltreeo.com/wp/sqltreeo-now-supports-sql-server-2012/</link>
		<comments>http://www.sqltreeo.com/wp/sqltreeo-now-supports-sql-server-2012/#comments</comments>
		<pubDate>Wed, 24 Oct 2012 13:17:32 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[databases]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[folders]]></category>
		<category><![CDATA[grouping]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[SSMS Add-In]]></category>
		<category><![CDATA[treeo]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1465</guid>
		<description><![CDATA[New version of SQLTreeo SSMS Add-In was released. We fixed few bugs and include also one major change &#8211; SQL Server 2012 support. SQLTreeo Add-In also became commercial product which will help us to give more attention to further development and support. We are now working on local storage support which was desperately demanded by ...]]></description>
			<content:encoded><![CDATA[<p>New version of SQLTreeo SSMS Add-In was released. We fixed few bugs and include also one major change &#8211; SQL Server 2012 support. SQLTreeo Add-In also became commercial product which will help us to give more attention to further development and support. We are now working on <a href="http://www.sqltreeo.com/wp/sql-treeo-extended-properties-or-local-file-please-vote/">local storage support</a> which was desperately demanded by almost everybody who used SQLTreeo in the past. However we will still support current way of storage using extended properties. You can download new version <a href="http://www.sqltreeo.com">here</a>.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/databases/" title="databases" rel="tag">databases</a>, <a href="http://www.sqltreeo.com/wp/tag/development/" title="development" rel="tag">development</a>, <a href="http://www.sqltreeo.com/wp/tag/folders/" title="folders" rel="tag">folders</a>, <a href="http://www.sqltreeo.com/wp/tag/grouping/" title="grouping" rel="tag">grouping</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms-add-in/" title="SSMS Add-In" rel="tag">SSMS Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/treeo/" title="treeo" rel="tag">treeo</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sqltreeo-now-supports-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
		<item>
		<title>SQLTreeo resurrection</title>
		<link>http://www.sqltreeo.com/wp/sqltreeo-resurrection/</link>
		<comments>http://www.sqltreeo.com/wp/sqltreeo-resurrection/#comments</comments>
		<pubDate>Fri, 21 Sep 2012 10:42:19 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[SQL Server Management Studio]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[SSMS Add-In]]></category>
		<category><![CDATA[treeo]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1406</guid>
		<description><![CDATA[I would like to inform you that I transferred all activities around SQLTreeo SSMS Add-In to couple of persons who will be running web site and add-in further. This step was inevitable as I have no chance to cultivate it in the future. There are few improvements I highlighted and they should be addressed after ...]]></description>
			<content:encoded><![CDATA[<p>I would like to inform you that I transferred all activities around <a href="http://sqltreeo.com">SQLTreeo SSMS Add-In</a> to couple of persons who will be running web site and add-in further. This step was inevitable as I have no chance to cultivate it in the future. </p>
<p><strong>There are few improvements I highlighted and they should be addressed after handover:</strong></p>
<ul>
<li>Change storage of folder structure from extended properties to local file.</li>
<li>Add SQL Server Management Studio 2012 Support</li>
<li>Add ability to organize SQL Server Agent Jobs and Diagrams in folders</li>
<li>Fix some bugs</li>
</ul>
<p>I hope that there will be more, it will be driven mainly by power of your demand. Contact and other important information are on the <a href="http://www.sqltreeo.com">www.sqltreeo.com</a>.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-management-studio/" title="SQL Server Management Studio" rel="tag">SQL Server Management Studio</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms-add-in/" title="SSMS Add-In" rel="tag">SSMS Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/treeo/" title="treeo" rel="tag">treeo</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sqltreeo-resurrection/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>SQLTreeo is suspended</title>
		<link>http://www.sqltreeo.com/wp/sqltreeo-is-suspended/</link>
		<comments>http://www.sqltreeo.com/wp/sqltreeo-is-suspended/#comments</comments>
		<pubDate>Thu, 12 Jul 2012 20:05:26 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1256</guid>
		<description><![CDATA[Last few month I was really busy and had completely no time for SQLTreeo. This add-in was started being used by quite many people and I have to admit that I couldn&#8217;t give them proper support because I had no energy to solve any issues in the evening after work. I was &#8220;waiting&#8221; few month if situation ...]]></description>
			<content:encoded><![CDATA[<p>Last few month I was really busy and had completely no time for <a href="http://www.sqltreeo.com">SQLTreeo</a>. This add-in was started being used by quite many people and I have to admit that I couldn&#8217;t give them proper support because I had no energy to solve any issues in the evening after work. I was &#8220;waiting&#8221; few month if situation will change but it won&#8217;t.<br />
There are personal reasons behind that (new family addition) and that&#8217;s why I don&#8217;t want to try wait any longer and will suspend SQLTreeo &#8220;officialy&#8221;. So from now on, I will no longer develop or give support to SQLTreeo Add-in. SQLTreeo will be available for download in its current version.</p>
<p><strong>I decided to offer SQLTreeo project for purchase. Whether I will sell SQLTreeo exclusive to someone (source codes,web,domain,etc.) or I will start selling source codes to individuals will depend solely on demand. If you are interested in any form, don&#8217;t hesitate to drop me an email (jakub.dvorak@sqltreeo.com).</strong></p>
<p>Thanks for the support and sorry for wasn&#8217;t being a best helpdesk <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sqltreeo-is-suspended/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>SQL Treeo &#8211; Extended properties or local file &#8211; please vote</title>
		<link>http://www.sqltreeo.com/wp/sql-treeo-extended-properties-or-local-file-please-vote/</link>
		<comments>http://www.sqltreeo.com/wp/sql-treeo-extended-properties-or-local-file-please-vote/#comments</comments>
		<pubDate>Sat, 12 May 2012 13:22:37 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[custom folders]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[treeo]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1249</guid>
		<description><![CDATA[Hi, I was trying to solve SQL Treeo&#8217;s performance issue on slow connection. This posts is also covers feature request of having separate folder structures for each developer (store folder structure in local file). SQL Treeo currently stores object&#8217;s folder name to its extended property named &#8220;VirtualFolder&#8221;. This helps to identify which object belongs to ...]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>I was trying to solve SQL Treeo&#8217;s performance issue on slow connection. This posts is also covers feature request of having separate folder structures for each developer (store folder structure in local file). SQL Treeo currently stores object&#8217;s folder name to its extended property named &#8220;VirtualFolder&#8221;. This helps to identify which object belongs to which folder.</p>
<p>Advantages of this apporach:</p>
<p>- folder integration is completely seamless as it&#8217;s part of database data (extended properties of e.g. stored procedure)<br />
- if it&#8217;s shared database, other developers with SQL Treeo installed have access to same folder structure</p>
<p>Disadvantages:</p>
<p>- it&#8217;s slower on non-local connections. It&#8217;s because of fetching extended properties using fn_listextendedproperty one-by-one. For database with thousands of objects accessed via e.g. VPN, it&#8217;s slow. (fetch all extended properties in batch is possible but not usable within add-in).<br />
- it&#8217;s limit for storing more attributes to folders (such as color, completely own structure etc.)</p>
<p><strong>Based on that, I would suggest to completely get rid of storing folder name to extended properties and store them to local file instead.</strong></p>
<p>Advantages:</p>
<p>- performance, no access to database is needed (probably only EXISTS for objects within folders)<br />
- each user could have its own folder structure<br />
- more flexibility for future enhancements (such as mixing more SQL object types in one folder.)</p>
<p>Disadvantages:</p>
<p>- local file will not be shared with other database developers by default. However this could be solved by storing this file to any source control or placing it to network shared folder.</p>
<p>Please let me know if you agree with new &#8220;local file&#8221; approach.</p>
<p><strong>Use this poll:</strong></p>
<p>Note: There is a poll embedded within this post, please visit the site to participate in this post&#8217;s poll.</p>
<p>Thanks.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/custom-folders/" title="custom folders" rel="tag">custom folders</a>, <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/download/" title="download" rel="tag">download</a>, <a href="http://www.sqltreeo.com/wp/tag/microsoft-sql-server/" title="Microsoft SQL Server" rel="tag">Microsoft SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/productivity/" title="productivity" rel="tag">productivity</a>, <a href="http://www.sqltreeo.com/wp/tag/refactoring/" title="refactoring" rel="tag">refactoring</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/table/" title="table" rel="tag">table</a>, <a href="http://www.sqltreeo.com/wp/tag/treeo/" title="treeo" rel="tag">treeo</a>, <a href="http://www.sqltreeo.com/wp/tag/view/" title="view" rel="tag">view</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-treeo-extended-properties-or-local-file-please-vote/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>RedGate SQLSearch &amp; SQL Treeo friendship</title>
		<link>http://www.sqltreeo.com/wp/redgate-sqlsearch-sql-treeo-friendship/</link>
		<comments>http://www.sqltreeo.com/wp/redgate-sqlsearch-sql-treeo-friendship/#comments</comments>
		<pubDate>Fri, 11 May 2012 08:08:56 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[custom folders]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[download]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[treeo]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1246</guid>
		<description><![CDATA[Hi, one of SQL Treeo users developed clever workaround for SQL Treeo SSMS Add-in to be more compatible with RedGate SQL Search. Enhancement causes that RedGate SQL Search can now jump in folders you created using SQL Treeo Add-in. This allows you to organize your SQL objects in folders and search them using RedGate SQL ...]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>one of SQL Treeo users developed clever workaround for <a href="http://www.sqltreeo.com">SQL Treeo SSMS Add-in</a> to be more compatible with <a href="http://www.redgate.com">RedGate SQL Search</a>. Enhancement causes that RedGate SQL Search can now jump in folders you created using SQL Treeo Add-in. This allows you to organize your SQL objects in folders and search them using RedGate SQL Search.</p>
<p>Patch can be downloaded <a href="http://forum.sqltreeo.com/viewtopic.php?f=5&amp;t=92">here</a>.</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/custom-folders/" title="custom folders" rel="tag">custom folders</a>, <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/download/" title="download" rel="tag">download</a>, <a href="http://www.sqltreeo.com/wp/tag/microsoft-sql-server/" title="Microsoft SQL Server" rel="tag">Microsoft SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/productivity/" title="productivity" rel="tag">productivity</a>, <a href="http://www.sqltreeo.com/wp/tag/refactoring/" title="refactoring" rel="tag">refactoring</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/table/" title="table" rel="tag">table</a>, <a href="http://www.sqltreeo.com/wp/tag/treeo/" title="treeo" rel="tag">treeo</a>, <a href="http://www.sqltreeo.com/wp/tag/view/" title="view" rel="tag">view</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/redgate-sqlsearch-sql-treeo-friendship/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Add-In for searching object in SSMS without touching mouse</title>
		<link>http://www.sqltreeo.com/wp/add-in-for-searching-object-in-ssms-without-touching-mouse/</link>
		<comments>http://www.sqltreeo.com/wp/add-in-for-searching-object-in-ssms-without-touching-mouse/#comments</comments>
		<pubDate>Mon, 07 Nov 2011 08:20:39 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[filter criteria]]></category>
		<category><![CDATA[quick find]]></category>
		<category><![CDATA[search]]></category>
		<category><![CDATA[search window]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[SSMS Add-In]]></category>
		<category><![CDATA[Stored procedure]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1240</guid>
		<description><![CDATA[I was using various &#8220;quick find&#8221; SSMS add-ins which give you ability to get your tables, stored procedures, functions and other objects quicker than by using standard SSMS features. Few of them were very well usable but all of them are built to support also complex searching including schema, source code occurence, object type etc. ...]]></description>
			<content:encoded><![CDATA[<p>I was using various &#8220;quick find&#8221; SSMS add-ins which give you ability to get your tables, stored procedures, functions and other objects quicker than by using standard SSMS features. Few of them were very well usable but all of them are built to support also complex searching including schema, source code occurence, object type etc. etc.  This is basically good feature but if you&#8217;re inside furious T-SQL coding you just want to jump accross objects with quickest way possible &#8211; every filter criteria or detail you must take care of is slowing you down.</p>
<p>This is the ideal situation for me:</p>
<p>1) press e.g. CTRL+T to bring tiny search window<br />
3) enter part of object&#8217;s name<br />
4) choose object and press ENTER to bring ALTER script or open window where this object is already opened</p>
<p>I prepared prototype of add-in which perfectly fulfills my requirements, sample screenshot is here:</p>
<p><a href="http://www.sqltreeo.com/wp/wp-content/uploads/2011/11/quickfind_prototype.png"><img class="size-full wp-image-1241" title="quickfind_prototype" src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/11/quickfind_prototype.png" alt="" width="635" height="351" /></a></p>
<p>Here is also <a title="Quick find SSMS add-in prototype" href="http://vimeo.com/31690352" target="_blank">short video</a> showing some more use cases.</p>
<p>Feel free to comment.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/filter-criteria/" title="filter criteria" rel="tag">filter criteria</a>, <a href="http://www.sqltreeo.com/wp/tag/quick-find/" title="quick find" rel="tag">quick find</a>, <a href="http://www.sqltreeo.com/wp/tag/search/" title="search" rel="tag">search</a>, <a href="http://www.sqltreeo.com/wp/tag/search-window/" title="search window" rel="tag">search window</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms-add-in/" title="SSMS Add-In" rel="tag">SSMS Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/stored-procedure/" title="Stored procedure" rel="tag">Stored procedure</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/add-in-for-searching-object-in-ssms-without-touching-mouse/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>New version of SQL Treeo SSMS productivity add-in was released</title>
		<link>http://www.sqltreeo.com/wp/new-version-of-sql-treeo-ssms-productivity-add-in-was-released/</link>
		<comments>http://www.sqltreeo.com/wp/new-version-of-sql-treeo-ssms-productivity-add-in-was-released/#comments</comments>
		<pubDate>Sun, 06 Nov 2011 20:36:07 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[custom folders]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[treeo]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1231</guid>
		<description><![CDATA[Hi, new, second version of SQL Treeo productivity add-in was released. For little reminder, here are main features of this add-in: Create custom folders in SSMS’s object explorer using drag&#38;drop. It works for databases, stored procedures, functions, views and tables Ctrl+click on object will bring ALTER script Perform bulk operations in explorer-style management dialog – ...]]></description>
			<content:encoded><![CDATA[<p>Hi,</p>
<p>new, second version of SQL Treeo productivity add-in was released. </p>
<p>For little reminder, here are main features of this add-in:</p>
<ul>
<li>Create custom folders in SSMS’s object explorer using drag&amp;drop.  It works for databases, stored procedures, functions, views and tables</li>
<li>Ctrl+click on object will bring ALTER script</li>
<li>Perform bulk operations in explorer-style management dialog – I did  that because while I was refactoring and re-foldering more objects,  drag&amp;drop was not enough</li>
</ul>
<p>See this <a href="http://www.sqltreeo.com/wp/dowload-free-ssms-add-in-to-create-own-folder-for-database-objects/">full post</a> or this <a href="http://vimeo.com/26274818">short video</a> to find how it works.</p>
<p>Since last version I worked especially on <strong>supporting all version of SSMS, fixing major bugs and on adding few more features</strong>. From outside view it seemed I worked with snail speed because first version of my add-in was release 3 month ago. Truth is that I develop this add-in only in my spare time which is very rare and that&#8217;s main reason why I am so slow <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Here is humble change log:</p>
<ul>
<li><strong>SSMS 2005, 2005 Express, 2008, 2008 Express, 2008 R2, 2008 R2 Express and Denali versions are now supported</strong> &#8211; I didn&#8217;t do comprehensive testing of all versions but it &#8220;should&#8221; work.</li>
<li>Add-in caused exceptions when used for some unsupported folders (e.g. maintenance plans). It should be ok now.</li>
<li>Folder names are now case sensitive.</li>
<li>When Offline databases were present, add-in failed badly. Fixed.</li>
<li>Ctrl+Click didn&#8217;t work for triggers . Fixed.</li>
<li>Add-in didn&#8217;t handle apostrophes in names very well. Fixed.</li>
<li>&#8220;Collapse all folders&#8221; feature was added.</li>
<li>Dragging object from object explorer to opened window didn&#8217;t result in pasting text as usual. Fixed.</li>
<li>Support for non-English SSMS versions added.</li>
<li>Slight performance improvement. But SQL Treeo add-in is still meant to be either for local databases or for quick networks.</li>
</ul>
<p>SQL Treeo add-in is <a href="http://www.sqltreeo.com">here for download</a>.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/custom-folders/" title="custom folders" rel="tag">custom folders</a>, <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/microsoft-sql-server/" title="Microsoft SQL Server" rel="tag">Microsoft SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/productivity/" title="productivity" rel="tag">productivity</a>, <a href="http://www.sqltreeo.com/wp/tag/refactoring/" title="refactoring" rel="tag">refactoring</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/table/" title="table" rel="tag">table</a>, <a href="http://www.sqltreeo.com/wp/tag/treeo/" title="treeo" rel="tag">treeo</a>, <a href="http://www.sqltreeo.com/wp/tag/view/" title="view" rel="tag">view</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/new-version-of-sql-treeo-ssms-productivity-add-in-was-released/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Server 2011 &#8220;Denali&#8221;: Four new useful DMV</title>
		<link>http://www.sqltreeo.com/wp/sql-server-2011-denali-four-new-useful-dmv/</link>
		<comments>http://www.sqltreeo.com/wp/sql-server-2011-denali-four-new-useful-dmv/#comments</comments>
		<pubDate>Wed, 14 Sep 2011 05:43:43 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[DMV]]></category>
		<category><![CDATA[listener]]></category>
		<category><![CDATA[management views]]></category>
		<category><![CDATA[operating system]]></category>
		<category><![CDATA[ports]]></category>
		<category><![CDATA[server agent]]></category>
		<category><![CDATA[service pack level]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[sql server 2011]]></category>
		<category><![CDATA[windows registry]]></category>
		<category><![CDATA[windows version]]></category>
		<category><![CDATA[xp reg]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1215</guid>
		<description><![CDATA[SQL Server 2011 &#8220;Denali&#8221; CTP3 introduced couple of new Dynamic Management Views (DMV). However, documentation says that some of them were already introduced in SP1 of SQL Server 2008 R2 but it was only two month ago (July 2011). I believe that these views were developed in new release (&#8220;Denali&#8221;) but were so impactless that ...]]></description>
			<content:encoded><![CDATA[<p><strong>SQL Server 2011</strong> &#8220;Denali&#8221; CTP3 introduced couple of new <strong>Dynamic Management Views (DMV)</strong>. However, documentation says that some of them were already introduced in <a href="http://www.microsoft.com/download/en/details.aspx?id=26727" target="_blank">SP1 of SQL Server 2008 R2</a> but it was only two month ago (July 2011). I believe that these views were developed in new release (&#8220;Denali&#8221;) but were so impactless that they were inserted to the latest SP of 2008 R2. Here are four of them which I find quite useful:</p>
<ol>
<li><strong>sys.dm_os_windows_info<br />
</strong>This DMV displays information about Windows version, Service Pack level and Language of your operating system.<br />
<strong><br />
</strong></li>
<li><strong>sys.dm_server_registry</strong><br />
This is replacement for <strong>xp_reg_read</strong>, which is used for reading SQL Server instance-related Windows registry entries.</li>
<li><strong>sys.dm_server_services<br />
</strong>This is very useful &#8211; it returns state of Windows services related to SQL Server instance. You can find out whether <strong>SQL Server Agent</strong> or <strong>Full Text indexing service</strong> are running.</li>
<li><strong>sys.dm_tcp_listener_states<br />
</strong>You can query which ports SQL Service listens on your server.</li>
</ol>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/dmv/" title="DMV" rel="tag">DMV</a>, <a href="http://www.sqltreeo.com/wp/tag/listener/" title="listener" rel="tag">listener</a>, <a href="http://www.sqltreeo.com/wp/tag/management-views/" title="management views" rel="tag">management views</a>, <a href="http://www.sqltreeo.com/wp/tag/operating-system/" title="operating system" rel="tag">operating system</a>, <a href="http://www.sqltreeo.com/wp/tag/ports/" title="ports" rel="tag">ports</a>, <a href="http://www.sqltreeo.com/wp/tag/server-agent/" title="server agent" rel="tag">server agent</a>, <a href="http://www.sqltreeo.com/wp/tag/service-pack-level/" title="service pack level" rel="tag">service pack level</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-2008-r2/" title="SQL Server 2008 R2" rel="tag">SQL Server 2008 R2</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-2011/" title="sql server 2011" rel="tag">sql server 2011</a>, <a href="http://www.sqltreeo.com/wp/tag/windows-registry/" title="windows registry" rel="tag">windows registry</a>, <a href="http://www.sqltreeo.com/wp/tag/windows-version/" title="windows version" rel="tag">windows version</a>, <a href="http://www.sqltreeo.com/wp/tag/xp-reg/" title="xp reg" rel="tag">xp reg</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-server-2011-denali-four-new-useful-dmv/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Server DMV Views: Getting slow queries within stored procedure</title>
		<link>http://www.sqltreeo.com/wp/sql-server-dmv-views-getting-slow-queries-within-stored-procedure/</link>
		<comments>http://www.sqltreeo.com/wp/sql-server-dmv-views-getting-slow-queries-within-stored-procedure/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 06:53:06 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Average]]></category>
		<category><![CDATA[CAST]]></category>
		<category><![CDATA[COUNT]]></category>
		<category><![CDATA[DMV]]></category>
		<category><![CDATA[duration]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[performance statistics]]></category>
		<category><![CDATA[scalar function]]></category>
		<category><![CDATA[SELECT]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SUBSTRING]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[WHERE]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1162</guid>
		<description><![CDATA[SQL Server DMV Views might be very helpful. Let&#8217;s imagine that you have a couple of nested stored procedures and you find out that it&#8217;s slow. It would be nice if you could discover which single query/ies in your procedures causes that it is slow. Few DMV views can tell you that. Ian W. Stirk ...]]></description>
			<content:encoded><![CDATA[<p><strong>SQL Server DMV Views</strong> might be very helpful. Let&#8217;s imagine that you have a couple of nested stored procedures and you find out that it&#8217;s <em>slow</em>. It would be nice if you could discover which single <strong>query</strong>/ies in your procedures causes that it is <strong>slow</strong>. Few DMV views can tell you that.</p>
<p>Ian W. Stirk wrote whole book about practical use DMV views which is definitely worth to read. I&#8217;ve received comment from Ian to one of <a href="../showing-slowest-part-of-sql-query/">my post</a> recently which contained helpful piece of advice I would like to share here.</p>
<p>Here is link to his book:<br />
<a href="http://www.amazon.com/gp/product/1935182730/ref=as_li_tf_tl?ie=UTF8&#038;tag=sq040-20&#038;linkCode=as2&#038;camp=217145&#038;creative=399373&#038;creativeASIN=1935182730">SQL Server DMVs in Action: Better Queries with Dynamic Management Views</a><img src="http://www.assoc-amazon.com/e/ir?t=sq040-20&#038;l=as2&#038;o=1&#038;a=1935182730&#038;camp=217145&#038;creative=399373" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /></p>
<p>He post me query which is using DMV view to basically order part of your stored procedure&#8217;s source code from slowest to fastest. After some time of using it I found out that it had really great value for me because it discovered things you couldn&#8217;t realize (e.g. that <a href="http://msmvps.com/blogs/robfarley/archive/2009/12/05/dangers-of-begin-and-end.aspx">scalar functions are terribly slow</a>).</p>
<p>Core of the query are two views &#8211; <strong>sys.dm_exec_query_stat</strong> and <strong>sys.dm_exec_sql_text. </strong></p>
<p>Sys.dm_exec_query_stat query contains performance statistics for each query executed on your SQL Server instance. No rocket science. But it contains even offset which can lead you to exact portion of query which is slow &#8211; this means that you can find out statistics for e.g. one SELECT query in your procedure. Sys.dm_exec_sql_text contains source code of executed SQL objects. If you combine those two views together you will find out statistics for each individual part of your stored procedure.</p>
<p>Here is the query:</p>
<pre class="brush: sql; title: ; notranslate">
SELECT

	CAST(total_elapsed_time / 1000000.0 AS DECIMAL(28, 2)) AS [Total Duration (s)]
	, CAST(total_worker_time * 100.0 / total_elapsed_time AS DECIMAL(28, 2)) AS [% CPU]
	, CAST((total_elapsed_time - total_worker_time)* 100.0 /
	total_elapsed_time AS DECIMAL(28, 2)) AS [% Waiting]
	, execution_count
	, CAST(total_elapsed_time / 1000000.0 / execution_count AS DECIMAL(28, 2)) AS [Average Duration (s)]
	, SUBSTRING (qt.text,(qs.statement_start_offset/2) + 1,
	 ((CASE WHEN qs.statement_end_offset = -1
	 THEN LEN(CONVERT(NVARCHAR(MAX), qt.text)) * 2
	 ELSE qs.statement_end_offset
	 END - qs.statement_start_offset)/2) + 1) AS [Individual Query]
	, SUBSTRING(qt.text,1,100) AS [Parent Query]
	, DB_NAME(qt.dbid) AS DatabaseName

FROM sys.dm_exec_query_stats qs
	CROSS APPLY sys.dm_exec_sql_text(qs.sql_handle) as qt
	WHERE total_elapsed_time &gt; 0
	ORDER BY total_elapsed_time DESC
</pre>
<p>There&#8217;s one <em>but</em> &#8211; I found out these DMV views contain all statistics data from start of your SQL Server Instance. I didn&#8217;t find out how to display these statistics only for last query. I always restart SQL Server instance if I don&#8217;t want to get mix of slow queries which are not related to last batch I executed. It&#8217;s dumb but I didn&#8217;t make up better solution. If you know better solution, please share.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/average/" title="Average" rel="tag">Average</a>, <a href="http://www.sqltreeo.com/wp/tag/cast/" title="CAST" rel="tag">CAST</a>, <a href="http://www.sqltreeo.com/wp/tag/count/" title="COUNT" rel="tag">COUNT</a>, <a href="http://www.sqltreeo.com/wp/tag/dmv/" title="DMV" rel="tag">DMV</a>, <a href="http://www.sqltreeo.com/wp/tag/duration/" title="duration" rel="tag">duration</a>, <a href="http://www.sqltreeo.com/wp/tag/microsoft-sql-server/" title="Microsoft SQL Server" rel="tag">Microsoft SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/performance-statistics/" title="performance statistics" rel="tag">performance statistics</a>, <a href="http://www.sqltreeo.com/wp/tag/scalar-function/" title="scalar function" rel="tag">scalar function</a>, <a href="http://www.sqltreeo.com/wp/tag/select/" title="SELECT" rel="tag">SELECT</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/substring/" title="SUBSTRING" rel="tag">SUBSTRING</a>, <a href="http://www.sqltreeo.com/wp/tag/view/" title="view" rel="tag">view</a>, <a href="http://www.sqltreeo.com/wp/tag/where/" title="WHERE" rel="tag">WHERE</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-server-dmv-views-getting-slow-queries-within-stored-procedure/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>SQL Server 2011 &#8220;Denali&#8221;: Give feedback and win nice gifts</title>
		<link>http://www.sqltreeo.com/wp/sql-server-2011-denali-give-feedback-and-win-nice-gifts/</link>
		<comments>http://www.sqltreeo.com/wp/sql-server-2011-denali-give-feedback-and-win-nice-gifts/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 06:18:08 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[feedback]]></category>
		<category><![CDATA[Microsoft Connect]]></category>
		<category><![CDATA[Microsoft SQL Server]]></category>
		<category><![CDATA[sql server 2011]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1154</guid>
		<description><![CDATA[Microsoft SQL Server guys extended periods for Denali&#8217;s feedback on Microsoft Connect. And they motivates with really nice gifts for 30$. All you have to do is to report a bug or make a suggestion till September 10, 2011 and be in 300 of first. Please follow instructions in this post. Tags: denali, feedback, Microsoft ...]]></description>
			<content:encoded><![CDATA[<p>Microsoft SQL Server guys extended periods for Denali&#8217;s feedback on Microsoft Connect. And they motivates with really nice gifts for 30$. All you have to do is to report a bug or make a suggestion till September 10, 2011 and be in 300 of first.</p>
<p>Please follow instructions in this <a href="http://blogs.technet.com/b/dataplatforminsider/archive/2011/08/19/announcing-the-sql-server-code-name-denali-feedback-challenge.aspx">post</a>.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/feedback/" title="feedback" rel="tag">feedback</a>, <a href="http://www.sqltreeo.com/wp/tag/microsoft-connect/" title="Microsoft Connect" rel="tag">Microsoft Connect</a>, <a href="http://www.sqltreeo.com/wp/tag/microsoft-sql-server/" title="Microsoft SQL Server" rel="tag">Microsoft SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-2011/" title="sql server 2011" rel="tag">sql server 2011</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-server-2011-denali-give-feedback-and-win-nice-gifts/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating Facebook page for blog</title>
		<link>http://www.sqltreeo.com/wp/creating-facebook-page-for-blog/</link>
		<comments>http://www.sqltreeo.com/wp/creating-facebook-page-for-blog/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 21:55:55 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[blog]]></category>
		<category><![CDATA[facebook]]></category>
		<category><![CDATA[Facebook features]]></category>
		<category><![CDATA[Facebook-related]]></category>
		<category><![CDATA[FBML]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[Social media]]></category>
		<category><![CDATA[Social network]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1088</guid>
		<description><![CDATA[Few weeks ago I released my SSMS productivity add-in and it has already almost 200 downloads. Responses are quite warm and I am very glad. Many users are also waiting for SQL Server 2005/2008 support (2008 R2 is only supported version now) and I will release it soon. I received few Facebook-related questions (add facebook ...]]></description>
			<content:encoded><![CDATA[<p>Few weeks ago I released my <a href="../../">SSMS productivity add-in</a> and it has already almost 200 downloads. Responses are quite warm and I am very glad. Many users are also waiting for SQL Server 2005/2008 support (2008 R2 is only supported version now) and I will release it soon. I received few Facebook-related questions (add facebook login/comments to my pages, where are your on facebook etc.) so I decided to join Facebook as well. I am quite new on Facebook as person but I decided to create SQL Treeo Facebook page for &#8220;facebook oriented&#8221; users of  my add-in.<br />
<AD><br />
I started with creating &#8220;Page&#8221; on Facebook, it was easy. Than I found application called <a href="http://www.rssgraffiti.com/">RSS-Grafitti</a> which is automatical filling Facebook Wall from given RSS feed. You can easily let fans of your Facebook page know about your blog posts without any human interaction with Facebook. Another handy Facebook application I used was <a href="http://www.facebook.com/apps/application.php?id=4949752878&amp;b">Static FBML</a>. This app enables you to place your own <strong>F</strong>ace<strong>B</strong>ook <strong>M</strong>arkup <strong>L</strong>anguage in your page and create e.g. custom homepage as I did.</p>
<p>Maybe I will like Facebook after all <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Check SQL Treeo on <a href="http://www.facebook.com/pages/SQL-Treeo/167558489985425">Facebook</a>.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/blog/" title="blog" rel="tag">blog</a>, <a href="http://www.sqltreeo.com/wp/tag/facebook/" title="facebook" rel="tag">facebook</a>, <a href="http://www.sqltreeo.com/wp/tag/facebook-features/" title="Facebook features" rel="tag">Facebook features</a>, <a href="http://www.sqltreeo.com/wp/tag/facebook-related/" title="Facebook-related" rel="tag">Facebook-related</a>, <a href="http://www.sqltreeo.com/wp/tag/fbml/" title="FBML" rel="tag">FBML</a>, <a href="http://www.sqltreeo.com/wp/tag/productivity/" title="productivity" rel="tag">productivity</a>, <a href="http://www.sqltreeo.com/wp/tag/rss/" title="RSS" rel="tag">RSS</a>, <a href="http://www.sqltreeo.com/wp/tag/social-media/" title="Social media" rel="tag">Social media</a>, <a href="http://www.sqltreeo.com/wp/tag/social-network/" title="Social network" rel="tag">Social network</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/creating-facebook-page-for-blog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to write data from user-defined function</title>
		<link>http://www.sqltreeo.com/wp/how-to-write-data-from-user-defined-function/</link>
		<comments>http://www.sqltreeo.com/wp/how-to-write-data-from-user-defined-function/#comments</comments>
		<pubDate>Sun, 28 Aug 2011 15:16:59 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[access permissions]]></category>
		<category><![CDATA[AppendText]]></category>
		<category><![CDATA[AUTHORIZATION]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[CALLER]]></category>
		<category><![CDATA[CLR]]></category>
		<category><![CDATA[CLR UDF]]></category>
		<category><![CDATA[csharpcode]]></category>
		<category><![CDATA[EXECUTE]]></category>
		<category><![CDATA[EXTERNAL]]></category>
		<category><![CDATA[OAxxxx]]></category>
		<category><![CDATA[OLE-Automation]]></category>
		<category><![CDATA[PERMISSION]]></category>
		<category><![CDATA[Register assembly]]></category>
		<category><![CDATA[security changes]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SqlFunction]]></category>
		<category><![CDATA[SqlServer]]></category>
		<category><![CDATA[trustworthy]]></category>
		<category><![CDATA[udf]]></category>
		<category><![CDATA[writeline]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=1055</guid>
		<description><![CDATA[Writing data from within user-defined function is not permitted. You cannot use any INSERT/UPDATE/DELETE/MERGE statement because they&#8217;re &#8220;side-effecting&#8221; for SQL Server. My colleague recently needed to log something from within function so we were searching for solution how to do that with least work. There is one option to call sp_OAxxxx extended stored procedures from ...]]></description>
			<content:encoded><![CDATA[<p>Writing data from within user-defined function is not permitted. You cannot use any INSERT/UPDATE/DELETE/MERGE statement because they&#8217;re &#8220;side-effecting&#8221; for SQL Server. My colleague recently needed to log something from within function so we were searching for solution how to do that with least work. There is one option to call sp_OAxxxx extended stored procedures from within function to write anything to debug file.  It <a href="http://sqlsolace.blogspot.com/2009/09/sql-server-blocked-access-to-procedure.html">can be done</a> but performance is horrible even for debug purposes because OA procedures use OLE-Automation model to create COM objects (and call their methods to write to file).</p>
<p>Much better solution is to develop simple SQL CLR UDF in C# to write to file for you.</p>
<p>1) Create CLR UDF in C#:</p>
<pre class="csharpcode">
<span class="kwrd">using</span> System.IO;
<span class="kwrd">using</span> Microsoft.SqlServer.Server;

<span class="kwrd">public</span> <span class="kwrd">class</span> Debug
{
    [SqlFunction]
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">int</span> ToFile(<span class="kwrd">string</span> fileName, <span class="kwrd">string</span> text)
    {
        <span class="kwrd">return</span> Write(fileName, text);
    }

    <span class="kwrd">private</span> <span class="kwrd">static</span> <span class="kwrd">int</span> Write (<span class="kwrd">string</span> fileName, <span class="kwrd">string</span> line)
    {
        <span class="kwrd">if</span> (File.Exists(fileName))
        {
            <span class="kwrd">using</span> (var sw = File.AppendText(fileName))
            {
                sw.WriteLine(line);

                <span class="kwrd">return</span> 0;
            }
        }

        <span class="kwrd">return</span> -1;
    }  
}</pre>
<p><AD><br />
2) I will not go deep into step of registering this function in SQL Server because it was described many times on <a href="http://blog.netnerds.net/2007/02/sql-server-2005-creating-your-first-c-clr-udf-in-10-easy-steps-one-of-which-includes-partying/">many places</a>. However, this function is manipulating with files, some security changes must be done to your database. In short, you need to set your database trustworthy, register assembly in SQL Server (with external access permissions) and create function.</p>
<pre class="csharpcode">
<span class="rem">-- set your database trustworthy because of </span>
<span class="rem">-- need for external access</span>
<span class="kwrd">ALTER</span> <span class="kwrd">DATABASE</span> &lt;your_database>
    <span class="kwrd">SET</span> TRUSTWORTHY <span class="kwrd">ON</span>

<span class="kwrd">GO</span>

<span class="rem">-- Register assembly to SQL Server</span>
<span class="kwrd">CREATE</span> ASSEMBLY <your_assembly_name>
<span class="kwrd">AUTHORIZATION</span> [dbo]
<span class="kwrd">FROM</span> 
<span class="kwrd">WITH</span> PERMISSION_SET = EXTERNAL_ACCESS

<span class="kwrd">GO</span>

<span class="rem">-- Create encapsulation for SQL CLR UDF.</span>
<span class="rem">-- Function has same signature as its C# counterpart.</span>
<span class="kwrd">CREATE</span> <span class="kwrd">FUNCTION</span> fn_ToFile(@fileName [nvarchar](255), @text [nvarchar](4000))
<span class="kwrd">RETURNS</span> <span class="kwrd">INT</span> <span class="kwrd">WITH</span> <span class="kwrd">EXECUTE</span> <span class="kwrd">AS</span> CALLER
<span class="kwrd">AS</span> 
--reference C# ToFile method <span class="kwrd">of</span> Debug <span class="kwrd">class</span>
<span class="kwrd">EXTERNAL</span> NAME <your_assembly_name>.Debug.ToFile
<span class="kwrd">GO</span>


<span class="rem">-- This is some business logic function you </span>
<span class="rem">-- want to log anything in.</span>
<span class="kwrd">CREATE</span> <span class="kwrd">FUNCTION</span> [fn_SomeBusinessLogicFunction]()
<span class="kwrd">RETURNS</span> <span class="kwrd">INT</span> <span class="kwrd">WITH</span> <span class="kwrd">EXECUTE</span> <span class="kwrd">AS</span> CALLER
<span class="kwrd">AS</span> 
<span class="kwrd">BEGIN</span>
    
    <span class="rem">-- log to file    </span>
    <span class="kwrd">RETURN</span> dbo.fn_ToFile(<span class="str">'your_file_path'</span>, <span class="str">'Debug text'</span>)

<span class="kwrd">END</span>
</pre>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/access-permissions/" title="access permissions" rel="tag">access permissions</a>, <a href="http://www.sqltreeo.com/wp/tag/appendtext/" title="AppendText" rel="tag">AppendText</a>, <a href="http://www.sqltreeo.com/wp/tag/authorization/" title="AUTHORIZATION" rel="tag">AUTHORIZATION</a>, <a href="http://www.sqltreeo.com/wp/tag/automation/" title="automation" rel="tag">automation</a>, <a href="http://www.sqltreeo.com/wp/tag/caller/" title="CALLER" rel="tag">CALLER</a>, <a href="http://www.sqltreeo.com/wp/tag/clr/" title="CLR" rel="tag">CLR</a>, <a href="http://www.sqltreeo.com/wp/tag/clr-udf/" title="CLR UDF" rel="tag">CLR UDF</a>, <a href="http://www.sqltreeo.com/wp/tag/csharpcode/" title="csharpcode" rel="tag">csharpcode</a>, <a href="http://www.sqltreeo.com/wp/tag/execute/" title="EXECUTE" rel="tag">EXECUTE</a>, <a href="http://www.sqltreeo.com/wp/tag/external/" title="EXTERNAL" rel="tag">EXTERNAL</a>, <a href="http://www.sqltreeo.com/wp/tag/oaxxxx/" title="OAxxxx" rel="tag">OAxxxx</a>, <a href="http://www.sqltreeo.com/wp/tag/ole-automation/" title="OLE-Automation" rel="tag">OLE-Automation</a>, <a href="http://www.sqltreeo.com/wp/tag/permission/" title="PERMISSION" rel="tag">PERMISSION</a>, <a href="http://www.sqltreeo.com/wp/tag/register-assembly/" title="Register assembly" rel="tag">Register assembly</a>, <a href="http://www.sqltreeo.com/wp/tag/security-changes/" title="security changes" rel="tag">security changes</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sqlfunction/" title="SqlFunction" rel="tag">SqlFunction</a>, <a href="http://www.sqltreeo.com/wp/tag/sqlserver/" title="SqlServer" rel="tag">SqlServer</a>, <a href="http://www.sqltreeo.com/wp/tag/trustworthy/" title="trustworthy" rel="tag">trustworthy</a>, <a href="http://www.sqltreeo.com/wp/tag/udf/" title="udf" rel="tag">udf</a>, <a href="http://www.sqltreeo.com/wp/tag/writeline/" title="writeline" rel="tag">writeline</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/how-to-write-data-from-user-defined-function/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Great new analytical functions in SQL Server 2011 &#8220;Denali&#8221;</title>
		<link>http://www.sqltreeo.com/wp/great-new-analytical-functions-in-sql-server-2011-denali/</link>
		<comments>http://www.sqltreeo.com/wp/great-new-analytical-functions-in-sql-server-2011-denali/#comments</comments>
		<pubDate>Sun, 14 Aug 2011 14:40:12 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[LAST_VALUE]]></category>
		<category><![CDATA[result set]]></category>
		<category><![CDATA[set partition]]></category>
		<category><![CDATA[sql server 2011]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[value functions]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=971</guid>
		<description><![CDATA[SQL Server 2011 &#8220;Denali&#8221; is quite rich for new functions which have really good practical use in T-SQL. I&#8217;ve read about another new functions in Denali&#8217;s Books Online. I am missing those functions, I will write about, really for years. They&#8217;re filled under &#8220;Analytical function&#8221; and they are close to sort of &#8220;ranking helper functions&#8221;. ...]]></description>
			<content:encoded><![CDATA[<p>SQL Server 2011 &#8220;Denali&#8221; is quite rich for new functions which have really good practical use in T-SQL. I&#8217;ve read about another new functions in <a href="http://msdn.microsoft.com/en-us/library/ms130214%28v=SQL.110%29.aspx" target="_blank">Denali&#8217;s Books Online.</a> I am missing those functions, I will write about, really for years. They&#8217;re filled under &#8220;Analytical function&#8221; and they are close to sort of &#8220;ranking helper functions&#8221;. My <a href="http://en.wikipedia.org/wiki/Fantastic_Four" target="_blank">Fantastic Four</a> is <strong>LAG</strong>, <strong>LEAD</strong>, <strong>FIRST_VALUE</strong> and <strong>LAST_VALUE</strong> and if you&#8217;re T-SQL developer you will love them! Another five new functions are less attractive for me but they&#8217;re just there &#8211; <strong>CUME_DIST</strong>, <strong>PERCENTILE_CONT</strong>, <strong>PERCENTILE_DISC</strong> and <strong>PERCENT_RANK</strong>.<br />
<AD><br />
<a href="http://msdn.microsoft.com/en-us/library/hh231256%28v=SQL.110%29.aspx" target="_blank"><strong>LAG</strong></a><strong> </strong><strong>and <a href="http://msdn.microsoft.com/en-us/library/hh213125%28v=SQL.110%29.aspx" target="_blank">LEAD</a> function</strong><a href="http://msdn.microsoft.com/en-us/library/hh231256%28v=SQL.110%29.aspx" target="_blank"><strong> </strong></a><strong>s</strong></p>
<p><a href="http://msdn.microsoft.com/en-us/library/hh231256%28v=SQL.110%29.aspx" target="_blank"><strong> </strong></a>They provide access to previous row (LAG) or subsequent row (LEAD)  in result set or partition. If they are combined with PARTITION BY clause within required OVER clause, it accesses previous/next row only from given partition. If there is previous/next row in result set/partition, both function returns null. These functions basically replace necessity for self-joins when you need to access previous/next row. Both functions provides access to rows based on given offset relative to current row.</p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/hh213018%28v=SQL.110%29.aspx" target="_blank">FIRST_VALUE</a> and <a href="http://msdn.microsoft.com/en-us/library/hh231517%28v=SQL.110%29.aspx" target="_blank">LAST_VALUE</a> functions</strong></p>
<p>These functions are very similar to LAG and LEAD and do what is expected &#8211; provide access to first or last row in result set or partition. They must be combined with OVER clause as well, PARTITION BY clause is optional.</p>
<p>Here is very simple example of new analytical functions in action. SQL command groups employees&#8217; lastname by department and use LAG, LEAD and FIRST_VALUE function results.</p>
<div style="overflow: auto;">
<pre class="csharpcode"> <span class="kwrd">SELECT</span> 

 d.GroupName,
 b.LastName,
 LAG (LastName) <span class="kwrd">OVER</span> (PARTITION <span class="kwrd">BY</span> d.GroupName <span class="kwrd">ORDER</span> <span class="kwrd">BY</span> b.LastName) <span class="kwrd">AS</span> <span class="str">'LAG - prev. value'</span>,
 LEAD (LastName) <span class="kwrd">OVER</span> (PARTITION <span class="kwrd">BY</span> d.GroupName <span class="kwrd">ORDER</span> <span class="kwrd">BY</span> b.LastName) <span class="kwrd">AS</span> <span class="str">'LEAD - next. value'</span>,
 FIRST_VALUE (LastName) <span class="kwrd">OVER</span> (PARTITION <span class="kwrd">BY</span> d.GroupName <span class="kwrd">ORDER</span> <span class="kwrd">BY</span> b.LastName) <span class="kwrd">AS</span> <span class="str">'FIRST_VALUE'</span>

 <span class="kwrd">FROM</span> HumanResources.Employee e
    <span class="kwrd">JOIN</span> Person.Person b <span class="kwrd">ON</span> b.BusinessEntityID = e.BusinessEntityID
    <span class="kwrd">JOIN</span> HumanResources.EmployeeDepartmentHistory dh <span class="kwrd">ON</span> dh.BusinessEntityID = b.BusinessEntityID
    <span class="kwrd">JOIN</span> HumanResources.Department d <span class="kwrd">ON</span> d.DepartmentID = dh.DepartmentID
    <span class="kwrd">ORDER</span> <span class="kwrd">BY</span> d.GroupName, b.LastName</pre>
<p>&#8230; which gives following result:</p>
<p><a href="http://www.sqltreeo.com/wp/great-new-analytical-functions-in-sql-server-2011-denali" target="_blank"><img class="alignnone size-full wp-image-1006" title="analytical_functions_2011" src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/08/analytical_functions_2011.png" alt="" width="771" height="631" /></a></p>
</div>
<p>&nbsp;</p>
<p><strong><a href="http://msdn.microsoft.com/en-us/library/hh231078%28v=SQL.110%29.aspx" target="_blank">CUME_DIST</a>, <a href="http://msdn.microsoft.com/en-us/library/hh231473%28v=SQL.110%29.aspx" target="_blank">PERCENTILE_CONT</a>, <a href="http://msdn.microsoft.com/en-us/library/hh231327%28v=SQL.110%29.aspx" target="_blank">PERCENTILE_DISC</a> and <a href="http://msdn.microsoft.com/en-us/library/hh213573%28v=SQL.110%29.aspx" target="_blank">PERCENT_RANK</a></strong></p>
<p>These function are pure statistical. You can use it when you need to calculate cumulative distribution or various types of percentile.</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/last_value/" title="LAST_VALUE" rel="tag">LAST_VALUE</a>, <a href="http://www.sqltreeo.com/wp/tag/result-set/" title="result set" rel="tag">result set</a>, <a href="http://www.sqltreeo.com/wp/tag/set-partition/" title="set partition" rel="tag">set partition</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-2011/" title="sql server 2011" rel="tag">sql server 2011</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/value-functions/" title="value functions" rel="tag">value functions</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/great-new-analytical-functions-in-sql-server-2011-denali/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Download free SSMS Add-In to create custom folders for database objects</title>
		<link>http://www.sqltreeo.com/wp/dowload-free-ssms-add-in-to-create-own-folder-for-database-objects/</link>
		<comments>http://www.sqltreeo.com/wp/dowload-free-ssms-add-in-to-create-own-folder-for-database-objects/#comments</comments>
		<pubDate>Mon, 08 Aug 2011 09:03:37 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[custom folders]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Red Gate]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[server management]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Prompt]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=709</guid>
		<description><![CDATA[Update for lazy readers: Short summary and download here. &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; I&#8217;ve just finished promised SSMS add-in which enables creating of custom folders directly in SSMS Object Explorer. Idea for add-in came from fact that I was dealing with hundreds of SQL objects and was fed up of browsing for correct one in SSMS standard folders. ...]]></description>
			<content:encoded><![CDATA[<h4>Update for lazy readers:</h4>
<p><strong>Short summary and download <a href="http://www.sqltreeo.com/wp?short">here</a></strong>.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
I&#8217;ve just finished promised SSMS add-in which enables creating of custom folders directly in SSMS Object Explorer. Idea for add-in came from fact that I was dealing with hundreds of SQL objects and was fed up of browsing for correct one in SSMS standard folders. I tried also many &#8220;quick find&#8221; add-ins but they were not 100% solution for this issue.<br />
<AD><br />
I observed that I search for object in two ways:</p>
<ul>
<li><strong>I know full or part object&#8217;s name</strong> &#8211; I use free <a href="http://www.red-gate.com/products/sql-development/sql-search/">Red Gate&#8217;s SQL Search Add-In</a> (unfortunately, I have issue with integrating it with my add-in now). To make long story short, you can find any object very quickly with this add-in.</li>
<li><strong>I can&#8217;t remember any part of object&#8217;s name  &#8211; </strong>I was lost at this point and doomed to browsing for object in standard e.g. <em>Stored procedure</em> node. <strong> </strong></li>
</ul>
<p>Second of mentioned points was the key driver to develop this add-in for me. I&#8217;ve even found similar <a href="http://www.skilledsoftware.com/">commercial solution</a> (for $65) but it supports only one level hierarchy. I employed SQL object schemas for emulating one level hierarchy before and it was simply not enough for me.</p>
<p>My Add-in currently supports following:</p>
<ul>
<li>Create custom folders in SSMS’s object explorer using drag&amp;drop.  It works for databases, stored procedures, functions, views and tables</li>
<li>Ctrl+click on object will bring ALTER script</li>
<li>Perform bulk operations in explorer-style management dialog – I did  that because while I was refactoring and re-foldering more objects,  drag&amp;drop was not enough</li>
</ul>
<p>See this <a href="http://vimeo.com/26274818">short video </a>or following screenshot to find how it works:</p>
<p><img class="alignnone size-full wp-image-710" title="sqltreeo_screenshot" src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/sqltreeo_screenshot.png" alt="" width="462" height="603" /><br />
<AD></p>
<h4>Technical solution</h4>
<p>I must say that it was really hard to develop this add-in because documentation for SSMS add-in development is really bad. Add-In is developed in C# and partly in C++ and is installable to SQL Server Management Studio and supports <strong>SQL Server 7.0 and higher</strong> versions on server-side. From client point of view, it supports currently only <strong>SQL Server 2008 R2 Client Tools</strong> because this is platform I am working on these days. My intention was to support also lower versions of client tools but I soon found out that Microsoft made incompatible changes in 2005, 2008 and 2008 R2. I did first version for me and my team members so there was no push to convert it to other platforms. If I feel pressure on different platforms I will probably convert it. You can push me for other <a href="http://forum.sqltreeo.com">supported platforms here</a>.</p>
<p>Logic of handling folders is quite simple:</p>
<p>If you create empty folder within standard SSMS node nothing happened on server-side, it only creates folder in client tree within Object Explorer. This is the reason why empty folders are not persistent. After you drag anything to folder, extended property is created/updated on dragged SQL object (e.g. table) by <strong>sp_addextendedproperty / sp_updateextendedproperty</strong> procedures.</p>
<p>If you expand standard tree node (e.g. Stored procedures), every object inside is queried with for extended properties by <strong>fn_listextendedproperty</strong> procedure. Add-in than creates folder structure dynamically based on found custom extended properties inserted by add-in before. Based on this internal behavior, you can bypass add-in when you create extended properties for your objects on your own &#8211; add-in just reads them and creates folders based on their values.</p>
<h4>Synergy with Red Gate&#8217;s products</h4>
<p>We are using ingenious <a href="http://www.red-gate.com/products/sql-development/sql-source-control/">Red Gate&#8217;s SQL Source Control SSMS Add-In</a> which handles database versioning for our team (in Subversion). I used extended properties as &#8220;persistence medium&#8221; for folders because I wanted to share logical folders in our databases across all team members. My add-in and Red Gate&#8217;s add-in together works like a charm for that &#8211; when I create folder in my dev database and commit changes to source control, other team member see this folder once he updates his development database from source control. This is because extended properties are versioned as well.</p>
<p>I am big fan of Red Gate&#8217;s SQL Search and <a href="http://www.red-gate.com/products/sql-development/sql-prompt/">SQL Prompt</a> add-ins so I examined coexistence of those to add-ins with mine. SQL Prompt is ok, no issues. Unfortunately my add-in cannot currently handle situation when SQL Search add-in is jumping to Object Explorer treeview for highlighting object (it crashes). I was trying hard to solve that but I will probably ask Red Gate for changing their SQL Search code because I simply cannot do that on my side. They will probably send me to ****** but I will try <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h4>Roadmap</h4>
<p>Roadmap is maybe strong word but here is a list of features which came from every day use of this add-in:</p>
<ul>
<li>ability to change color of folders &#8211; this will faster catch your eye for correct folder</li>
<li>have one folder &#8220;Uncategorized&#8221; which would serve as container for unfoldered objects</li>
<li>ability to completely bypass standard SSMS structure (e.g. combine tables, stored procedures and function in one folder name &#8220;Banking Module&#8221;)</li>
<li>store custom queries in your folders</li>
<li>&#8230; and some more stuff</li>
</ul>
<p>You can download add-in <a href="http://www.sqltreeo.com/" target="_blank">on my pages</a>.</p>
<p>I hope it will be helpful.<br />
<AD><br />
&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/custom-folders/" title="custom folders" rel="tag">custom folders</a>, <a href="http://www.sqltreeo.com/wp/tag/development/" title="development" rel="tag">development</a>, <a href="http://www.sqltreeo.com/wp/tag/red-gate/" title="Red Gate" rel="tag">Red Gate</a>, <a href="http://www.sqltreeo.com/wp/tag/refactoring/" title="refactoring" rel="tag">refactoring</a>, <a href="http://www.sqltreeo.com/wp/tag/server-management/" title="server management" rel="tag">server management</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-prompt/" title="SQL Prompt" rel="tag">SQL Prompt</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/dowload-free-ssms-add-in-to-create-own-folder-for-database-objects/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>SQL Server 2011 &#8220;Denali&#8221;: Get T-SQL query metadata without executing it</title>
		<link>http://www.sqltreeo.com/wp/sql-server-2011-denali-get-t-sql-query-metadata-without-executing-it/</link>
		<comments>http://www.sqltreeo.com/wp/sql-server-2011-denali-get-t-sql-query-metadata-without-executing-it/#comments</comments>
		<pubDate>Wed, 03 Aug 2011 18:15:32 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[built-in functions]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[dynamic sql]]></category>
		<category><![CDATA[metadata]]></category>
		<category><![CDATA[sql server 2011]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=961</guid>
		<description><![CDATA[SQL Server 2011 &#8220;Denali&#8221; introduces couple of new procedures which enable you to read query metadata without executing it. Previous versions of SQL Server support reading metadata from system catalog but it is possible only for fixed objects. With following new procedures you can do similar thing but for dynamic T-SQL query. There are 3 ...]]></description>
			<content:encoded><![CDATA[<p>SQL Server 2011 &#8220;Denali&#8221; introduces couple of new procedures which enable you to read query metadata without executing it. Previous versions of SQL Server support reading metadata from system catalog but it is possible only for fixed objects. With following new procedures you can do similar thing but for dynamic T-SQL query.<br />
<AD><br />
There are 3 new procedures:</p>
<ul>
<li><strong>sys.dm_exec_describe_first_result</strong> &#8211; this procedure returns column of first result set in query. Query is passed to procedure as a dynamic SQL.</li>
<li><strong>sys.dm_exec_describe_first_result_set_for_object</strong> &#8211; do the same thing as previous but for specific object_id</li>
<li><strong>sp_describe_undeclared_parameters</strong> &#8211; this procedure pulls parameters from SQL query and return them in result set</li>
</ul>
<p>See following example for <strong>sys.dm_exec_describe_first_result</strong>:</p>
<pre class="csharpcode"><span class="kwrd">DECLARE</span> @<span class="kwrd">sql</span> <span class="kwrd">VARCHAR</span>(500)

<span class="kwrd">SET</span> @<span class="kwrd">sql</span> = 
<span class="str">'USE AdventureWorks
SELECT e.LoginID, c.FirstName, c.LastName FROM HumanResources.Employee e 
JOIN Person.Contact c ON c.ContactID = e.ContactID'</span>

<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> sys.dm_exec_describe_first_result_set (@<span class="kwrd">sql</span>, <span class="kwrd">null</span>, 0) ;</pre>
<p>&#8230;will result in grid with LoginID, FirstName and LastName columns and their metadata:<br />
<a href="http://www.sqltreeo.com/wp/sql-server-2011-denali-get-t-sql-query-metadata-without-executing-it"><img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/08/describe_resultset.png" alt="" title="describe_resultset" width="437" height="121" class="alignnone size-full wp-image-964" /></a></p>
<p>Here is sample for <strong>sp_describe_undeclared_parameters</strong>:</p>
<pre class="csharpcode"><span class="kwrd">DECLARE</span> @<span class="kwrd">sql</span> NVARCHAR(<span class="kwrd">MAX</span>)

<span class="kwrd">SET</span> @<span class="kwrd">sql</span> = 
<span class="str">'USE AdventureWorks
SELECT e.LoginID, c.FirstName, c.LastName FROM HumanResources.Employee e 
JOIN Person.Contact c ON c.ContactID = e.ContactID
WHERE FirstName LIKE @firstName'</span>

<span class="kwrd">EXEC</span> sp_describe_undeclared_parameters @tsql= @sql</pre>
<p>&#8230;will result in grid describing @firstName parameter:<br />
<a href="http://www.sqltreeo.com/wp/sql-server-2011-denali-get-t-sql-query-metadata-without-executing-it"><img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/08/undeclared_parameters.png" alt="" title="undeclared_parameters" width="616" height="86" class="alignnone size-full wp-image-965" /></a></p>
<p>Those procedures might be very handy when you&#8217;re dealing with dynamic SQL but there are a lot of limitations. Please read <a href="http://msdn.microsoft.com/en-us/library/ff878260%28v=SQL.110%29.aspx">Books Online</a> for more details.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/built-in-functions/" title="built-in functions" rel="tag">built-in functions</a>, <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/dynamic-sql/" title="dynamic sql" rel="tag">dynamic sql</a>, <a href="http://www.sqltreeo.com/wp/tag/metadata/" title="metadata" rel="tag">metadata</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-2011/" title="sql server 2011" rel="tag">sql server 2011</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-server-2011-denali-get-t-sql-query-metadata-without-executing-it/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Awesome new features in SQL Server 2011 &#8220;Denali&#8221;: IIF and CHOOSE functions</title>
		<link>http://www.sqltreeo.com/wp/awesome-new-features-in-sql-server-2011-denali-iif-and-choose-functions/</link>
		<comments>http://www.sqltreeo.com/wp/awesome-new-features-in-sql-server-2011-denali-iif-and-choose-functions/#comments</comments>
		<pubDate>Tue, 02 Aug 2011 19:10:42 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[flow control]]></category>
		<category><![CDATA[iif function]]></category>
		<category><![CDATA[new features]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=921</guid>
		<description><![CDATA[SQL Server 2011 &#8220;Denali&#8221; introduces two new features for flow control which may help you to keep your T-SQL cleaner than in previous versions. It is IIF and CHOOSE. These two function are very simple but it&#8217;s good to know they&#8217;re there. IIF Function Any developer should be aware of this function but prior SQL Server ...]]></description>
			<content:encoded><![CDATA[<p>SQL Server 2011 &#8220;Denali&#8221; introduces two new features for flow control which may help you to keep your T-SQL cleaner than in previous versions. It is IIF and CHOOSE. These two function are very simple but it&#8217;s good to know they&#8217;re there.<br />
<AD><br />
<strong>IIF Function</strong><br />
Any developer should be aware of this function but prior SQL Server 2011 IIF workaround was:</p>
<pre class="csharpcode">
<span class="kwrd">DECLARE</span> @<span class="kwrd">variable</span> <span class="kwrd">VARCHAR</span>(50) = <span class="str">'I am not NULL'</span>    

<span class="kwrd">SELECT</span> 

    <span class="kwrd">CASE</span> 
        <span class="kwrd">WHEN</span> @<span class="kwrd">variable</span> <span class="kwrd">IS</span> <span class="kwrd">NOT</span> <span class="kwrd">NULL</span> <span class="kwrd">THEN</span> <span class="str">'NOT_NULL'</span>
        <span class="kwrd">ELSE</span> <span class="kwrd">NULL</span>
    <span class="kwrd">END</span></pre>
<p>But in SQL Server 2011 you can use true IIF function:</p>
<pre class="csharpcode">
<span class="kwrd">DECLARE</span> @<span class="kwrd">variable</span> <span class="kwrd">VARCHAR</span>(50) = <span class="str">'I am not NULL'</span>    

<span class="kwrd">SELECT</span> IIF (@<span class="kwrd">variable</span> <span class="kwrd">IS</span> <span class="kwrd">NOT</span> <span class="kwrd">NULL</span>,<span class="str">'NOT_NULL'</span>,<span class="kwrd">NULL</span>)    </pre>
<p><strong>CHOOSE Function</strong><br />
CHOOSE function is not so typical but may help as well. Purpose of this function is to pick a value based on its index from given range. </p>
<p>Check this sample with subconscious communication <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  :</p>
<pre class="csharpcode">
<span class="kwrd">SELECT</span> CHOOSE ( 3, <span class="str">'You'</span>, <span class="str">'love'</span>, <span class="str">'SQLTreeo'</span>)</pre>
<p>Result will be &#8220;SQLTreeo&#8221;.</p>
<p>Nice.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/flow-control/" title="flow control" rel="tag">flow control</a>, <a href="http://www.sqltreeo.com/wp/tag/iif-function/" title="iif function" rel="tag">iif function</a>, <a href="http://www.sqltreeo.com/wp/tag/new-features/" title="new features" rel="tag">new features</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/awesome-new-features-in-sql-server-2011-denali-iif-and-choose-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Good, the Bad and the Ugly SQL Cursor types</title>
		<link>http://www.sqltreeo.com/wp/the-good-the-bad-and-the-ugly-sql-cursor-types/</link>
		<comments>http://www.sqltreeo.com/wp/the-good-the-bad-and-the-ugly-sql-cursor-types/#comments</comments>
		<pubDate>Sat, 30 Jul 2011 23:12:59 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[correlated subqueries]]></category>
		<category><![CDATA[cursors]]></category>
		<category><![CDATA[dynamic cursor]]></category>
		<category><![CDATA[keyset cursor]]></category>
		<category><![CDATA[locks]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[tempdb]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=913</guid>
		<description><![CDATA[T-SQL cursors are generally bad approach and are often misused. In today&#8217;s world of correlated subqueries, CTE&#8217;s, recursive CTE&#8217;s, ranking and windowing functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE) and other tools, you have really few moments where cursor is better solution. On the other hand, there are still scenarios where cursor are best and most efficient ...]]></description>
			<content:encoded><![CDATA[<p>T-SQL cursors are generally bad approach and are often misused. In today&#8217;s world of correlated subqueries, CTE&#8217;s, recursive CTE&#8217;s, ranking and windowing functions (ROW_NUMBER, RANK, DENSE_RANK, NTILE) and other tools, you have really few moments where cursor is better solution. On the other hand, there are still scenarios where cursor are best and most efficient solution.<br />
<AD><br />
If you think that holy moment of cursor occurred, it&#8217;s good to know which type of cursor you should use. It&#8217;s good to know what cursor features you need and choose a proper type of cursor. Or you&#8217;re most probably wasting server resources. I tried to summarize important aspects which you need consider when using cursors to prevent such situation.</p>
<p>Here is important settings from standard cursor declaration syntax:</p>
<p>DECLARE <em>cursor_name</em> CURSOR<br />
[ LOCAL | GLOBAL ]<br />
[ FORWARD_ONLY | SCROLL ]<br />
[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ]<br />
[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ]</p>
<p>Here is fast human description of cursor options:</p>
<p><strong>[ LOCAL | GLOBAL ]</strong> &#8211; Use LOCAL, cursor will live only in scope of batch. GLOBAL cursor lives in whole connection, I can&#8217;t imagine many scenarios where you really need it.</p>
<p><strong>[ FORWARD_ONLY | SCROLL ]</strong> &#8211; Use FORWARD_ONLY, cursor will enable only sequential forward only reading. SCROLL cursor enables to move freely forward and backward which require more resources.</p>
<p><strong>[ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ] &#8211; </strong>Use FAST_FORWARD, it is the combination of READ_ONLY and FORWARD_ONLY cursor. <strong>FAST_FORWARD cursor enables only forward and read only movement which makes it most efficient compared to the other types.</strong> STATIC cursor loops over copy of data, not over original data hence doesn&#8217;t support any updates. Creating copy of data is very resource-intensive. KEYSET cursor requires unique key identifying rows, these keys are copied in tempdb when cursor is opened. It loops then based on those keys. You must have very good reason to use KEYSET cursor. DYNAMIC cursor immediately reflects any changes made to underlying tables.</p>
<p><strong>[ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ] -</strong>READ_ONLY is good but doesn&#8217;t support any updates. SCROLL_LOCKS option tells cursor to lock every read row to ensure that updates through cursor will succeed.</p>
<p>And few more important points :</p>
<ul>
<li>start with LOCAL FAST_FORWARD cursor and then change it if you need something special</li>
<li>use minimal count of rows and columns within cursor</li>
<li>think about what you&#8217;re doing inside cursor, try to avoid complex SELECT queries inside</li>
<li>try to avoid STATIC and KEYSET cursors, they work with tempdb which is another overhead</li>
<li>don&#8217;t forget to CLOSE and DEALLOCATE cursor regardless of its scope settings</li>
</ul>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/correlated-subqueries/" title="correlated subqueries" rel="tag">correlated subqueries</a>, <a href="http://www.sqltreeo.com/wp/tag/cursors/" title="cursors" rel="tag">cursors</a>, <a href="http://www.sqltreeo.com/wp/tag/dynamic-cursor/" title="dynamic cursor" rel="tag">dynamic cursor</a>, <a href="http://www.sqltreeo.com/wp/tag/keyset-cursor/" title="keyset cursor" rel="tag">keyset cursor</a>, <a href="http://www.sqltreeo.com/wp/tag/locks/" title="locks" rel="tag">locks</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/tempdb/" title="tempdb" rel="tag">tempdb</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/the-good-the-bad-and-the-ugly-sql-cursor-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Azure &#8211; simple description, part I.</title>
		<link>http://www.sqltreeo.com/wp/sql-azure-simple-description-part-i/</link>
		<comments>http://www.sqltreeo.com/wp/sql-azure-simple-description-part-i/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 23:31:28 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[business edition]]></category>
		<category><![CDATA[database engine]]></category>
		<category><![CDATA[Michael Jurek]]></category>
		<category><![CDATA[SQL Azure]]></category>
		<category><![CDATA[web edition]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=906</guid>
		<description><![CDATA[I was studying SQL Azure parameters some times ago and found out that there are no materials which tells you clearly what it is, what it costs, what are limitations, what developers can and can&#8217;t do etc. There are so many marketing materials, business materials and even very technical materials but not simple, single-level big ...]]></description>
			<content:encoded><![CDATA[<p>I was studying SQL Azure parameters some times ago and found out that there are no materials which tells you clearly what it is, what it costs, what are limitations, what developers can and can&#8217;t do etc. There are so many marketing materials, business materials and even very technical materials but not simple, single-level big picture stuff (at least for my brain). I found great article by Microsoft Developer Evangelist Michael Jurek but it is unfortunately in my native language so I translated some parts from it.<br />
<AD><br />
Let&#8217;s start with some business/high-level aspects:</p>
<ol>
<li>SQL Azure is cloud version of SQL Server database engine.</li>
<li>Microsoft has many computers spread over three continents (Europe, Asia, North America). SQL Azure is running on many of them</li>
<li>As customer, you can create virtual SQL server on these cloud servers. Virtual server consists of databases, user accounts etc. known stuff</li>
<li>You can create databases on virtual server. You must pay for them on regular basis, appx 10 USD for GB per month</li>
<li>There are two editions &#8211; Web (1 and 5GB sizes) and Business (10,20,30,40 and 50 GB sizes).</li>
<li>If you need bigger databases you can use <a href="http://blogs.msdn.com/b/sqlazure/archive/2010/12/23/10108670.aspx">sharding</a>.</li>
<li>You have no control where your databases are physically placed (on which boxes)</li>
<li>When you&#8217;re creating database you chose edition (Web/Business) and maximal size of database (1 or 5 GB for Web and 10 or 20 or 30 or 40 or 50 for Business)</li>
<li>You pay monthly but only for space which you&#8217;ve consumed rounded up to next available size in your edition. Example: You have Web edition with 1GB and consumed 0,2 GB &#8211; you pay for 1 GB which is 10 USD/month. Example 2: You have Business edition with 40 GB and consumed only 16 GB &#8211; you pay as for 20 GB which is 200 USD.</li>
<li>&#8220;Consumed space&#8221; is tables, indexes etc. &#8211; everyhing which is stored in .MDF file. Transaction log space is for free.</li>
<li>There are no fees for HW, CALs, processor licences etc. There is one fee you may pay &#8211; for transferred GB&#8217;s. It is 0,1 USD/GB for in-traffic and 0,15 USD for out-traffic. However this fee is relevant only if your application layer is out of cloud. If you have your application layer within cloud, there is no external traffic in/out SQL Azure which means no additional fees.</li>
<li>Microsoft states that for this money you get high availability SQL Server and automatic, seamless fail-over if anything goes wrong</li>
<li>You are basically focused only to your databases, not to HW, operating system, security updates or anything else</li>
</ol>
<p>Next time, I will continue with what is permitted and what&#8217;s not from administration point of view.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/business-edition/" title="business edition" rel="tag">business edition</a>, <a href="http://www.sqltreeo.com/wp/tag/database-engine/" title="database engine" rel="tag">database engine</a>, <a href="http://www.sqltreeo.com/wp/tag/michael-jurek/" title="Michael Jurek" rel="tag">Michael Jurek</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-azure/" title="SQL Azure" rel="tag">SQL Azure</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/web-edition/" title="web edition" rel="tag">web edition</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-azure-simple-description-part-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL refactoring</title>
		<link>http://www.sqltreeo.com/wp/t-sql-refactoring/</link>
		<comments>http://www.sqltreeo.com/wp/t-sql-refactoring/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 19:29:18 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[dependencies]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[Red Gate]]></category>
		<category><![CDATA[refactoring]]></category>
		<category><![CDATA[Rename]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=873</guid>
		<description><![CDATA[Keeping your SQL objects&#8217; naming rules during furious development is hard. New levels of information are added continuously, entities are being splitted, then joined together again, entities are often renamed several times just because you have better name for them etc. For me, renaming is heart of T-SQL refactoring. But whenever you need to rename ...]]></description>
			<content:encoded><![CDATA[<p>Keeping your SQL objects&#8217; naming rules during furious development is hard. New levels of information are added continuously, entities are being splitted, then joined together again, entities are often renamed several times just because you have better name for them etc. For me, renaming is heart of T-SQL refactoring. But whenever you need to rename stored procedure which is referenced by many others, you have no built-in options how to do that. If you are in Visual Studio .NET and have <a href="http://www.jetbrains.com/resharper/">ReSharper</a> installed, it&#8217;s piece of cake. You just click rename, and it&#8217;s safely done throughout your solution.<br />
<AD><br />
You have one painful &#8220;workaround&#8221; in T-SQL (as far as I know) &#8211; to script all dependencies and perform careful search and replace operation and re-create all affected objects. I personally hated that because when<br />
I was refactoring I wanted to think only about proper new names, not about anything else. But if you&#8217;re renaming frequent word which is used in not only in object name (but in column names etc.), you&#8217;re forced to walk through hundreds of lines and thinking If I should rename this or that. </p>
<p>On my current project, we started to use <a href="http://www.red-gate.com/products/sql-development/sql-prompt/">SQL Prompt SSMS add-in</a> many months ago but I discovered it&#8217;s great value for T-SQL refactoring yesterday. It provides feature which is invaluable for described refactoring scenarios. This features is called &#8220;Smart Rename&#8221; and provides consistent renaming of SQL object even with renaming it in all dependencies. </p>
<p>Saved me hours this week. </p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/dependencies/" title="dependencies" rel="tag">dependencies</a>, <a href="http://www.sqltreeo.com/wp/tag/development/" title="development" rel="tag">development</a>, <a href="http://www.sqltreeo.com/wp/tag/red-gate/" title="Red Gate" rel="tag">Red Gate</a>, <a href="http://www.sqltreeo.com/wp/tag/refactoring/" title="refactoring" rel="tag">refactoring</a>, <a href="http://www.sqltreeo.com/wp/tag/rename/" title="Rename" rel="tag">Rename</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/t-sql-refactoring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fastest row counting method</title>
		<link>http://www.sqltreeo.com/wp/fastest-row-counting-method/</link>
		<comments>http://www.sqltreeo.com/wp/fastest-row-counting-method/#comments</comments>
		<pubDate>Tue, 26 Jul 2011 11:48:30 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Clustered]]></category>
		<category><![CDATA[COUNT]]></category>
		<category><![CDATA[Heap]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=867</guid>
		<description><![CDATA[SELECT COUNT(*) is most common method (and exact) how to find out how many records is in table. There is also another method which is not exact but is way faster especially when you are expecting zillion of rows to be counted. This method is based on fact that SQL Server internally tracks how many ...]]></description>
			<content:encoded><![CDATA[<p>SELECT COUNT(*) is most common method (and exact) how to find out how many records is in table. There is also another method which is not exact but is way faster especially when you are expecting zillion of rows to be counted. This method is based on fact that SQL Server internally tracks how many rows is maintaned by each index.<br />
<AD><br />
There are two situations in table which are important for what I am talking about:</p>
<ul>
<li>Clustered index is present on table</li>
<li>Clustered index not present on table, records are &#8220;organized&#8221; in heap. Other non-clustered indexes doesn&#8217;t affect heap, they only point to records in the heap.</li>
</ul>
<p>For both situations SQL Server knows how many records are contained either in the clustered index or in the heap. This information is held in sys.indexes system table and maintained by updating statistics. This is the reason why number of rows in sys.indexes table is not guaranteed &#8211; if statistics are not updated by any reason, this number is not actual.</p>
<p>Here&#8217;s code:</p>
<pre class="csharpcode">
<span class="kwrd">USE</span> AdventureWorks

<span class="kwrd">DECLARE</span> @<span class="kwrd">table</span> <span class="kwrd">VARCHAR</span>(250)
<span class="kwrd">SET</span> @<span class="kwrd">table</span> = <span class="str">'Person.Address'</span> <span class="rem">-- name of your table</span>
    
<span class="kwrd">SELECT</span> rowcnt
<span class="kwrd">FROM</span> sys.sysindexes
<span class="kwrd">WHERE</span> id = OBJECT_ID(@<span class="kwrd">table</span>) 
      <span class="kwrd">AND</span> (indid = 0 <span class="kwrd">OR</span> indid = 1) <span class="rem">-- clustered index or heap</span></pre>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/clustered/" title="Clustered" rel="tag">Clustered</a>, <a href="http://www.sqltreeo.com/wp/tag/count/" title="COUNT" rel="tag">COUNT</a>, <a href="http://www.sqltreeo.com/wp/tag/heap/" title="Heap" rel="tag">Heap</a>, <a href="http://www.sqltreeo.com/wp/tag/index/" title="Index" rel="tag">Index</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/fastest-row-counting-method/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Partial name matching in SQL Server 2011 &#8220;Denali&#8221;</title>
		<link>http://www.sqltreeo.com/wp/partial-name-matching-in-sql-server-2011-denali/</link>
		<comments>http://www.sqltreeo.com/wp/partial-name-matching-in-sql-server-2011-denali/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 23:40:57 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[intellisense]]></category>
		<category><![CDATA[Red Gate]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Prompt]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=855</guid>
		<description><![CDATA[While playing with SQL Server 2011 &#8220;Denali&#8221;, I&#8217;ve accidentally found out that it has slightly enhanced intellisense in SSMS. There is one thing which I really like much &#8211; partial name matching. Prior to SSMS 2011 you had to enter object&#8217;s first letters in order to be completed by intellisense. For example to find &#8220;JobCandidate&#8221; ...]]></description>
			<content:encoded><![CDATA[<p>While playing with SQL Server 2011 &#8220;Denali&#8221;, I&#8217;ve accidentally found out that it has slightly enhanced intellisense in SSMS. There is one thing which I really like much &#8211; partial name matching. Prior to SSMS 2011 you had to enter object&#8217;s first letters in order to be completed by intellisense. For example to find &#8220;JobCandidate&#8221; table you had to enter &#8220;Jo &#8230; &#8220;. This was quite annoying especially if you had many objects with same prefix &#8211;   this turn intellisense almost unusable. In SQL Server 2011 you can enter any part of object&#8217;s name and intellisense will still find it.<br />
<AD><br />
See simple demonstration <a href="http://www.sqltreeo.com/wp/sqlserver2011intellisensevideos/" target="_blank">here</a>.</p>
<p>I am personally switched off standard intellisense in SSMS 2008 long time ago and using <a href="http://www.red-gate.com/products/sql-development/sql-prompt/" target="_blank">Red Gate&#8217;s SQL Prompt</a> instead because it is just better. This is much more than enhanced intellisense but probably to align SQL Prompt intellisense to SSMS 2008 intellisense behavior, partial name matching is not implemented in SQL Prompt . I was googling and found out that partial name matching is in <a href="http://redgate.uservoice.com/forums/94413-sql-prompt-feature-suggestions/topics/95019-how-can-we-improve-sql-prompt-/filter/top" target="_blank">top 3 feature request</a> for SQL Prompt. I voted <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>To complete intellisense story &#8211; I was googling for full list of SSMS 2011 intellisense enhancements and found this <a href="http://blogs.msdn.com/b/sqlrem/archive/2011/01/04/t-sql-intellisense-enhancements-in-ssms-denali.aspx" target="_blank">article</a>.</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/intellisense/" title="intellisense" rel="tag">intellisense</a>, <a href="http://www.sqltreeo.com/wp/tag/red-gate/" title="Red Gate" rel="tag">Red Gate</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-prompt/" title="SQL Prompt" rel="tag">SQL Prompt</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/partial-name-matching-in-sql-server-2011-denali/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Article: How to use temporary table in function</title>
		<link>http://www.sqltreeo.com/wp/article-how-to-use-temporary-table-in-function/</link>
		<comments>http://www.sqltreeo.com/wp/article-how-to-use-temporary-table-in-function/#comments</comments>
		<pubDate>Thu, 21 Jul 2011 12:19:53 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[temp]]></category>
		<category><![CDATA[voiceofthedba]]></category>
		<category><![CDATA[workaround]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=836</guid>
		<description><![CDATA[Steve Jones from voiceofthedba.com, editor on SQLServerCentral.com gave me an opportunity to publish humble article about workaround solution for using temp table in user-defined function. I was very pleased and I am now proud that my article is on SQLServerCentral homepage (at least for today). After reading discussion under article today, I realized that I ...]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.voiceofthedba.com/">Steve Jones</a> from voiceofthedba.com, editor on <a href="http://www.sqlservercentral.com">SQLServerCentral.com</a> gave me an opportunity to publish humble article about workaround solution for using temp table in user-defined function. I was very pleased and I am now proud that my article is on <a href="http://www.sqlservercentral.com">SQLServerCentral homepage</a> (at least for today). <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<AD><br />
After reading discussion under article today, I realized that I should also mention practical scenario which leads me to this nasty workaround. Here it is:</br><br />
I needed to share data among stored procedures because I had very complex task which required to pull data from fixed tables to some temporary structures and do some logic above them. I picked #table solution for sharing data because all others (output parameters, UDDT, &#8230;) were not sufficient for my scenario. Then I&#8217;ve started to &#8220;encapsulate&#8221; this complex logic and realized that you cannot use #table with function. I made up synonym workaround at this point. At the end of a day I had to use process-keyed tables for data sharing because this workaround had very big maintenance drawback as described in article.<br />
<br/><br />
Article is <a href="http://www.sqlservercentral.com/articles/UDF/74231/">here</a>.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/table/" title="table" rel="tag">table</a>, <a href="http://www.sqltreeo.com/wp/tag/temp/" title="temp" rel="tag">temp</a>, <a href="http://www.sqltreeo.com/wp/tag/voiceofthedba/" title="voiceofthedba" rel="tag">voiceofthedba</a>, <a href="http://www.sqltreeo.com/wp/tag/workaround/" title="workaround" rel="tag">workaround</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/article-how-to-use-temporary-table-in-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom folders SSMS Add-In slightly delayed</title>
		<link>http://www.sqltreeo.com/wp/custom-folders-ssms-add-in-slightly-delayed/</link>
		<comments>http://www.sqltreeo.com/wp/custom-folders-ssms-add-in-slightly-delayed/#comments</comments>
		<pubDate>Sun, 17 Jul 2011 17:45:56 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[custom folders]]></category>
		<category><![CDATA[management objects]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=833</guid>
		<description><![CDATA[I am desperately trying to finalize this add-in and it is very very close. I was performing another testing on few thousands of objects and found out that add-in is causing instability to Management Studio from time to time while handlilng this high numbers of objects. It is very hard to investigate why is that ...]]></description>
			<content:encoded><![CDATA[<p>I am desperately trying to finalize this <a href="http://www.sqltreeo.com/wp/?add-in-delayed">add-in</a> and it is very very close. I was performing another testing on few thousands of objects and found out that add-in is causing instability to Management Studio from time to time while handlilng this high numbers of objects. It is very hard to investigate why is that because there are no possiblities to debug what is happening in SSMS. It looks to SQL Server Management Objects (SMO) issue currently. </p>
<p>I am sorry for everybody who is registered on my web and waiting for this add-in, I probably will keep announced summer release, but probably not July.<br />
<AD></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/custom-folders/" title="custom folders" rel="tag">custom folders</a>, <a href="http://www.sqltreeo.com/wp/tag/management-objects/" title="management objects" rel="tag">management objects</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/custom-folders-ssms-add-in-slightly-delayed/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>SQL Injection is still actual :)</title>
		<link>http://www.sqltreeo.com/wp/practical-examples-of-sql-injection-in-real-world/</link>
		<comments>http://www.sqltreeo.com/wp/practical-examples-of-sql-injection-in-real-world/#comments</comments>
		<pubDate>Sat, 16 Jul 2011 08:15:33 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[Injection]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=820</guid>
		<description><![CDATA[Here are two samples showing that SQL Injection is still here and dangerous !!! source:xkcd.com This use case is especially insidious Tags: fun, Injection, SQL]]></description>
			<content:encoded><![CDATA[<p>Here are two samples showing that SQL Injection is still here and dangerous !!! <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </br><br />
<a href="http://www.sqltreeo.com/wp/practical-examples-of-sql-injection-in-real-world"><img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/sql_injection2.png" alt="" title="sql_injection2" width="580" height="179" class="alignnone size-full wp-image-822" /></a><br />
<em>source:xkcd.com</em><br />
</br><br />
This use case is especially insidious <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </br><br />
<a href="http://www.sqltreeo.com/wp/practical-examples-of-sql-injection-in-real-world"><img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/sql_injection1.jpg" alt="" title="sql_injection1" width="403" height="1000" class="alignnone size-full wp-image-821" /></a><br />
<AD></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/fun/" title="fun" rel="tag">fun</a>, <a href="http://www.sqltreeo.com/wp/tag/injection/" title="Injection" rel="tag">Injection</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/practical-examples-of-sql-injection-in-real-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Tracking table changes seamlessly with Change Data Capture (CDC)</title>
		<link>http://www.sqltreeo.com/wp/tracking-table-changes-seamlessly-with-change-data-capture-cdc/</link>
		<comments>http://www.sqltreeo.com/wp/tracking-table-changes-seamlessly-with-change-data-capture-cdc/#comments</comments>
		<pubDate>Fri, 15 Jul 2011 07:31:26 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[built-in functions]]></category>
		<category><![CDATA[cdc]]></category>
		<category><![CDATA[Change Data Capture]]></category>
		<category><![CDATA[query functions]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql server agent]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[transaction log]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=807</guid>
		<description><![CDATA[I am going to describe quite forgotten feature in SQL Server 2008. It is Change Data Capture (CDC) &#8211; powerful feature to track changes in database (tables) without touching table&#8217;s schema. There are two features in SQL Server 2008 which support similar thing and look same but they are not &#8211; Change Data Capture (CDC) ...]]></description>
			<content:encoded><![CDATA[<p>I am going to describe quite forgotten feature in SQL Server 2008. It is <strong>Change Data Capture (CDC)</strong> &#8211; powerful feature to track changes in database (tables) without touching table&#8217;s schema. There are two features in SQL Server 2008 which support similar thing and look same but they are not &#8211; <strong>Change Data Capture (CDC)</strong> and <strong>Change Tracking. </strong>I will be focused mainly to CDC in this post.<br />
<AD><br />
But what is the difference between CDC and Change Tracking?</p>
<p>It seems to me that <strong>CDC</strong> is similar to same feature on Oracle &#8211; it is asynchronous tracking of changes to user tables by harvesting transaction log changes and translating them user readable relational format. <strong>Change tracking</strong> does similar thing but is synchronous and provides you less information about changes to tables. It provides only information that table <em>was changed</em> but doesn&#8217;t provide row values for changes which were made. CDC is treated in SQL Server documentation as more robust feature than Change Tracking. Maybe this is the reason why CDC is supported only in Enteprise and Datacenter edition while Change Tracking is supported in all versions. CDC is also mentioned together with ETL process where it has probably most useful usage.</p>
<p>This was very basic description of difference between CDC and Change Tracking. Let&#8217;s look closer to CDC now.</p>
<p>I borrow following schema from <a href="http://msdn.microsoft.com/en-us/library/cc645937.aspx">MSDN</a>, please read this paragraph to end before looking to schema.  As I mentioned above, CDC provides asynchronous tracking of changes by looking to transaction <em>Log</em> and translating recorded changes for <em>Source tables</em>. Changes are gathered by special SQL Server Agent Job (<em>Capture process</em> on following picture). These changes are inserted to <em>Change tables</em> which can be accessed by special user-defined functions created for that purpose  &#8211; they are called <em>Change data capture query functions</em> on following picture. Schema depicts situation when ETL process consumes these functions but this is only sample usage.</p>
<p><a href="http://www.sqltreeo.com/wp/tracking-table-changes-seamlessly-with-change-data-capture-cdc" target="_blank"><img class="alignnone size-full wp-image-808" title="IC156272" src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/IC156272.gif" alt="" width="238" height="399" /></a></p>
<p>&nbsp;</p>
<h4>How to setup Change Data Tracking (CDC)</h4>
<p>Setup is quite straightforward (more than querying changes). Here are steps to bring it alive:</p>
<ol>
<li>Enable CDC for database by calling <strong>sys.sp_cdc_enable_db </strong>procedure. It adds few CDC related system tables to current database and set IS_CDC_ENABLED column in sys.databases to 1.</li>
<li>Enable CDC for particular table by calling <strong>sys.sp_cdc_enable_table</strong>. It creates SQL Server Agent Job for harvesting transaction log, <em>Change tables</em> and <em>Change data capture query function</em>s which enable you to query <em>Change tables</em>. It does few more things like creating &#8220;cdc&#8221; security schema in your database.</li>
<li>Now any changes for enabled table are gathered by job and written to change tables.</li>
</ol>
<h4>How to query changes</h4>
<p>Query changes is not as straightforward as is for Change Tracking feature. It basically let you deal with LSN (Log Sequence Number) directly. LSN is unique identifier for each record in transaction log.</p>
<p>It is recommended to use only <em>Change data capture functions</em> for querying changes &#8211; not <em>Change tables</em> directly. These functions are created dynamically for each CDC-enabled table (created in setup &#8211; step 2.). Let&#8217;s Assume that we have table called &#8220;Customer&#8221; in &#8220;dbo&#8221; schema. There are than two basic function to retrieve changes for that table:</p>
<ul>
<li><strong>cdc.fn_cdc_get_all_changes_dbo_Customer. </strong>This table-valued function returns ALL changes to the Customer table, including previous and new values (depends on parameters). It takes range of LSN you want to search changes for and @row_filter parameter. You can pull LSN range from built-in functions <strong>sys .fn_cdc_get_min_lsn </strong>and <strong>sys .fn_cdc_get_max_lsn </strong>(see T-SQL below). Row filter parameter can have two values &#8211; &#8220;all&#8221; which returns all changes but for changes caused by UPDATEs it doesn&#8217;t include previous values, only new ones. &#8220;all update old&#8221; parameter value returns the same as &#8220;all&#8221; but for UPDATEs returns previous values as well.</li>
<li><strong>cdc .fn_cdc_get_net_changes_dbo_Customer. </strong>This table-valued function returns only takes three parameters as well but returns only final changes to each row. This function is created only if @supports_net_changes parameter is set to 1 while enabling CDC for table. I will not concentrate to this function, please refere to <a href="http://msdn.microsoft.com/en-us/library/bb522511.aspx">MSDN </a>if you should need it.</li>
</ul>
<p>See this short sample how to setup up CDC and query changed data:</p>
<pre class="csharpcode"><span class="rem">-- create Customer table in current DB</span>
<span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> Customer (ID <span class="kwrd">INT</span> <span class="kwrd">IDENTITY</span> (1,1) <span class="kwrd">PRIMARY</span> <span class="kwrd">KEY</span>, Name <span class="kwrd">VARCHAR</span>(50))

<span class="rem">-- Enable CDC for current DB</span>
<span class="kwrd">EXEC</span> sys.sp_cdc_enable_db

<span class="rem">-- Enable CDC for Customer table</span>
<span class="kwrd">EXEC</span> sys.sp_cdc_enable_table @source_schema = <span class="str">'dbo'</span>, @source_name = <span class="str">'Customer'</span>, @role_name=<span class="str">'test'</span>, @supports_net_changes = 1

<span class="rem">-- insert some records</span>
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> Customer (Name) <span class="kwrd">VALUES</span> (<span class="str">'Microsoft'</span>)
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> Customer (Name) <span class="kwrd">VALUES</span> (<span class="str">'Oracle'</span>)
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> Customer (Name) <span class="kwrd">VALUES</span> (<span class="str">'Apple'</span>)

!!! <span class="rem">-- now SQL Server agent job named cdc.&lt;database_name&gt;_capture must be executed</span>

<span class="rem">-- get min LSN for table customer</span>
<span class="kwrd">DECLARE</span> @min_lsn <span class="kwrd">BINARY</span>(10) = sys.fn_cdc_get_min_lsn (<span class="str">'dbo_Customer'</span>)
<span class="rem">-- get max LSN for current DB</span>
<span class="kwrd">DECLARE</span> @max_lsn <span class="kwrd">BINARY</span>(10) = sys.fn_cdc_get_max_lsn ()

<span class="rem">-- select all changes made to Customer table</span>
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> cdc.fn_cdc_get_all_changes_dbo_Customer(@min_lsn, @max_lsn, <span class="str">'all update old'</span>);</pre>
<p>Result is following:</p>
<p><a href="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/simple_cdc_result.png"><img class="alignnone size-full wp-image-812" title="simple_cdc_result" src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/simple_cdc_result.png" alt="" width="614" height="118" /></a></p>
<p>Not nice result, isn&#8217;t it? Result contains all columns from Customer table (ID and Name) and some more. <strong>_$operation</strong> column contains indication of INSERT (2), DELETE (1) or UPDATE with previous values (3), UPDATE for new values (4). <strong>_$update_mask</strong> is bit mask containing information which column was updated (if applicable). Please see full description <a href="http://msdn.microsoft.com/en-us/library/bb510627.aspx">here</a>.</p>
<p>As you see, you must handle LSN directly. To track changed data in increments (probably most common scenario) you would have to store last max LSN value you&#8217;ve processed changes for and start another run with this value.</p>
<p>There are couple of helper fuctions which may help you with LSN:</p>
<ul>
<li><strong>sys.fn_cdc_map_time_to_lsn &#8211; </strong>it map time milestones to LSN</li>
<li><strong>sys.fn_cdc_map_lsn_to_time</strong> &#8211; &#8230; do opposite</li>
<li><strong>sys .fn_cdc_increment_lsn &#8211; </strong>returns next LSN based upon your &#8220;stored&#8221; LSN. Great for tracking incremental changes.</li>
</ul>
<p>That was basics of Change Data Capture (CDC). There&#8217;s really much much much more about it &#8211; if you&#8217;re seriously considering using this feature, read <a href="http://msdn.microsoft.com/en-us/library/bb522489.aspx" target="_blank">full description of CDC in MSDN</a>.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/built-in-functions/" title="built-in functions" rel="tag">built-in functions</a>, <a href="http://www.sqltreeo.com/wp/tag/cdc/" title="cdc" rel="tag">cdc</a>, <a href="http://www.sqltreeo.com/wp/tag/change-data-capture/" title="Change Data Capture" rel="tag">Change Data Capture</a>, <a href="http://www.sqltreeo.com/wp/tag/query-functions/" title="query functions" rel="tag">query functions</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-agent/" title="sql server agent" rel="tag">sql server agent</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/transaction-log/" title="transaction log" rel="tag">transaction log</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/tracking-table-changes-seamlessly-with-change-data-capture-cdc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Do you use T-SQL debugging?</title>
		<link>http://www.sqltreeo.com/wp/do-you-use-t-sql-debugging/</link>
		<comments>http://www.sqltreeo.com/wp/do-you-use-t-sql-debugging/#comments</comments>
		<pubDate>Wed, 13 Jul 2011 23:25:31 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=799</guid>
		<description><![CDATA[T-SQL debugging is not 100% in SQL Server. It has few bad drawbacks starting from difficulties when setting it up and ending with inability to view any data table during debug. I have an idea for post for DBAs who use T-SQL debugging and are interested in improving debugging experience. It is very time consuming ...]]></description>
			<content:encoded><![CDATA[<p>T-SQL debugging is not 100% in SQL Server. It has few bad drawbacks starting from difficulties when setting it up and ending with inability to view any data table during debug.<br />
I have an idea for post for DBAs who use T-SQL debugging and are interested in improving debugging experience. It is very time consuming to prepare that so I would like to know who is really routinely using this feature.<br />
I prepared hassle free answer machine for you which is compatible with RSS readers. I call it &#8220;click-and-forget&#8221; <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<AD><br />
Please click to answer if you are interested in T-SQL debugging related posts:</p>
<div style="width:250px">
<h1><a href="http://www.sqltreeo.com/wp/you-are-great?debugging-yes">YES</a></h1>
<p>or </p>
<h1><a href="http://www.sqltreeo.com/wp/you-are-great?debugging-no">NO</a></h1>
</div>
<p><br/><br />
<br/><br />
Thanks!</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/debugging/" title="debugging" rel="tag">debugging</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/do-you-use-t-sql-debugging/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Copying parent-child rows and SQL Dependency Tracker</title>
		<link>http://www.sqltreeo.com/wp/copying-parent-child-rows-and-sql-dependency-tracker/</link>
		<comments>http://www.sqltreeo.com/wp/copying-parent-child-rows-and-sql-dependency-tracker/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 23:12:50 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[child record]]></category>
		<category><![CDATA[ChildTable]]></category>
		<category><![CDATA[database schema]]></category>
		<category><![CDATA[Dependency]]></category>
		<category><![CDATA[hierarchies]]></category>
		<category><![CDATA[IDENTITY]]></category>
		<category><![CDATA[ParentTable]]></category>
		<category><![CDATA[Red Gate]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=763</guid>
		<description><![CDATA[I was facing quite well-known issue today &#8211; I had to develop logic of duplicating (versioning) parent-child records. Because I was not fully familiar with database schema I simply didn&#8217;t know which entities I should involve. I am very visual person and that&#8217;s why I employed Red Gate&#8217;s tool called SQL Dependency Tracker which my ...]]></description>
			<content:encoded><![CDATA[<p>I was facing quite well-known issue today &#8211; I had to develop logic of duplicating (versioning) parent-child records. Because I was not fully familiar with database schema I simply didn&#8217;t know which entities I should involve. I am very visual person and that&#8217;s why I employed Red Gate&#8217;s tool called <a href="http://www.red-gate.com/products/sql-development/sql-dependency-tracker/">SQL Dependency Tracker</a> which my boss bought me last month. It can display dependencies visually and helped me a lot today. I saw dependencies very fast, observe cardinalities, columns and quite quickly have rough idea what is logic behind  it. Than I started to write SQL.<br />
<AD><br />
Only sensible way how to copy parent-child hierarchies I know is to use OUTPUT clause and place temporary column in affected parent table:</p>
<div style="overflow:auto">
<pre class="csharpcode">    <span class="rem">-- create sample parent-child tables</span>
    <span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> ParentTable (Id <span class="kwrd">INT</span> <span class="kwrd">IDENTITY</span>, Version <span class="kwrd">INT</span>, TempId <span class="kwrd">INT</span>)
    <span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> ChildTable (Id <span class="kwrd">INT</span> <span class="kwrd">IDENTITY</span>, ParentId <span class="kwrd">INT</span>, Name <span class="kwrd">VARCHAR</span>(100)) <span class="rem">-- here is version column redundant just for this sample1</span>

    <span class="rem">-- insert sample data</span>
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ParentTable (Version) <span class="kwrd">VALUES</span> (1)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ParentTable (Version) <span class="kwrd">VALUES</span> (1)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ParentTable (Version) <span class="kwrd">VALUES</span> (1)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ParentTable (Version) <span class="kwrd">VALUES</span> (1)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (1,<span class="str">'I am 1st child record of parent no. 1'</span>)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (1,<span class="str">'I am 2nd child record of parent no. 1'</span>)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (2,<span class="str">'I am 1nd child record of parent no. 2'</span>)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (2,<span class="str">'I am 2nd child record of parent no. 2'</span>)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (3,<span class="str">'I am 1st child record of parent no. 3'</span>)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (3,<span class="str">'I am 2nd child record of parent no. 3'</span>)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (4,<span class="str">'I am 1st child record of parent no. 4'</span>)
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name) <span class="kwrd">VALUES</span> (4,<span class="str">'I am 2nd child record of parent no. 4'</span>)

    <span class="rem">-- display sample data as join between Parent and Child table</span>
    <span class="kwrd">SELECT</span> p.Id ParentId, p.Version ParentVersion, p.TempId <span class="kwrd">as</span> VersionToParentId, c.Id ChildId, c.Name <span class="kwrd">as</span> ChildName 
        <span class="kwrd">FROM</span> ParentTable p
        <span class="kwrd">JOIN</span> ChildTable c <span class="kwrd">ON</span> c.ParentId = p.Id

    <span class="rem">-- here starts copy sql    </span>
    <span class="kwrd">DECLARE</span> @ParentsToCopy <span class="kwrd">TABLE</span> (Id <span class="kwrd">INT</span>)
    <span class="kwrd">DECLARE</span> @ParentsMapping <span class="kwrd">TABLE</span> (OldId <span class="kwrd">INT</span>, ClonedId <span class="kwrd">INT</span>)    <span class="rem">-- temp table holding pairs of old and new ids of parent entities</span>

    <span class="rem">-- parent Ids to copy</span>
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> @ParentsToCopy <span class="kwrd">VALUES</span> (1),(2),(3),(4)

    <span class="rem">-- Copy parents at first and save pairs of OldId and ClonedId</span>
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ParentTable (Version, TempId)
        <span class="kwrd">OUTPUT</span> inserted.TempId, inserted.Id
        <span class="kwrd">INTO</span> @ParentsMapping (OldId, ClonedId)        
        <span class="kwrd">SELECT</span> Version+1, p.Id
            <span class="kwrd">FROM</span> ParentTable p
            <span class="kwrd">JOIN</span> @ParentsToCopy pc <span class="kwrd">ON</span> pc.Id = p.Id <span class="rem">-- filter parent records to those I want to copy</span>

    <span class="rem">--now have in @ParentsMapping pairs of old and new id  of ParentTable entity</span>        

    <span class="rem">--let's insert child records by joining them to @ParentsMapping.OldId</span>
    <span class="rem">-- It finds children records which parents I've copied in previous step and use ClonedId as new ParentId for them</span>
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> ChildTable (ParentId, Name)
        <span class="kwrd">SELECT</span> p.ClonedId, Name
            <span class="kwrd">FROM</span> ChildTable c
            <span class="kwrd">JOIN</span> @ParentsMapping p <span class="kwrd">ON</span> p.OldId = c.ParentId  

    <span class="rem">-- display sample data again </span>
    <span class="kwrd">SELECT</span> p.Id ParentId, p.Version ParentVersion, p.TempId <span class="kwrd">as</span> VersionToParentId,c.Id ChildId,  c.Name <span class="kwrd">as</span> ChildName 
        <span class="kwrd">FROM</span> ParentTable p
        <span class="kwrd">JOIN</span> ChildTable c <span class="kwrd">ON</span> c.ParentId = p.Id</pre>
</div>
<p>&nbsp;<br />
Main point of this script is that you get newly inserted IDs of parent records in @ParentsMapping table and than can use it as source for children records copy operation. But it is not possible without TempId column in ParentTable added only for copy purposes. Reason why you have to add TempId column to parent entity is in limitation of OUTPUT clause. You cannot use anything but only &#8220;inserted.*&#8221; columns or constants with OUTPUT clause. You cannot simply do this:</p>
<pre class="csharpcode">
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> &lt;some_table&gt; (some_columns)
        <span class="kwrd">OUTPUT</span> inserted.*, <span style="color:red">ID_OF_PARENT_RECORD</span>
        <span class="kwrd">INTO</span> &lt;temp_table&gt;
        <span class="kwrd">SELECT</span> <span style="color:red">ID_OF_PARENT_RECORD</span>
            <span class="kwrd">FROM</span> &lt;another_table&gt;</pre>
<p>You have to insert ID_OF_PARENT_RECORD column to ParentTable.TempId column and get it back to @ParentsMapping using &#8220;inserted.TempId&#8221;. That&#8217;s thing I really don&#8217;t like.<br />
If you have better idea how to copy parent-child record (but as bulk operation not row by row) without altering parent entity, please let me know.</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/child-record/" title="child record" rel="tag">child record</a>, <a href="http://www.sqltreeo.com/wp/tag/childtable/" title="ChildTable" rel="tag">ChildTable</a>, <a href="http://www.sqltreeo.com/wp/tag/database-schema/" title="database schema" rel="tag">database schema</a>, <a href="http://www.sqltreeo.com/wp/tag/dependency/" title="Dependency" rel="tag">Dependency</a>, <a href="http://www.sqltreeo.com/wp/tag/hierarchies/" title="hierarchies" rel="tag">hierarchies</a>, <a href="http://www.sqltreeo.com/wp/tag/identity/" title="IDENTITY" rel="tag">IDENTITY</a>, <a href="http://www.sqltreeo.com/wp/tag/parenttable/" title="ParentTable" rel="tag">ParentTable</a>, <a href="http://www.sqltreeo.com/wp/tag/red-gate/" title="Red Gate" rel="tag">Red Gate</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/copying-parent-child-rows-and-sql-dependency-tracker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Select random records using TABLESAMPLE clause</title>
		<link>http://www.sqltreeo.com/wp/select-random-records-using-tablesample-clause/</link>
		<comments>http://www.sqltreeo.com/wp/select-random-records-using-tablesample-clause/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 23:26:03 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Martin Catherall]]></category>
		<category><![CDATA[newid]]></category>
		<category><![CDATA[PERCENT]]></category>
		<category><![CDATA[random records]]></category>
		<category><![CDATA[ROWS]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[TABLESAMPLE]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=683</guid>
		<description><![CDATA[While I was reading Martin Catherall&#8217;s post about Selecting from a table with no rows returned I remembered TABLESAMPLE function which does similar thing. TABLESAMPLE function works together with SELECT statemenet to show sample of rows from respective table. You can use this function to &#8220;randomize&#8221; standard SELECT TOP xx FROM some_table. Usage is following: ...]]></description>
			<content:encoded><![CDATA[<p>While I was reading Martin Catherall&#8217;s post about <a href="http://www.sqlservercentral.com/blogs/martin_catherall/archive/2011/07/04/select-from-a-table-with-no-rows-returned_2E00_.aspx">Selecting from a table with no rows returned</a> I remembered TABLESAMPLE function which does similar thing.<br />
<AD><br />
<a href="http://msdn.microsoft.com/en-us/library/ms189108.aspx">TABLESAMPLE</a> function works together with SELECT statemenet to show sample of rows from respective table.  You can use this function to &#8220;randomize&#8221; standard SELECT TOP xx FROM some_table. <br/><br />
Usage is following:</p>
<pre class="csharpcode">
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> &lt;table_name&gt;  <span class="kwrd">TABLESAMPLE</span> (10 <span class="kwrd">PERCENT</span>)</pre>
<p>&#8230; this will return approximately 10% procent of rows from &lt;table_name&gt;. <br/><br />
It&#8217;s quite ellegant solution for SELECTing news or random records from table for any purpose. How resulting data are randomized in the background? SQL Server sort of randomly picks physical data page and return everything on the page. This means that it is not guaranteed that TABLESAMPLE returns always same number of rows. From table which has 100 000 records, TABLESAMPLE (10 PERCENT) may return  9943 records for the first call and 10 232 for second call. This is because combination of two facts: TABLESAMPLE picks whole content of randomly chosen data page and each data page can carry different number of rows. This behavior might suits you or not. You choose. <br/><br />
You can also combine TABLESAMPLE with TOP clause:</p>
<pre class="csharpcode">
<span class="kwrd">SELECT</span> <span class="kwrd">TOP</span> 10 * <span class="kwrd">FROM</span> &lt;table_name&gt; <span class="kwrd">TABLESAMPLE</span> (10 <span class="kwrd">PERCENT</span>)</pre>
<p>&#8230; but in that case, this solution for selecting exact count of random records is better:</p>
<pre class="csharpcode">
<span class="kwrd">SELECT</span> <span class="kwrd">TOP</span> 10 * <span class="kwrd">FROM</span> &lt;table_name&gt; <span class="kwrd">ORDER</span> <span class="kwrd">BY</span> NEWID() </pre>
<p>It has one &#8220;but&#8221; &#8211; you must use TABLESAMPLE only with tables, not views or table UDF. It make sense, it just comes from its internal working.</br><br />
One more thing. You can use table sample also like this:</p>
<pre class="csharpcode">
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> &lt;table_name&gt;  <span class="kwrd">TABLESAMPLE</span> (1000 <span class="kwrd">ROWS</span>)</pre>
<p>It returns &#8220;approximately&#8221; 1000 rows from &lt;table_name&gt;. But anyway, ROWS clause is internally converted to percentage of rows based on available statistics.<br/></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/martin-catherall/" title="Martin Catherall" rel="tag">Martin Catherall</a>, <a href="http://www.sqltreeo.com/wp/tag/newid/" title="newid" rel="tag">newid</a>, <a href="http://www.sqltreeo.com/wp/tag/percent/" title="PERCENT" rel="tag">PERCENT</a>, <a href="http://www.sqltreeo.com/wp/tag/random-records/" title="random records" rel="tag">random records</a>, <a href="http://www.sqltreeo.com/wp/tag/rows/" title="ROWS" rel="tag">ROWS</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/tablesample/" title="TABLESAMPLE" rel="tag">TABLESAMPLE</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/select-random-records-using-tablesample-clause/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Denali&#8217;s Skeletons in the closet (and workaround), Part II</title>
		<link>http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-ii/</link>
		<comments>http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-ii/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 07:04:32 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[READONLY]]></category>
		<category><![CDATA[sql server 2011]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[udtt]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=561</guid>
		<description><![CDATA[I continued to check SQL Server 2011 &#8220;Denali&#8221; for few things I dislike on 2008 R2 and tried to find out whether they were improved (first part is here). User defined table value types are great step forward in T-SQL programming. They worth in many situations, for me it was encapsulating list of business objects ...]]></description>
			<content:encoded><![CDATA[<p>I continued to check SQL Server 2011 &#8220;Denali&#8221; for few things I dislike on 2008 R2 and tried to find out whether they were improved (first part is <a href="http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-i/">here</a>).<br />
<AD><br />
<a href="http://msdn.microsoft.com/en-us/library/bb522526.aspx">User defined table value types</a> are great step forward in T-SQL programming. They worth in many situations, for me it was encapsulating list of business objects with them and passing them to stored procedures. </p>
<p>Unfortunately you must treat these kind of parameters as readonly (with READONLY clause), so you cannot pass table variable in stored procedure and update it there. </p>
<p>Life will be much easier if you could do this (this code is obviously only schematic):</p>
<pre class="csharpcode">
<span class="rem">--------------------------------------------</span>
<span class="rem">-- create Stock table type</span>
<span class="rem">--------------------------------------------</span>
<span class="kwrd">CREATE</span> TYPE Stock <span class="kwrd">AS</span> <span class="kwrd">TABLE</span> 
(
    <span class="rem">-- many important columns</span>
    <span class="kwrd">Value</span> <span class="kwrd">VARCHAR</span>(500)    
)
<span class="kwrd">GO</span>

<span class="rem">--------------------------------------------</span>
<span class="rem">-- create stored proc which does some complex calculations over list of stocks</span>
<span class="rem">--------------------------------------------</span>
<span class="kwrd">CREATE</span> <span class="kwrd">PROC</span> ap_Do_Some_Very_Complex_Calculation_Over_Stocks
    @stocks Stock  <span class="rem">-- here should be READONLY clause</span>
<span class="kwrd">AS</span>
<span class="kwrd">BEGIN</span>
        
    <span class="kwrd">UPDATE</span> @stocks
        <span class="kwrd">SET</span> <span class="kwrd">Value</span> = <span class="str">'complex calculation'</span>
        
    <span class="rem">-- @stocks variable leaves this procedure changed</span>
<span class="kwrd">END</span>

<span class="rem">--------------------------------------------</span>
<span class="rem">-- do calculations over list of stocks</span>
<span class="rem">--------------------------------------------</span>
<span class="kwrd">DECLARE</span> @stocks <span class="kwrd">AS</span> Stock 

<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> @stocks
    <span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> <span class="rem">-- fill @stocks table from somewhere</span>
    
<span class="kwrd">EXEC</span> ap_Do_Some_Very_Complex_Calculation_Over_Stocks @stocks

<span class="rem">-- here @stocks table content would be changed by procedure</span></pre>
<p>I was eager to try it in SQL Server 2011 &#8220;Denali&#8221; but was dissappointed because there was no improvement of this feature (still hoping that &#8220;yet&#8221;).<br />
Here is proof, you are still required to make table value type readonly while used as SP parameter in SQL Server 2011 &#8220;Denali&#8221;:<br />
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/denali_udtt_readonly.png" alt="" title="denali_udtt_readonly" width="579" height="475" class="alignnone size-full wp-image-565" /></p>
<p><a href="http://martincatherall.com/">Martin Catherall</a> commented my previous post and advised me to raise connect item if not exists yet. Connect item for this issue <a href="http://connect.microsoft.com/SQLServer/feedback/details/299296/relax-restriction-that-table-parameters-must-be-readonly-when-sps-call-each-other">already exists</a> for quite long time but with promising comment from Microsoft that it may be implemented in version 2011. Let&#8217;s hope. </p>
<p>In case this features will not be introduced in SQL Server 2011, you can use &#8220;workaround&#8221; with XML datatype variable as input parameter to stored procedure. This approach is described with connect item I&#8217;ve referred to. But If you are focused at least a little to performance, I wouldn&#8217;t recommend it. XML handling has always performance drawbacks and cannot replace table value type especially if you update it within stored procedure. You can also read excellent article on this theme here &#8211; <a href="http://www.sommarskog.se/tableparam.html">Why Read-only Table Parameters is Not Enough</a>.</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/readonly/" title="READONLY" rel="tag">READONLY</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-2011/" title="sql server 2011" rel="tag">sql server 2011</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/udtt/" title="udtt" rel="tag">udtt</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to merge (combine) date intervals in T-SQL</title>
		<link>http://www.sqltreeo.com/wp/how-to-merge-combine-date-intervals-in-t-sql/</link>
		<comments>http://www.sqltreeo.com/wp/how-to-merge-combine-date-intervals-in-t-sql/#comments</comments>
		<pubDate>Sat, 09 Jul 2011 17:19:38 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[DATEADD]]></category>
		<category><![CDATA[interval]]></category>
		<category><![CDATA[intervals]]></category>
		<category><![CDATA[milestone]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[udtt]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=651</guid>
		<description><![CDATA[On my current project, I am dealing with date intervals in T-SQL very heavily. I&#8217;ve hit interesting issue recently &#8211; how to combine/merge date intervals into more intervals. I am talking about this: I was googling and googling but found only not suitable solutions or solutions which I don&#8217;t like. That&#8217;s why I am now ...]]></description>
			<content:encoded><![CDATA[<p>On my current project, I am dealing with date intervals in T-SQL very heavily. I&#8217;ve hit interesting issue recently &#8211; how to combine/merge date intervals into more intervals.<br />
<br/><br />
<AD><br />
I am talking about this:<br />
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/merge_date_intervals.png" alt="" title="merge_date_intervals" width="552" height="253" class="alignnone size-full wp-image-652" /></p>
<p>I was googling and googling but found only not suitable solutions or solutions which I don&#8217;t like. That&#8217;s why I am now sharing solution which I find quite ellegant. Feel free to comment it or propose anything better. Thanks</p>
<div style="overflow:auto" >
<pre class="csharpcode">
<span class="rem">-- create UDTT which serves as input parameter</span>
<span class="kwrd">CREATE</span> TYPE <span class="kwrd">Interval</span> <span class="kwrd">AS</span> <span class="kwrd">TABLE</span> (datefrom DATETIME, dateto DATETIME)

<span class="rem">-- create UDF which combines date intervals</span>
<span class="kwrd">CREATE</span> <span class="kwrd">FUNCTION</span> fn_MergeIntervals (@intervals <span class="kwrd">Interval</span> READONLY)
<span class="kwrd">RETURNS</span> @<span class="kwrd">output</span> <span class="kwrd">TABLE</span> (datefrom DATETIME, dateto DATETIME)
<span class="kwrd">AS</span> 
<span class="kwrd">BEGIN</span>

    <span class="rem">-- create unique milestones consisting of datefrom and dateto of each interval</span>
    <span class="kwrd">WITH</span> milestones (milestoneOrder, milestone, milestoneType)
    <span class="kwrd">AS</span>
    (
        <span class="kwrd">SELECT</span> 
                    
            ROW_NUMBER() <span class="kwrd">OVER</span> (<span class="kwrd">ORDER</span> <span class="kwrd">BY</span> milestone) milestoneOrder,  <span class="rem">-- create order for each milestone</span>
            milestone,
            milestoneType
            
        <span class="kwrd">FROM</span> (<span class="kwrd">SELECT</span> datefrom <span class="kwrd">as</span> milestone, 1 <span class="kwrd">AS</span> milestoneType <span class="kwrd">FROM</span> @intervals
              <span class="kwrd">UNION</span>
              <span class="kwrd">SELECT</span> dateto, 2 <span class="kwrd">FROM</span> @intervals) <span class="kwrd">AS</span> ms      
    )
    
    <span class="rem">-- join milestone to previous milestone based on ROW_NUMBER orderring</span>
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> @<span class="kwrd">output</span>
        <span class="kwrd">SELECT</span> 
                
            <span class="kwrd">CASE</span> 
                <span class="kwrd">WHEN</span> m.milestoneType = 2 <span class="kwrd">THEN</span> DATEADD(dd, 1, m.milestone) <span class="rem">-- distinguish between starting and ending date</span>
                <span class="kwrd">ELSE</span> m.milestone
            <span class="kwrd">END</span>    datefrom ,    
            <span class="kwrd">CASE</span> 
                <span class="kwrd">WHEN</span> s.milestoneType = 1 <span class="kwrd">THEN</span> DATEADD(dd, -1, s.milestone) <span class="rem">-- distinguish between starting and ending date </span>
                <span class="kwrd">ELSE</span> s.milestone
            <span class="kwrd">END</span>    dateto 

        <span class="kwrd">FROM</span> milestones m
        <span class="kwrd">JOIN</span> milestones s <span class="kwrd">ON</span> s.milestoneOrder = m.milestoneOrder + 1
    
    <span class="kwrd">RETURN</span>
<span class="kwrd">END</span>

<span class="rem">-- fill intervals from sample </span>
<span class="kwrd">DECLARE</span> @intervals <span class="kwrd">AS</span> <span class="kwrd">Interval</span>

<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> @intervals <span class="kwrd">VALUES</span> (<span class="str">'20110101'</span>,<span class="str">'20110430'</span>);
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> @intervals <span class="kwrd">VALUES</span> (<span class="str">'20110420'</span>,<span class="str">'20111005'</span>);
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> @intervals <span class="kwrd">VALUES</span> (<span class="str">'20110415'</span>,<span class="str">'20111130'</span>);

<span class="rem">-- merge intervals    </span>
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> fn_MergeIntervals (@intervals)</pre>
</div>
<p></br><br />
Result is following:<br />
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/merge_date_intervals_result.png" alt="" title="merge_date_intervals_result" width="317" height="148" class="alignnone size-full wp-image-655" /></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/dateadd/" title="DATEADD" rel="tag">DATEADD</a>, <a href="http://www.sqltreeo.com/wp/tag/interval/" title="interval" rel="tag">interval</a>, <a href="http://www.sqltreeo.com/wp/tag/intervals/" title="intervals" rel="tag">intervals</a>, <a href="http://www.sqltreeo.com/wp/tag/milestone/" title="milestone" rel="tag">milestone</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/udtt/" title="udtt" rel="tag">udtt</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/how-to-merge-combine-date-intervals-in-t-sql/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Showing slowest parts of SQL query</title>
		<link>http://www.sqltreeo.com/wp/showing-slowest-part-of-sql-query/</link>
		<comments>http://www.sqltreeo.com/wp/showing-slowest-part-of-sql-query/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 17:28:25 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[performance tuning]]></category>
		<category><![CDATA[query plan]]></category>
		<category><![CDATA[SHOWPLAN]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[t-sql]]></category>
		<category><![CDATA[XSD]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=624</guid>
		<description><![CDATA[I&#8217;ve received interesting question/requirement few days ago: &#8220;&#8230; When viewing a query plan graphically, we usually have to hunt for the most expensive operator.  In some queries, it&#8217;s hard to spot &#8216;em due to the high number of operators.  It would be cool to have a keyboard command that would jump to the most expensive ...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve received interesting question/requirement few days ago:</p>
<p><em>&#8220;&#8230; When viewing a query plan graphically, we usually have to hunt for the  most expensive operator.  In some queries, it&#8217;s hard to spot &#8216;em due to  the high number of operators.  It would be cool to have a keyboard  command that would jump to the most expensive operator, then the next  most expensive, and so on. &#8230;&#8221;</em><br />
<AD><br />
I quite like this idea because from time to time I hit this issue as well. I was playing it with half an hour and found out that it is quite possible by doing this:</p>
<pre class="csharpcode"><span class="kwrd">SET</span> SHOWPLAN_XML <span class="kwrd">ON</span>
<span class="kwrd">GO</span>
<span class="rem">-- query to investigate</span>
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> sys.databases <span class="kwrd">WHERE</span> name = <span class="str">'master'</span>
<span class="kwrd">GO</span>
<span class="kwrd">SET</span> SHOWPLAN_XML OFF</pre>
<p>It produces result containing full XML of query plan which you can freely parse directly in T-SQL or using SQL CLR. XSD schemas for that is available on Microsoft web <a href="http://schemas.microsoft.com/sqlserver/2004/07/showplan/" target="_blank">here</a>. By &#8220;XPathing&#8221; it, you can select e.g. TOP 10 most extensive operators and show it automatically to user as ordinary table. I was googling little bit and found this <a href="http://blog.sqlauthority.com/2009/03/17/sql-server-practical-sql-server-xml-part-one-query-plan-cache-and-cost-of-operations-in-the-cache/" target="_blank">post</a> by famous Pinal Dave. It schematically describes how to parse this XML and is very good start.</p>
<p>I was thinking about <strong>developing mighty stored procedure</strong> which would take your query as dynamic SQL string and would display top 10 bottlenecks in grid. You could bind this procedure to keyboard shortcut in SSMS and have quite nice performance tuning solution. Clean and fast.</p>
<p>It is not that easy because you can&#8217;t use SET SHOWPLAN_XML within stored procedure (it needs separate batch). I googled more and found <strong><a href="http://msdn.microsoft.com/en-us/library/ms189747.aspx" target="_blank">sys.dm_exec_query_plan</a> </strong>function which takes <strong>plan_handle </strong>argument and displays exactly the same as SET SHOWPLAN_XML (and you can use it within procedure). You can pull <strong>plan_handle</strong> value from <strong><a href="http://msdn.microsoft.com/en-us/library/ms177648.aspx">sys.dm_exec_requests</a> </strong>view which contains every request made to SQL Server instance. It needs more investigation and tuning but it could bring this mighty procedure alive.</p>
<p>I am wondering if you are interested in this solution or if you would use it only rarely. Please vote <a href="http://www.sqltreeo.com/wp/might-stored-procedure/" target="_blank">here</a>.</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/performance-tuning/" title="performance tuning" rel="tag">performance tuning</a>, <a href="http://www.sqltreeo.com/wp/tag/query-plan/" title="query plan" rel="tag">query plan</a>, <a href="http://www.sqltreeo.com/wp/tag/showplan/" title="SHOWPLAN" rel="tag">SHOWPLAN</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a>, <a href="http://www.sqltreeo.com/wp/tag/xsd/" title="XSD" rel="tag">XSD</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/showing-slowest-part-of-sql-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Wife and kids are leaving for holiday (it&#8217;s not off-topic)</title>
		<link>http://www.sqltreeo.com/wp/ssms-custom-folders-add-in-download-close-finally/</link>
		<comments>http://www.sqltreeo.com/wp/ssms-custom-folders-add-in-download-close-finally/#comments</comments>
		<pubDate>Thu, 07 Jul 2011 23:13:18 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[Add-In]]></category>
		<category><![CDATA[Context menu]]></category>
		<category><![CDATA[custom folders]]></category>
		<category><![CDATA[Icon]]></category>
		<category><![CDATA[Red Gate]]></category>
		<category><![CDATA[satellite assembly]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[TreeView]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=588</guid>
		<description><![CDATA[My wife and kids are leaving for holiday (without me). Bad thing is that I will miss them, good thing is that I will finally finish my SSMS add-in I promised. If you are not aware about it, read here why I am developing it. After this SSMS add-in development experience I truly admire Red ...]]></description>
			<content:encoded><![CDATA[<p>My wife and kids are leaving for holiday (without me). Bad thing is that I will miss them, good thing is that I will finally finish my <a href="http://www.sqltreeo.com">SSMS add-in</a> I promised. If you are not aware about it, read <a href="http://www.sqltreeo.com/wp/sql-treeo-create-custom-folders-in-ssms/">here</a> why I am developing it.<br />
<AD><br />
After this SSMS add-in development experience I truly admire Red Gate developers for what magic they can do within their SSMS add-ins (especially <a href="http://www.red-gate.com/products/sql-development/sql-source-control/">Red Gate Source Control</a>). It is really biggest development headache I ever had. One example for all &#8211; I was trying to be precise as guys from Red Gate are and put icon to SSMS menu items (I can&#8217;t sleep without that!). It perfectly distinguishes which menu items belong to which add-in, especially if you have several different add-ins installed:<br />
&nbsp;<br />
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/ssms_menu_item_icon2.png" alt="" title="ssms_menu_item_icon2" width="585" height="354" class="alignnone size-full wp-image-612" /><br />
&nbsp;<br />
It was easy but you can&#8217;t use traditional TreeView techniques because SSMS disposes and recreates context menu on every click. That&#8217;s why I solved it by ContextMenuStripChanged event and push my commands if context menu exists.<br />
&nbsp;<br />
It&#8217;s not interesting as next point.<br />
&nbsp;<br />
This was to put icon on SSMS Tools menu item:<br />
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/ssms_menu_item_icon3.png" alt="" title="ssms_menu_item_icon3" width="543" height="300" class="alignnone size-full wp-image-613" /><br />
&nbsp;<br />
I thought &#8211; ok, you&#8217;re former MS Certified Solution Developer for Winforms .NET, no problem to put it there&#8230; NO! It&#8217;s even not possible in C# I had to ask my friend who is familiar with C++ to do that!<br />
&nbsp;<br />
You must create satellite assembly in C++ and register your menu item using this code:</p>
<pre class="csharpcode">
var command = commands.AddNamedCommand2(
                    (AddIn)addInInst,
                    <span class="str">"ShowSQLTreeoSettings"</span>,
                    <span class="str">"SQL Treeo"</span>,
                    <span class="str">"Displays the SQL Treeo Settings Dialog"</span>,
                    <span class="kwrd">false</span>,
                    101,
                    <span class="kwrd">ref</span> contextGuids)</pre>
<p>Where 5th is information to get icon from own resource and 6th parameter is identifier of the icon resource. Whole Google and Interet is almost silence about that, it took  couple of hours <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Anyway, here is &#8220;list&#8221; of features which should work now:</p>
<ul>
<li>Create custom folders in SSMS&#8217;s object explorer using drag&amp;drop. It works for databases, stored procedures, functions, views and tables</li>
<li>Ctrl+click on object will bring ALTER script (my favourite)</li>
<li>Perform bulk operations in explorer-style management dialog &#8211; I did that because I while I was refactoring and re-foldering more objects, drag&amp;drop was not enough</li>
</ul>
<p>If you&#8217;re interested in more features you can let me know <a href="http://www.sqltreeo.com/wp/dalsi/">here</a>.<br />
Please bear with me, it will be available soon.<br />
&nbsp;<br />
PS: I found out that I wasn&#8217;t receiving any emails from contact form during last week, if you registered for notification of release, I probably didn&#8217;t get your email.<br />
<br/><br />
<br/><br />
Jakub Dvorak @ <a href="http://www.sqltreeo.com">www.sqltreeo.com</a></p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/add-in/" title="Add-In" rel="tag">Add-In</a>, <a href="http://www.sqltreeo.com/wp/tag/context-menu/" title="Context menu" rel="tag">Context menu</a>, <a href="http://www.sqltreeo.com/wp/tag/custom-folders/" title="custom folders" rel="tag">custom folders</a>, <a href="http://www.sqltreeo.com/wp/tag/icon/" title="Icon" rel="tag">Icon</a>, <a href="http://www.sqltreeo.com/wp/tag/red-gate/" title="Red Gate" rel="tag">Red Gate</a>, <a href="http://www.sqltreeo.com/wp/tag/satellite-assembly/" title="satellite assembly" rel="tag">satellite assembly</a>, <a href="http://www.sqltreeo.com/wp/tag/sql/" title="SQL" rel="tag">SQL</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/treeview/" title="TreeView" rel="tag">TreeView</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/ssms-custom-folders-add-in-download-close-finally/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Less code with rethrowing &#8220;exception&#8221; in SQL Server 2011</title>
		<link>http://www.sqltreeo.com/wp/less-code-with-rethrowing-exception-in-sql-server-2011/</link>
		<comments>http://www.sqltreeo.com/wp/less-code-with-rethrowing-exception-in-sql-server-2011/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 23:15:38 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[denali]]></category>
		<category><![CDATA[exception]]></category>
		<category><![CDATA[new features]]></category>
		<category><![CDATA[THROW]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=550</guid>
		<description><![CDATA[I wrote about some skeletons in the Denali&#8217;s closet here. Let&#8217;s also put some good on the table. Very handy new thing in Denali is the way how you can rethrow error in catch block. In SQL Server 2008 you must do this to rethrow error: BEGIN TRY -- plenty of code or function/procedures invocation ...]]></description>
			<content:encoded><![CDATA[<p>I wrote about some skeletons in the Denali&#8217;s closet <a href="http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-i/">here</a>. Let&#8217;s also put some good on the table. Very handy new thing in Denali is the way how you can rethrow error in catch block.<br />
<AD><br />
In SQL Server 2008 you must do this to rethrow error:</p>
<pre class="csharpcode">
<span class="kwrd">BEGIN</span> TRY
   
    <span class="rem">-- plenty of code or function/procedures invocation </span>
    <span class="rem">-- where you don't have any error handling </span>

    <span class="rem">-- will it work?</span>
    <span class="kwrd">SELECT</span> 1/0
    
<span class="kwrd">END</span> TRY
<span class="kwrd">BEGIN</span> CATCH

    <span class="kwrd">DECLARE</span> @errorid <span class="kwrd">INT</span> = ERROR_NUMBER()
    <span class="kwrd">DECLARE</span> @<span class="kwrd">proc</span> <span class="kwrd">VARCHAR</span>(200) = ERROR_PROCEDURE()
    <span class="kwrd">DECLARE</span> @ErrorMessage <span class="kwrd">VARCHAR</span>(200) = ERROR_MESSAGE()
    <span class="kwrd">DECLARE</span> @Severity <span class="kwrd">INT</span> = ERROR_SEVERITY()
    <span class="kwrd">DECLARE</span> @<span class="kwrd">State</span> <span class="kwrd">INT</span> = ERROR_STATE()
    
    <span class="kwrd">SET</span> @ErrorMessage = <span class="str">'My error - '</span> + @ErrorMessage
    <span class="kwrd">RAISERROR</span>(@ErrorMessage, @Severity, @<span class="kwrd">State</span>) 

<span class="kwrd">END</span> CATCH</pre>
<p>In SQL Server 2011 you can simply use THROW keyword:</p>
<pre class="csharpcode">
<span class="kwrd">BEGIN</span> TRY
    
    <span class="rem">-- plenty of code or function/procedures invocation </span>
    <span class="rem">-- where you don't have any error handling </span>

    <span class="rem">-- will it work?</span>
    <span class="kwrd">SELECT</span> 1/0
    
<span class="kwrd">END</span> TRY
<span class="kwrd">BEGIN</span> CATCH

    THROW

<span class="kwrd">END</span> CATCH
</pre>
<p>I like that but question is who will re-write my existing code <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<br/><br />
<br/><br />
Jakub Dvorak @ <a href="http://www.sqltreeo.com">www.sqltreeo.com</a></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/denali/" title="denali" rel="tag">denali</a>, <a href="http://www.sqltreeo.com/wp/tag/exception/" title="exception" rel="tag">exception</a>, <a href="http://www.sqltreeo.com/wp/tag/new-features/" title="new features" rel="tag">new features</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/throw/" title="THROW" rel="tag">THROW</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/less-code-with-rethrowing-exception-in-sql-server-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Denali&#8217;s Skeletons in the closet, Part I</title>
		<link>http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-i/</link>
		<comments>http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-i/#comments</comments>
		<pubDate>Wed, 06 Jul 2011 05:23:06 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[sql server 2011]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=542</guid>
		<description><![CDATA[I&#8217;ve started to play little bit with SQL Server 2011 CTP Denali to find out whether some open wounds were healed yet. I like new things in Denali but I&#8217;ve hit one thing I am still terribly missing. This is RAISERROR in function. Today is black day for me because Denali gave me error message ...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started to play little bit with SQL Server 2011 CTP Denali to find out whether some open wounds were healed yet. I like new things in Denali but I&#8217;ve hit one thing I am still terribly missing. This is RAISERROR in function.<br />
<AD><br />
Today is black day for me because Denali gave me error message for that (same as 2008 R2):</p>
<div style="overflow:auto">
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/denali_raiserror_in_function.png" alt="" title="denali_raiserror_in_function" width="983" height="697" class="alignnone size-full wp-image-543" />
</div>
<p>Very sad, I am still doomed to nasty workarounds such as &#8220;ERROR&#8221; string returning or on-purpose division by zero.<br />
Hopefully it will be in final version.<br />
<br/><br />
<br/><br />
Jakub Dvorak @ <a href="http://www.sqltreeo.com">www.sqltreeo.com</a></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/sql-server-2011/" title="sql server 2011" rel="tag">sql server 2011</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/denalis-skeletons-in-the-closet-part-i/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>SQL Server on steroids with RAM disk</title>
		<link>http://www.sqltreeo.com/wp/sql-server-on-steroids-with-ram-disk/</link>
		<comments>http://www.sqltreeo.com/wp/sql-server-on-steroids-with-ram-disk/#comments</comments>
		<pubDate>Tue, 05 Jul 2011 07:00:46 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=529</guid>
		<description><![CDATA[I&#8217;ve faced pretty common situation recently &#8211; you need to work with huge data during development/testing. It&#8217;s good if you have option how to access large data volumes XXX% faster on your laptop. I can imagine many other scenarios when you need to work with hundreds thousands of records on your development machine. Great solution ...]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve faced pretty common situation recently &#8211; you need to work with huge data during development/testing. It&#8217;s good if you have option how to access large data volumes XXX% faster on your laptop. I can imagine many other scenarios when you need to work with hundreds thousands of records on your development machine. Great solution for that is to use RAM disk and store your database on it.<br />
<AD><br />
According to <a href="http://en.wikipedia.org/wiki/RAMDisk">wikipedia</a> RAM Disk &#8220;is a block of RAM (primary storage or volatile memory) that a computer&#8217;s software is treating as if the memory were a disk drive (secondary storage).&#8221;. It does what it does so you can slice part of your memory and create logical disk drive which is treated as drive on system level (by low-level drivers). SQL Server doesn&#8217;t care that this &#8220;disk&#8221; was created as part of your memory.</p>
<p>I used this <a href="http://www.romexsoftware.com/en-us/index.html">tool</a> to create RAM disk. It&#8217;s commercial but more stable than some free alternatives (I am using it also Visual Studio development). </p>
<p>This code performs <strong>simple 100k insert</strong>, I ran it on database stored on SSD disk at first, than on database stored on RAM disk (see results below):</p>
<div style="overflow : auto;">
<pre class="csharpcode">
<span class="rem">-- create database on physical drive c:\ with default setting</span>
<span class="kwrd">CREATE</span> <span class="kwrd">DATABASE</span> [TestPerformance] <span class="kwrd">ON</span>  <span class="kwrd">PRIMARY</span> 
( NAME = N<span class="str">'TestPerformance'</span>, FILENAME = N<span class="str">'C:\Sql Databases\TestPerformance.mdf'</span> , <span class="kwrd">SIZE</span> = 4352KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG <span class="kwrd">ON</span> 
( NAME = N<span class="str">'TestPerformance_log'</span>, FILENAME = N<span class="str">'C:\Sql Databases\TestPerformance_log.LDF'</span> , <span class="kwrd">SIZE</span> = 576KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
<span class="kwrd">GO</span>
<span class="rem">-- create database on ramdisk (drive L:\) with default setting</span>
<span class="kwrd">CREATE</span> <span class="kwrd">DATABASE</span> [TestPerformanceRamdisk] <span class="kwrd">ON</span>  <span class="kwrd">PRIMARY</span> 
( NAME = N<span class="str">'TestPerformanceRamdisk'</span>, FILENAME = N<span class="str">'L:\TestPerformanceRamdisk.mdf'</span> , <span class="kwrd">SIZE</span> = 4096KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
 LOG <span class="kwrd">ON</span> 
( NAME = N<span class="str">'TestPerformanceRamdisk_log'</span>, FILENAME = N<span class="str">'L:\TestPerformanceRamdisk_log.ldf'</span> , <span class="kwrd">SIZE</span> = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
<span class="kwrd">GO</span>

<span class="rem">-- create simple test tables in both databases</span>
<span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> TestPerformance..TestInsert (testColumn UNIQUEIDENTIFIER)
<span class="kwrd">GO</span>
<span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> TestPerformanceRamDisk..TestInsert (testColumn UNIQUEIDENTIFIER)
<span class="kwrd">GO</span>

<span class="rem">-- run this script on both databases and compare time</span>
<span class="kwrd">DECLARE</span> @i <span class="kwrd">INT</span> = 100000
<span class="kwrd">DECLARE</span> @<span class="kwrd">start</span> DATETIME = GETDATE()
<span class="kwrd">DECLARE</span> @<span class="kwrd">end</span> DATETIME 

<span class="kwrd">WHILE</span> @i &gt; 0
<span class="kwrd">BEGIN</span>

    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> TestInsert
        <span class="kwrd">VALUES</span> (NEWID())

    <span class="kwrd">SET</span> @i -= 1
<span class="kwrd">END</span>

<span class="kwrd">SET</span> @<span class="kwrd">end</span> = GETDATE()
<span class="kwrd">SELECT</span> <span class="kwrd">CONVERT</span> (<span class="kwrd">TIME</span>, @<span class="kwrd">end</span> - @<span class="kwrd">start</span>)</pre>
</div>
<p>Results are nice.<br />
For SSD version, it was (~20s):<br />
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/steroids_ssd_results.png" alt="" title="steroids_ssd_results" width="165" height="77" class="alignnone size-full wp-image-530" /></p>
<p>&#8230; for RAM disk version, it was (~7s):<br />
<img src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/07/steroids_ramdisk_results.png" alt="" title="steroids_ramdisk_results" width="161" height="74" class="alignnone size-full wp-image-531" /></p>
<p>RAM disk version is ~300% faster which is not negligible. Using RAM disk needn&#8217;t to be an option only for development machines. You can use RAM disk even for production to hold TEMPDB database (or any other treated as non-critical for persistence). Tony Rogerson&#8217;s wrote about this <a href="http://sqlblogcasts.com/blogs/tonyrogerson/archive/2006/08/24/958.aspx">here</a>.<br />
<br/><br />
<br/><br />
Jakub Dvorak <a href="http://www.sqltreeo.com">@ www.sqltreeo.com</a></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-server-on-steroids-with-ram-disk/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How to re-create database quickly</title>
		<link>http://www.sqltreeo.com/wp/how-to-re-create-database-quickly/</link>
		<comments>http://www.sqltreeo.com/wp/how-to-re-create-database-quickly/#comments</comments>
		<pubDate>Mon, 04 Jul 2011 13:54:42 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=519</guid>
		<description><![CDATA[I was running some database unit tests and needed to drop completely everyhing in my database. There are really many ways how to do that but I like following way because it kills all existing connections automatically and can be executed with one keystroke. It is not any rocket science but trick with bringing database ...]]></description>
			<content:encoded><![CDATA[<p>I was running some database unit tests and needed to drop completely everyhing in my database. There are really many ways how to do that but I like following way because it kills all existing connections automatically and can be executed with one keystroke. It is not any rocket science but trick with bringing database offline and online again solves issue with existing connections. Then it drops and creates empty db again.<br />
<AD><br />
This is it:</p>
<pre class="csharpcode">
<span class="kwrd">USE</span> master
<span class="kwrd">GO</span>
<span class="kwrd">ALTER</span> <span class="kwrd">DATABASE</span> &lt;your_db&gt; <span class="kwrd">SET</span> OFFLINE <span class="kwrd">WITH</span> <span class="kwrd">ROLLBACK</span> <span class="kwrd">IMMEDIATE</span>
<span class="kwrd">GO</span>
<span class="kwrd">ALTER</span> <span class="kwrd">DATABASE</span> &lt;your_db&gt; <span class="kwrd">SET</span> ONLINE <span class="kwrd">WITH</span> <span class="kwrd">ROLLBACK</span> <span class="kwrd">IMMEDIATE</span>
<span class="kwrd">GO</span>
<span class="kwrd">DROP</span> <span class="kwrd">DATABASE</span>  &lt;your_db&gt;
<span class="kwrd">GO</span>
<span class="kwrd">CREATE</span> <span class="kwrd">DATABASE</span> &lt;your_db&gt;
</pre>
<p><br/><br />
<br/><br />
Jakub Dvorak @ <a href="http://www.sqltreeo.com">www.sqltreeo.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/how-to-re-create-database-quickly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cleaner T-SQL with Table Value Constructors</title>
		<link>http://www.sqltreeo.com/wp/cleaner-t-sql-with-table-value-constructors/</link>
		<comments>http://www.sqltreeo.com/wp/cleaner-t-sql-with-table-value-constructors/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 07:33:24 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=412</guid>
		<description><![CDATA[Table Value Constructors were introduced in SQL Server 2008 and enables you to (not only) insert more records with one INSERT statement. &#8220;Standard&#8221; option to insert more records is following: INSERT INTO Continents (Name) VALUES ('Asia') INSERT INTO Continents (Name) VALUES ('Africa') With Table Value Constructors you can do this: INSERT INTO Continents (Name) VALUES ...]]></description>
			<content:encoded><![CDATA[<p>Table Value Constructors were introduced in SQL Server 2008 and enables you to (not only) insert more records with one INSERT statement.<br />
<AD><br />
&#8220;Standard&#8221; option to insert more records is following:</p>
<pre class="csharpcode">
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> Continents (Name) <span class="kwrd">VALUES</span> (<span class="str">'Asia'</span>)
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> Continents (Name) <span class="kwrd">VALUES</span> (<span class="str">'Africa'</span>)</pre>
<p>With Table Value Constructors you can do this:</p>
<pre class="csharpcode">
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> Continents (Name)
<span class="kwrd">VALUES</span> (<span class="str">'Asia'</span>),(<span class="str">'Africa'</span>)</pre>
<p>I must admin that I like it but there&#8217;s more. Sometimes you need list of constants in temporary set to be used just within your batch &#8211; I was always doing something similar to this (if there was only few values):</p>
<pre class="csharpcode">
<span class="kwrd">SELECT</span> <span class="str">'Africa'</span> 
<span class="kwrd">UNION</span> <span class="kwrd">ALL</span> 
<span class="kwrd">SELECT</span> <span class="str">'Asia'</span></pre>
<p>With Table Value Constructors you can do this:</p>
<pre class="csharpcode">
<span class="kwrd">SELECT</span> * 
    <span class="kwrd">FROM</span> (<span class="kwrd">VALUES</span> (<span class="str">'Asia'</span>), (<span class="str">'Africa'</span>)) <span class="kwrd">AS</span> Continents (Name)</pre>
<p>You can even use subquery within VALUES command. Check full reference and limitations of Table Value Constructors on <a href="http://technet.microsoft.com/en-us/library/dd776382.aspx">Technet</a>.<br />
<br/><br />
<br/></p>
<p>Jakub Dvorak @ <a href="http://www.sqltreeo.com">www.sqltreeo.com</a></p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/cleaner-t-sql-with-table-value-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Instead Of Trigger and OUTPUT clause headache</title>
		<link>http://www.sqltreeo.com/wp/get-identity-from-inserted-records-from-instead-of-trigger-using-output-clause/</link>
		<comments>http://www.sqltreeo.com/wp/get-identity-from-inserted-records-from-instead-of-trigger-using-output-clause/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 09:08:19 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=401</guid>
		<description><![CDATA[It is known that you cannot get inserted identity values using OUTPUT clause when inserting to view which has instead of trigger on it. I would like to offer another workaround I was used recently. I am talking about fact that you will not succeed with this: -- inserting records via vw_Customer which has instead ...]]></description>
			<content:encoded><![CDATA[<p>It is known that you cannot get inserted identity values using OUTPUT clause when inserting to view which has instead of trigger on it. I would like to offer another workaround I was used recently.<br />
<AD><br />
I am talking about fact that you will not succeed with this:</p>
<div style="overflow : auto;">
<pre class="csharpcode">
    <span class="rem">-- inserting records via vw_Customer which has instead of trigger on itself. </span>
    <span class="rem">-- Underlying Customer table has Id column defined as IDENTITY.</span>
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> vw_Customer ( Name , Value_FilledBy_Instead_Of_Trigger )
        <span class="kwrd">OUTPUT</span> inserted.Id, inserted.Name, inserted.Value_FilledBy_Instead_Of_Trigger
        <span class="kwrd">INTO</span> @someTable
        <span class="kwrd">SELECT</span> Name, <span class="kwrd">Value</span>
            <span class="kwrd">FROM</span> someOtherTable</pre>
</div>
<p>Result is that you don&#8217;t have identity values in output @someTable. This is behavior described e.g. <a href="http://www.sqlservercentral.com/questions/T-SQL/69989/">here</a> or discussed on Microsoft Connect <a href="http://connect.microsoft.com/SQLServer/feedback/details/427666/instead-trigger-return-zero-number-into-output-identity">here</a>. </p>
<p>I used workaround with process-keyed table and process-keyed view which encapsulates reading/inserting of keyed records. It is similar workaround as using #table but I dislike #table management within triggers. </p>
<p>Check this commented code:</p>
<div style="overflow : auto;">
<pre class="csharpcode">
/*
<span class="rem">-- drops </span>
<span class="kwrd">DROP</span> <span class="kwrd">TABLE</span> Customer 
<span class="kwrd">DROP</span> <span class="kwrd">VIEW</span> vw_Customer
<span class="kwrd">DROP</span> <span class="kwrd">TABLE</span> PK_Customer_TriggerScope
<span class="kwrd">DROP</span> <span class="kwrd">VIEW</span> vw_Customer_TriggerScope
*/

<span class="rem">-- create customer table</span>
<span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> Customer (id <span class="kwrd">INT</span> <span class="kwrd">IDENTITY</span> (1,1) <span class="kwrd">NOT</span> <span class="kwrd">NULL</span>, Name <span class="kwrd">VARCHAR</span>(20) <span class="kwrd">NOT</span> <span class="kwrd">NULL</span>, Value_FilledBy_Instead_Of_Trigger <span class="kwrd">VARCHAR</span>(20) <span class="kwrd">NOT</span> <span class="kwrd">NULL</span>)
<span class="kwrd">GO</span>

<span class="rem">-- create customer view</span>
<span class="kwrd">CREATE</span> <span class="kwrd">VIEW</span> vw_Customer
<span class="kwrd">AS</span>
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> Customer
<span class="kwrd">GO</span>

<span class="rem">-- create process keyed table for usage in instead of trigger</span>
<span class="kwrd">CREATE</span> <span class="kwrd">TABLE</span> PK_Customer_TriggerScope ( SPID <span class="kwrd">INT</span>, Id <span class="kwrd">INT</span>, Name <span class="kwrd">VARCHAR</span>(20), Value_FilledBy_Instead_Of_Trigger <span class="kwrd">VARCHAR</span>(20) )
<span class="kwrd">GO</span>

<span class="rem">-- add default constraint for process keyed table to ensure that SPID is filled/filtered automatically</span>
<span class="kwrd">ALTER</span> <span class="kwrd">TABLE</span> PK_Customer_TriggerScope <span class="kwrd">ADD</span>  <span class="kwrd">CONSTRAINT</span> DF_PK_Customer_TriggerScope_SPID  <span class="kwrd">DEFAULT</span> (@@spid) <span class="kwrd">FOR</span> [SPID]
<span class="kwrd">GO</span>

<span class="rem">-- create view on process keyed table to encapsulate records process keying </span>
<span class="kwrd">CREATE</span> <span class="kwrd">VIEW</span> vw_Customer_TriggerScope
<span class="kwrd">AS</span> 
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> PK_Customer_TriggerScope <span class="kwrd">WHERE</span> SPID = <span class="preproc">@@SPID</span>
<span class="kwrd">GO</span>

<span class="rem">-- create instead of insert trigger on customer view</span>
<span class="kwrd">CREATE</span> <span class="kwrd">TRIGGER</span> tg_IOI_vw_Customer <span class="kwrd">ON</span> vw_Customer INSTEAD <span class="kwrd">OF</span> <span class="kwrd">INSERT</span>
<span class="kwrd">AS</span>
<span class="kwrd">BEGIN</span>

    <span class="rem">-- flush records keyed for this process </span>
    <span class="kwrd">DELETE</span> <span class="kwrd">FROM</span> vw_Customer_TriggerScope
    
    <span class="rem">-- do customer inserts and fill PK_Customer_TriggerScope with output keyed for this process</span>
    <span class="rem">-- unfortunately you cannot use view in output clause so PK_Customer_TriggerScope must be use directly</span>
    <span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> Customer ( Name , Value_FilledBy_Instead_Of_Trigger )
        <span class="kwrd">OUTPUT</span> @@SPID, inserted.Id, inserted.Name, inserted.Value_FilledBy_Instead_Of_Trigger
        <span class="kwrd">INTO</span> PK_Customer_TriggerScope 
        <span class="kwrd">SELECT</span> Name, <span class="str">'Filled by IOI'</span>
            <span class="kwrd">FROM</span> inserted

<span class="kwrd">END</span>
<span class="kwrd">GO</span>

<span class="rem">-- Insert few customers and try to get identity </span>
<span class="kwrd">INSERT</span> <span class="kwrd">INTO</span> vw_Customer ( Name )
    <span class="kwrd">SELECT</span> <span class="kwrd">TOP</span> 10 name <span class="kwrd">FROM</span> msdb.sys.objects <span class="rem">-- use sys.objects as list of customers</span>

<span class="rem">-- get inserted records from process keyed table </span>
<span class="rem">-- it is ensured that now it contains only customer records inserted within instead of trigger</span>
<span class="kwrd">SELECT</span> * <span class="kwrd">FROM</span> vw_Customer_TriggerScope</pre>
</div>
<p><br/><br />
<br/><br />
Jakub Dvorak @ <a href="http://www.sqltreeo.com">www.sqltreeo.com</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/get-identity-from-inserted-records-from-instead-of-trigger-using-output-clause/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scripting dependencies of User-defined Table Types</title>
		<link>http://www.sqltreeo.com/wp/scripting-dependecies-of-user-defined-table-types/</link>
		<comments>http://www.sqltreeo.com/wp/scripting-dependecies-of-user-defined-table-types/#comments</comments>
		<pubDate>Thu, 23 Jun 2011 18:20:01 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[SSMS]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=383</guid>
		<description><![CDATA[I was using user-defined tables types (UDTT) quite intensively despite of their current disadvantages especially their forced READONLY behavior when used as stored procedures parameter. Another disadvantage which I didn&#8217;t realize impact for is that UDTT cannot be altered. I hit maintenance nightmare when they became spread across my T-SQL code. What I did is ...]]></description>
			<content:encoded><![CDATA[<p>I was using <a href="http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters">user-defined tables types</a> (UDTT) quite intensively despite of their current disadvantages especially their forced READONLY behavior when used as <a href="http://www.sommarskog.se/tableparam.html">stored procedures parameter</a>. Another disadvantage which I didn&#8217;t realize impact for is that <strong>UDTT cannot be altered</strong>. I hit maintenance nightmare when they became spread across my T-SQL code.<br />
<AD><br />
What I did is that I encapsulated  few business objects as UDTT and started to use them consistently in my T-SQL stored procedures and functions (directly in code and as input parameters as well). It works great, your code looks &#8220;strongly-typed&#8221; and it is definitely big step forward in T-SQL programming. But whenever I wanted to add or alter field in my UDTT, I had to drop/create all my procedures or functions which this UDTT depends on. It looks peaceful but it&#8217;s totally annoying because you have no built-in option to script references for UDTT and you have to do it manually or through &#8220;script database&#8221; task. If you are in development phase when everything changes from minute to minute, it&#8217;s unusable.</p>
<p>I wrote helper stored procedures which take UDTT name as only parameter and returns DROP and CREATE scripts of all references. It basically do what I&#8217;ve mentioned, it scripts all UDTT&#8217;s references and saves my time.</p>
<p>First is <em>ap_FindReferences</em> which finds all references and returns them as single result set. It would be enough to display this result set as text in SSMS (CTRL+T) but it&#8217;s not because SSMS cannot display strings long more than few thousands characters in results pane. That&#8217;s why there is second procedure <em>ap_WriteReferences </em>which do quite nasty job of writing result from <em>ap_FindReferences </em>into .sql file you can display in SSMS.</p>
<p>Usage is following:</p>
<pre class="csharpcode"><span class="kwrd">EXEC</span> ap_WriteReferences <span class="str">'&lt;your user-defined table type&gt;'</span></pre>
<p>&#8230;result is text file defined within ap_WriteReferences with scripted references to your UDDT.</p>
<p>Good option is to bind this command to keyboard shortcut in SSMS.</p>
<p>Here are those two procedures (some notes follow):</p>
<div style="overflow : auto;">
<pre class="csharpcode"><span class="rem">-- Find all referencing objects to user-defined table type in @fullObjectName parameter</span>
<span class="rem">-- and generate DROP scripts and CREATE scripts for them</span>
<span class="kwrd">CREATE</span> <span class="kwrd">PROC</span> ap_FindReferences (@fullObjectName <span class="kwrd">VARCHAR</span>(200))
<span class="kwrd">AS</span>
<span class="kwrd">BEGIN</span>
    <span class="kwrd">SET</span> NOCOUNT <span class="kwrd">ON</span>

    <span class="kwrd">IF</span> (TYPE_ID (@fullObjectName) <span class="kwrd">IS</span> <span class="kwrd">NULL</span>)
    <span class="kwrd">BEGIN</span>
        <span class="kwrd">RAISERROR</span> (<span class="str">'User-defined table type '</span><span class="str">'%s'</span><span class="str">' does not exists. Include full object name with schema.'</span>, 16,1, @fullObjectName)
        <span class="kwrd">RETURN</span>
    <span class="kwrd">END</span>;

    <span class="kwrd">WITH</span> sources
    <span class="kwrd">AS</span>
    (
        <span class="kwrd">SELECT</span> ROW_NUMBER() <span class="kwrd">OVER</span> (<span class="kwrd">ORDER</span> <span class="kwrd">BY</span> OBJECT_NAME(m.object_id)) RowId, definition
        <span class="kwrd">FROM</span> sys.sql_expression_dependencies d
        <span class="kwrd">JOIN</span> sys.sql_modules m <span class="kwrd">ON</span> m.object_id = d.referencing_id
        <span class="kwrd">JOIN</span> sys.objects o <span class="kwrd">ON</span> o.object_id = m.object_id
        <span class="kwrd">WHERE</span> referenced_id = TYPE_ID(@fullObjectName)
    )

    <span class="kwrd">SELECT</span> 

        <span class="str">'DROP '</span> +
            <span class="kwrd">CASE</span> OBJECTPROPERTY(referencing_id, <span class="str">'IsProcedure'</span>)
            <span class="kwrd">WHEN</span> 1 <span class="kwrd">THEN</span> <span class="str">'PROC '</span>
            <span class="kwrd">ELSE</span>
                <span class="kwrd">CASE</span>
                    <span class="kwrd">WHEN</span> OBJECTPROPERTY(referencing_id, <span class="str">'IsScalarFunction'</span>) = 1 <span class="kwrd">OR</span> OBJECTPROPERTY(referencing_id, <span class="str">'IsTableFunction'</span>) = 1 <span class="kwrd">OR</span> OBJECTPROPERTY(referencing_id, <span class="str">'IsInlineFunction'</span>) = 1 <span class="kwrd">THEN</span> <span class="str">'FUNCTION '</span>
                    <span class="kwrd">ELSE</span> <span class="str">''</span>
                <span class="kwrd">END</span>
            <span class="kwrd">END</span>
        + SCHEMA_NAME(o.schema_id) + <span class="str">'.'</span> +
        + OBJECT_NAME(m.object_id)    

    <span class="kwrd">FROM</span> sys.sql_expression_dependencies d
    <span class="kwrd">JOIN</span> sys.sql_modules m <span class="kwrd">ON</span> m.object_id = d.referencing_id
    <span class="kwrd">JOIN</span> sys.objects o <span class="kwrd">ON</span> o.object_id = m.object_id
    <span class="kwrd">WHERE</span> referenced_id = TYPE_ID(@fullObjectName)
    <span class="kwrd">UNION</span>  <span class="kwrd">ALL</span>
    <span class="kwrd">SELECT</span>  <span class="str">'GO'</span>
    <span class="kwrd">UNION</span>  <span class="kwrd">ALL</span>
    <span class="kwrd">SELECT</span>
        <span class="kwrd">CASE</span>
            <span class="kwrd">WHEN</span> number = RowId    <span class="kwrd">THEN</span> DEFINITION
            <span class="kwrd">ELSE</span> <span class="str">'GO'</span>
        <span class="kwrd">END</span>
     <span class="kwrd">FROM</span> sources s
    <span class="kwrd">JOIN</span> (<span class="kwrd">SELECT</span> <span class="kwrd">DISTINCT</span> number <span class="kwrd">FROM</span> master.dbo.spt_values) n <span class="kwrd">ON</span> n.number <span class="kwrd">BETWEEN</span> RowId <span class="kwrd">AND</span> RowId+1

<span class="kwrd">END</span>
<span class="kwrd">GO</span>

<span class="rem">-- Invokes ap_FindReferences procedure and writes scripted result to .sql file </span>
<span class="kwrd">CREATE</span> <span class="kwrd">PROC</span> ap_WriteReferences
@typeToFind <span class="kwrd">VARCHAR</span>(200)
<span class="kwrd">AS</span>
<span class="kwrd">BEGIN</span>

    <span class="kwrd">DECLARE</span> @sqlCmd <span class="kwrd">VARCHAR</span>(500)
    <span class="kwrd">DECLARE</span> @<span class="kwrd">database</span> <span class="kwrd">VARCHAR</span>(200) = <span class="str">'test'</span>
    <span class="kwrd">DECLARE</span> @outputFile <span class="kwrd">VARCHAR</span>(500) = <span class="str">'c:\refences.sql'</span>

    <span class="kwrd">SET</span> @sqlCmd = <span class="str">'sqlcmd.exe -d '</span>+@<span class="kwrd">database</span>+<span class="str">' -q "EXEC ap_FindReferences '</span><span class="str">''</span>+ @typeToFind +<span class="str">''</span><span class="str">'" -o '</span>+ @outputFile +<span class="str">' -h-1 -y0'</span>

    <span class="kwrd">EXEC</span> xp_cmdshell @sqlCmd

<span class="kwrd">END</span></pre>
</div>
<p>Please pay attention to these notes if you are going to use it:</p>
<ul>
<li>you must change your database name and file name in <em>ap_WriteReferences</em> procedure</li>
<li>it uses <em>sys.sql_modules </em>view to find references, hence it may not have most current information. It worked for me so far but still&#8230;</li>
<li>please use it carefully, it drops objects and it is intended to dedicated development databases only (and at your own risk <img src='http://www.sqltreeo.com/wp/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</li>
<li>it works only on SQL Server 2008 and higher</li>
<li>extended properties are not scripted, it will be forgotten related objects are dropped</li>
</ul>
<p><strong>If you know any better way how to avoid this maintenance issue with UDDT, surely let me know!</strong> Also let me know here if you want me to include it as feature to my &#8220;pocket&#8221; <a href="http://www.sqltreeo.com"><strong>SSMS productivity add-in</strong></a>.</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a>, <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/scripting-dependecies-of-user-defined-table-types/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Red Gate’s SQL Source Control and better SQL objects organization</title>
		<link>http://www.sqltreeo.com/wp/redgate-source-control-and-custom-folders/</link>
		<comments>http://www.sqltreeo.com/wp/redgate-source-control-and-custom-folders/#comments</comments>
		<pubDate>Wed, 22 Jun 2011 23:01:21 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=330</guid>
		<description><![CDATA[Red Gate’s SQL Source Control (RGSSC) is very decent solution source control solution for database development. I am personally using it and despite it is still young product it is very helpful for me and rest of team members. I am not going to review this tool but I am going to look closer how ...]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://www.red-gate.com/products/sql-development/sql-source-control/" target="_blank">Red Gate’s SQL Source Control (RGSSC)</a></strong> is very decent solution source control solution for database development. I am personally using it and despite it is still young product it is very helpful for me and rest of team members. I am not going to review this tool but I am going to look closer how you can improve your experience with it using <strong><a href="http://www.sqltreeo.com">SQL Treeo SSMS Add-In</a></strong>.<br />
<AD><br />
RGSSC is Management Studio Add-In which enables developer to track changes over SQL database and commit/update changes to source control repository (TFS, SVN and others) directly from SQL Server Management Studio (SSMS). For me, using SSMS for database development is still better way comparing to Visual Studio 2010 Database Projects (VS.NET) because I simply not got used to many drawbacks which VS.NET provides from database development point of view. But I will return back to this later.</p>
<p>RGSSC was quite life saver for me because you have quite few options of source control if you&#8217;re developing over dedicated local database. To make my life more comfortable I&#8217;ve developed SQL Treeo Add-In which allows me to create complex hierarchy of folders directly under my database bound to SVN. This folder (or groups) hierarchy is shared across all team members because this database is under source control. You can see short sample video <strong><a href="http://vimeo.com/24504339?autoplay=1" target="_blank">here</a></strong>.</p>
<p>So all team members see the same picture of logical modules directly under our development database (green folders are created using <strong><a href="http://www.sqltreeo.com">SQL Treeo  Add-In</a></strong>):</p>
<p><a href="http://www.sqltreeo.com/wp/wp-content/uploads/2011/06/red_gate_source_control_with_sql_treeo.png"><img class="size-full wp-image-332 alignnone" style="border: 1px solid black;" title="red_gate_source_control_with_sql_treeo" src="http://www.sqltreeo.com/wp/wp-content/uploads/2011/06/red_gate_source_control_with_sql_treeo.png" alt="Red Gate Source Control with custom folders using SQL Treeo Add-In" width="414" height="678" /></a></p>
<p>Logical groups for objects within SSMS standard nodes can be created for all basic SQL objects &#8211; tables, views, stored procedures, functions and even for databases.</p>
<p>Jakub Dvorak</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/redgate-source-control-and-custom-folders/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Stored procedures vs. Do-It-At-Application-Layer-At-All-Costs</title>
		<link>http://www.sqltreeo.com/wp/stored-procedures-vs-do-it-at-application-layer-at-all-costs/</link>
		<comments>http://www.sqltreeo.com/wp/stored-procedures-vs-do-it-at-application-layer-at-all-costs/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 14:52:11 +0000</pubDate>
		<dc:creator>SQLTreeo</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=318</guid>
		<description><![CDATA[Many years back when ASP 2.0 (not ASP.NET) was used as primary server language for web application on Microsoft platform, it was common that business logic was implemented using stored procedures on database side. Nowadays, this approach is used less and less especially because of.NET capabilities to act as full-featured application layer. Big step forward ...]]></description>
			<content:encoded><![CDATA[<p>Many years back when ASP 2.0 (not ASP.NET) was used as primary server language for web application on Microsoft platform, it was common that business logic was implemented using stored procedures on database side. Nowadays, this approach is used less and less especially because of.NET capabilities to act as full-featured application layer. Big step forward were also arise of various ORM tools (such as NHibernate) which did awful of creating CRUD operations completely for developer. I am against any generalization but I am not far from truth if I say that most of newly developed .NET software today uses some kind of object-relational mapping technique at least for very basic SQL operations such as insert, update and delete.<br />
<AD><br />
<strong>NHibernate </strong>is good option but there are still some concept <strong>disadvantages</strong>:</p>
<ul>
<li>Your domain object model must be very well designed and carefully mapped to relational data. ORM creates pressure to developer to sometimes “align” object model to relational model just because mapping could be done easier.</li>
<li>It brings performance drawbacks especially during application startup</li>
<li>You can easily misuse ORM without proper technical background of your ORM tool (such as nhibernate).</li>
<li>You are limited what you can do with your SQL code. It applies to DDL and DML code as well.</li>
</ul>
<p>If you are facing to complex SQL query you can use things like <a href="http://knol.google.com/k/nhibernate-chapter-13-criteria-queries" target="_blank">NHibernate criterias</a> or  <a href="http://ayende.com/blog/2227/implementing-linq-for-nhibernate-a-how-to-guide-part-1" target="_blank">NHibernate Linq provider</a> which may provide you way to build that kind of queries.</p>
<p>There are basically three scenarios where NHibernate is not helpful:</p>
<ul>
<li>Even more complex data retrieval query which you want to fine tune using e.g. <a href="http://msdn.microsoft.com/en-us/library/ms181714.aspx" target="_blank">SQL Server query hints</a>.</li>
<li>Any DML batch operation which affects more rows at one time</li>
<li>Any complex application logic which require &#8220;batch approach&#8221; to your data and cannot be done &#8220;row-by-row&#8221; using your domain model manipulation and its mapping to relational data</li>
</ul>
<p>For those scenarios, you must simply go lower in your application stack and deal with it directly in database. For first scenario you could use database views, for second and third you could use stored procedures mapped as named query in NHibernate.</p>
<p>Imagine even more complex application logic which must be implemented as stored procedures just because of performance &#8211; if you deploy your best developer skills such as encapsulation or reuse, you can deal with many stored procedures and functions (you can organize them with <a href="http://www.sqltreeo.com" target="_blank">SQL Treeo Add-In</a> in your SQL Server Management Studio).</p>
<p><strong>I&#8217;ve identified very bad developer habit &#8220;do-it-at-application-layer-at-all-costs&#8221; which is that developer does even very complex SQL tasks using manipulating object model and ORM. Just not to <em>dirty</em> his code <em></em>with some direct SQL. It inevitably leads to many row-by-tow operations and inefficient queries made by confused OR mapping components.</strong></p>
<p>Head first &#8211; sometimes few stored procedures, functions or views at proper places could make better job than <em>do-it-at-application-layer-at-all-costs.</em></p>
<p>Jakub Dvořák</p>
<div id="_mcePaste" class="mcePaste" style="position: absolute; left: -10000px; top: 129px; width: 1px; height: 1px; overflow: hidden;"><em>dirty</em></div>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/stored-procedures-vs-do-it-at-application-layer-at-all-costs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Static or &#8220;global&#8221; variables in T-SQL using CLR UDT</title>
		<link>http://www.sqltreeo.com/wp/static-or-global-variables-in-t-sql-using-clr-udt/</link>
		<comments>http://www.sqltreeo.com/wp/static-or-global-variables-in-t-sql-using-clr-udt/#comments</comments>
		<pubDate>Sun, 05 Jun 2011 11:40:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[t-sql]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=307</guid>
		<description><![CDATA[I faced problem that I had many lookup tables like ProductType, ClientType etc. and wanted to refer to their values in code to base some logic on them. This is quite common. I am talking about something like following: SELECT @ClientType = ClientType, -- other columns FROM Client WHERE Id = @ClientId IF (@clientType = ...]]></description>
			<content:encoded><![CDATA[<p>I faced problem that I had many lookup tables like ProductType, ClientType etc. and wanted to refer to their values in code to base some logic on them. This is quite common.<br />
<AD><br />
I am talking about something like following:</p>
<pre class="csharpcode"><span class="kwrd">SELECT</span> @ClientType = ClientType, <span class="rem">-- other columns</span>
    <span class="kwrd">FROM</span> Client
    <span class="kwrd">WHERE</span> Id = @ClientId

<span class="kwrd">IF</span> (@clientType = <span class="str">'SOMETYPE'</span>)
    <span class="rem">-- do something</span></pre>
<p>It is quite ok, but practice problem happened if you spread string constants representing lookup values over your T-SQL routines. This means that you have your &#8216;SOMETYPE&#8217; constant defined on many places which is always not good for many reasons. If you would be in C# or other type-safe language you could afford to use constant static variables representing each lookup value and define their string values only at one place.</p>
<p>Such pseudo-hybrid code would look like this:</p>
<pre class="csharpcode"><span class="kwrd">SELECT</span> @ClientType = ClientType, <span class="rem">-- other columns</span>
    <span class="kwrd">FROM</span> Client
    <span class="kwrd">WHERE</span> Id = @ClientId

<span class="kwrd">IF</span> (@clientType = ClientType.SOMETYPE)
    <span class="rem">-- do something</span></pre>
<p>This is of course syntactically not correct in T-SQL but using CLR static variable might be good option.  There are few solutions how to implement &#8220;static&#8221; variables in T-SQL. Among better solutions belongs usage of UDF described e.g. <a href="http://weblogs.asp.net/doubinski/archive/2003/10/16/32187.aspx" target="_blank">here</a> or very limited solution with CONTEXT_INFO described <a href="http://weblogs.sqlteam.com/mladenp/archive/2007/04/23/60185.aspx" target="_blank">here</a>.</p>
<p>I would like to describe other solution which uses SQL CLR User-Defined Type as static class for string constants. I am not going to describe what is CLR UDT and how to develop and deploy them, you can read it <a href="http://msdn.microsoft.com/en-us/library/ms131120.aspx" target="_blank">msdn</a>.</p>
<p>First step is to write and deploy following UDT:</p>
<pre class="csharpcode">[Serializable()]
[SqlUserDefinedType(Format.UserDefined, MaxByteSize = -1)]
<span class="kwrd">public</span> <span class="kwrd">struct</span> ClientType : IBinarySerialize, INullable
{
    <span class="rem">/// &lt;summary&gt;</span>
    <span class="rem">/// TypeA value</span>
    <span class="rem">/// </span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> TypeA { get { <span class="kwrd">return</span> <span class="str">"TYPEA"</span>; } }

    <span class="rem">///
    <span class="rem">/// TypeB value</span>
    <span class="rem">/// </span></span>
    <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">string</span> TypeB { get { <span class="kwrd">return</span> <span class="str">"TYPEB"</span>; } }

    <span class="kwrd">public</span> <span class="kwrd">void</span> Read(BinaryReader r)
    {
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException(<span class="str">"Don't persist this UDT, it holds only constants."</span>);
    }

    <span class="kwrd">public</span> <span class="kwrd">void</span> Write(BinaryWriter w)
    {
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException(<span class="str">"Don't persist this UDT, it holds only constants."</span>);
    }

    <span class="kwrd">public</span> <span class="kwrd">bool</span> IsNull
    {
        get { <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException(); }
    }

    <span class="kwrd">public</span> <span class="kwrd">static</span> ClientType Null
    {
        get
        {
            <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException(<span class="str">"Shouldn't have null value."</span>);
        }
    }

    <span class="kwrd">public</span> <span class="kwrd">override</span> <span class="kwrd">string</span> ToString()
    {
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException(<span class="str">"Don't persist this UDT, it holds only constants."</span>);
    }
    <span class="kwrd">public</span> <span class="kwrd">static</span> ClientType Parse(SqlString s)
    {
        <span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException(<span class="str">"Don't persist this UDT, it holds only constants."</span>);
    }

}</pre>
<p>Second step is to use this UDT in your code:</p>
<pre class="csharpcode"><span class="kwrd">SELECT</span> @ClientType = ClientType, <span class="rem">-- other columns</span>
    <span class="kwrd">FROM</span> Client
    <span class="kwrd">WHERE</span> Id = @ClientId

<span class="kwrd">IF</span> (@clientType = ClientType::TypeA)
    <span class="rem">-- do something</span></pre>
<p>Now you have &#8220;strongly typed&#8221; lookup value used in your T-SQL code. Trick is that you can call static members of UDT from T-SQL by using &#8220;::&#8221; and completely avoid using string constants for lookups throughout your SQL code.</p>
<p>Jakub Dvorak</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/t-sql/" title="t-sql" rel="tag">t-sql</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/static-or-global-variables-in-t-sql-using-clr-udt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is SSMS Addin</title>
		<link>http://www.sqltreeo.com/wp/what-is-ssms-addin/</link>
		<comments>http://www.sqltreeo.com/wp/what-is-ssms-addin/#comments</comments>
		<pubDate>Fri, 03 Jun 2011 19:37:13 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQLServerPedia Syndication]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=293</guid>
		<description><![CDATA[SSMS is abbreviation of SQL Server Management Studio, according to Microsoft description it is &#8220;an integrated environment for accessing, configuring, managing, administering, and developing all components of SQL Server. SQL Server Management Studio combines a broad group of graphical tools with a number of rich script editors to provide access to SQL Server to developers ...]]></description>
			<content:encoded><![CDATA[<p>SSMS is abbreviation of SQL Server Management Studio, according to Microsoft description it is &#8220;an integrated environment for accessing, configuring, managing, administering, and developing all components of SQL Server. SQL Server Management Studio combines a broad group of graphical tools with a number of rich script editors to provide access to SQL Server to developers and administrators of all skill levels&#8221;.<br />
<AD><br />
Microsoft enables developers to enhance or alter features of their development tools (SSMS in this case) by creating &#8220;add-in&#8221; (or addin). Add-In is software component which is installed on same computer as SSMS and is fully integrated in dev. environment. Fully integrated means that your add-in is loaded when SSMS is starting and begin to do its job. SSMS addins are written in .NET and utilize SSMS internal object model (or &#8220;services&#8221;) to achieve their goal. One big issue on SSMS addins development is that mentioned object model (or its interfaces) sometimes dramatically differs from one SQL version to the other. It really depends on addin logic if &#8220;version inconsistent&#8221; interfaces will be hit.</p>
<p>Who is interested in more deeper implementation view, read e.g. this <a href="http://blogs.microsoft.co.il/blogs/shair/archive/2008/07/28/how-to-create-sql-server-management-studio-addin.aspx">article</a>.</p>
<p>Jakub Dvorak</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/what-is-ssms-addin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SQL Treeo &#8211; Create custom folders in SSMS</title>
		<link>http://www.sqltreeo.com/wp/sql-treeo-create-custom-folders-in-ssms/</link>
		<comments>http://www.sqltreeo.com/wp/sql-treeo-create-custom-folders-in-ssms/#comments</comments>
		<pubDate>Wed, 25 May 2011 07:48:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://www.sqltreeo.com/wp/?p=86</guid>
		<description><![CDATA[SQL Treeo is free add-in to SQL Server Management Studio (SSMS) which enables user to create own folder structures within standard SQL Server nodes. With SQL Treeo you can logically separate your databases, stored procedures, tables, views and user-defined functions directly in standard Object Explorer within SSMS. And finally stop dealing with hundreds of objects ...]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://www.sqltreeo.com">SQL Treeo is free add-in to SQL Server Management Studio (SSMS)</a></strong> which enables user to create own folder structures within standard SQL Server nodes. With SQL Treeo you can logically separate your databases, stored procedures, tables, views and user-defined functions directly in standard Object Explorer within SSMS. And finally stop dealing with hundreds of objects contained in one folder within SSMS.<br />
<AD><br />
Reason why SQL Treeo appears is that I was frustrated with continuous browsing for SQL objects in standard e.g. &#8220;Programmability/Stored procedures&#8221; node. Almost every database project provides &#8220;some&#8221; logical structure (based e.g. on modules, parts&#8230;) which should be ideally reflected in naming convention and security schemas. But naming convention and schemas provide only limited possibilities how to achieve that goal and in short while you suddenly have maintainability issue again. It is really helpful if you group your logically related SQL objects into groups/folders and dramatically improve your orientation in SSMS then.</p>
<p>I decided to provide SQL Treeo for <strong>free</strong>.</p>
<p>I hope that this add-in will help others as it helped me.</p>
<p>Jakub Dvorak</p>
<p>&nbsp;</p>

	Tags: <a href="http://www.sqltreeo.com/wp/tag/sql-server/" title="SQL Server" rel="tag">SQL Server</a>, <a href="http://www.sqltreeo.com/wp/tag/ssms/" title="SSMS" rel="tag">SSMS</a><br /><br /><br />
]]></content:encoded>
			<wfw:commentRss>http://www.sqltreeo.com/wp/sql-treeo-create-custom-folders-in-ssms/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
