<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-495177919198719500</atom:id><lastBuildDate>Wed, 15 Feb 2012 05:17:46 +0000</lastBuildDate><category>Integration Services SSIS</category><category>SQL Azure</category><category>Link List</category><category>SQL Data Services</category><category>Transact SQL T-SQL</category><category>SQL Server Training</category><category>Business Intelligence</category><category>SQL Server Administration</category><category>SQL Server Denali</category><category>Analysis Services SSAS</category><category>Reporting Services SSRS</category><category>SQL Server General</category><title>Microsoft Sql Server Tutorials</title><description>Learn how to use SQL Server 2005 and SQL Server 2008 R2 with free SQL Server tutorials.</description><link>http://www.sqlservercurry.com/</link><managingEditor>noreply@blogger.com (Suprotim Agarwal)</managingEditor><generator>Blogger</generator><openSearch:totalResults>604</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/sqlservercurry/blog" /><feedburner:info uri="sqlservercurry/blog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>sqlservercurry/blog</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-7573357691522800741</guid><pubDate>Wed, 15 Feb 2012 05:17:00 +0000</pubDate><atom:updated>2012-02-14T21:17:46.524-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Using sys.dm_os_performance_counters to measure Transactions Per Second</title><description>&lt;div align="justify"&gt;
Performance Monitor tool in SQL Server represents aspects of system performance, such as CPU Usage % , Memory Paging etc. The sys.dm_os_performance_counters Dynamic Management View exposes data of all performance counters for the particular instance of SQL Server. This view is extremely useful when you have to present the performance data of your database in your dashboard.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;Note: If you are handling multiple databases, check out the handy &lt;a href="https://www.idera.com/SQL-Server/SQL-diagnostic-manager/?s=BN300_curry" target="_blank" title="SQL Diagnostics"&gt;&lt;strong&gt;SQL Diagnostic Manager tool&lt;/strong&gt;&lt;/a&gt; from Idera which monitors and manage your entire SQL Server environment from a single console&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
We have touched upon the usefulness of the &lt;code&gt;sys.dm_os_performance_counters &lt;/code&gt;in my previous post &lt;a href="http://www.sqlservercurry.com/2011/09/sql-server-removing-deprecated-code-and.html"&gt;Removing Deprecated Code and Future Proofing your Queries&lt;/a&gt;. In this post, we will see another use of the sys.dm_os_performance_counters view to measure SQL transactions per second for a database. Here’s a very handy query written by Joe Stefanelli&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;DECLARE @ctr bigint&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;SELECT @ctr = ctr     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM sys.dm_os_performance_counters      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE counter_name = 'transactions/sec'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND object_name = 'SQLServer:Databases'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND instance_name = 'SomeDBName'&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;WAITFOR DELAY '00:00:01'&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;SELECT ctr - @ctr     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM sys.dm_os_performance_counters      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE counter_name = 'transactions/sec'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND object_name = 'SQLServer:Databases'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND instance_name = 'DotNetCurry2'&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
As you can see, were taking the difference of two values for a delay of one second to get the number of transactions per second. In future posts, we will see some more queries using this useful DMV.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-7573357691522800741?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/eZ0g_eBIuNQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/eZ0g_eBIuNQ/using-sysdmosperformancecounters-to.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/02/using-sysdmosperformancecounters-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2314436367757864281</guid><pubDate>Tue, 07 Feb 2012 04:38:00 +0000</pubDate><atom:updated>2012-02-06T20:38:35.806-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Training</category><title>SQL Server 2012 Free Cook Book and Training</title><description>Microsoft has created some free content designed to get started with SQL Server 2012.&amp;nbsp; You can use the SQL Server 2012 Early Adoption Cook Book Wiki at &lt;a href="http://social.technet.microsoft.com/wiki/contents/articles/6967.sql-server-2012-early-adoption-cook-book.aspx"&gt;http://social.technet.microsoft.com/wiki/contents/articles/6967.sql-server-2012-early-adoption-cook-book.aspx&lt;/a&gt;. You can bookmark this link as Microsoft to update this wiki with the latest SQL Server 2012 information as and when it is available.&lt;br /&gt;
&lt;br /&gt;
Microsoft also released the SQL Server 2012 Developer Training Kit which includes technical content including labs, demos and presentations designed to help you learn how to develop SQL Server 2012 database and BI solutions.&lt;br /&gt;
&lt;br /&gt;
You can download &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27721" target="_blank" title="SQL Server 2012 Training Kit"&gt;SQL Server 2012 Training Kit over here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-2314436367757864281?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/4Rwpryg7VIg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/4Rwpryg7VIg/sql-server-2012-free-cook-book-and.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/02/sql-server-2012-free-cook-book-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3030232659417967469</guid><pubDate>Mon, 23 Jan 2012 07:51:00 +0000</pubDate><atom:updated>2012-01-25T09:53:27.539-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Declaring Length for VARCHAR and NVARCHAR datatypes IS Necessary</title><description>&lt;div align="justify"&gt;
I have seen some new SQL Server developers declaring Varchar and Nvarchar data types without specifying a length, since it is optional. Although this works in some other programming languages like C#, SQL Server behaves differently and this is a bad practice overall. Let us see with an example of what’s wrong with not declaring a length for the Char or Varchar data type.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
Consider the following t-sql code&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="varchar-length" border="0" height="163" src="http://lh6.ggpht.com/-ChoWCkj9twU/Tx0Qgfxf-ZI/AAAAAAAACZA/eMb7KmtFdxs/varchar-length%25255B2%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="varchar-length" width="454" /&gt;&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="75" src="http://lh3.ggpht.com/-AK1PKuO6axI/Tx0QhyZWZXI/AAAAAAAACZI/WY9lQaatW2s/image%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="266" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Surprised seeing the output? When a length is not specified while declaring a variable, the default length is 1 and when a length is not specified when using the CAST and CONVERT functions, the default length is 30. That’s the reason you get the output you see above.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Try doing the same when you create a table with a CHAR column, without specifying the length.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
To get the desired results, rewrite the t-sql code in the following manner:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;DECLARE @v1 AS char(30)     &lt;br /&gt;DECLARE @v2 AS varchar(60)      &lt;br /&gt;SET @v1 = 'abcdefghijkl'      &lt;br /&gt;SET @v2 = 'The quick brown fox jumped over the lazy dog'&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;SELECT      &lt;br /&gt;DATALENGTH(@v2),      &lt;br /&gt;DATALENGTH(CAST(@v2 as varchar(60)))      &lt;br /&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now you get the desired results&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="79" src="http://lh3.ggpht.com/-LsDuF-kR4ns/Tx0QjEN9soI/AAAAAAAACZQ/lxq5XaXNS7g/image%25255B9%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="280" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Another place where you got to be careful is while creating stored procedures with parameters. If you have created a stored procedure that accepts a parameter with a VARCHAR datatype with no length, you will be in for a surprise to learn that SQL Server silently truncates the string and adds some leading characters.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Overall, &lt;strong&gt;always&lt;/strong&gt; specify a length for the CHAR, VARCHAR, NVARCHAR and similar data types. It’s a good practice!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-3030232659417967469?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/hg4BVXW7Qs0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/hg4BVXW7Qs0/declaring-length-for-varchar-and.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-ChoWCkj9twU/Tx0Qgfxf-ZI/AAAAAAAACZA/eMb7KmtFdxs/s72-c/varchar-length%25255B2%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/01/declaring-length-for-varchar-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5356567576078500699</guid><pubDate>Mon, 09 Jan 2012 11:33:00 +0000</pubDate><atom:updated>2012-01-09T03:36:59.519-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Restoring SQL Server Database: Points to Consider</title><description>&lt;div style="text-align: justify;"&gt;
Here are some important points to consider while restoring a database backup. Let us first create a sample database named test using the following code&lt;/div&gt;
&lt;br /&gt;
&lt;img alt="sql-restore" border="0" height="90" src="http://lh5.ggpht.com/-mgZZihAmCjE/TwrQnjwI24I/AAAAAAAAASk/W-944TfD-14/sql-restore%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-restore" width="438" /&gt;&lt;br /&gt;
&lt;br /&gt;
A new database named &lt;em&gt;test&lt;/em&gt; will be created in your server and data and log files are created&amp;nbsp;in the H: drive.&lt;br /&gt;
&lt;br /&gt;
Let us assume that you regularly take backup of this database using the below code:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;backup database test to disk='h:\test.bak'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Now if you want to create another database or restore this &lt;em&gt;test&lt;/em&gt; database to a new database named &lt;em&gt;testing, y&lt;/em&gt;ou can use the following code&lt;/div&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;restore database testing from disk='h:\test.bak'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
However executing the code above will give you the following error&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red;"&gt;Msg 1834, Level 16, State 1, Line 2     &lt;br /&gt;The file 'h:\test_dat.mdf' cannot be overwritten.&amp;nbsp; It is being used by database 'test'.      &lt;br /&gt;Msg 3156, Level 16, State 4, Line 2      &lt;br /&gt;File 'test_dat' cannot be restored to 'h:\test_dat.mdf'. Use WITH MOVE to identify a valid location for the file.      &lt;br /&gt;Msg 1834, Level 16, State 1, Line 2      &lt;br /&gt;The file 'h:\test_log.ldf' cannot be overwritten.&amp;nbsp; It is being used by database 'test'.      &lt;br /&gt;Msg 3156, Level 16, State 4, Line 2      &lt;br /&gt;File 'test_log' cannot be restored to 'h:\test_log.ldf'. Use WITH MOVE to identify a valid location for the file.      &lt;br /&gt;Msg 3119, Level 16, State 1, Line 2      &lt;br /&gt;Problems were identified while planning for the RESTORE statement. Previous messages provide details.      &lt;br /&gt;Msg 3013, Level 16, State 1, Line 2      &lt;br /&gt;RESTORE DATABASE is terminating abnormally.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
This error occurs because the data and log files are currently being used by the database &lt;em&gt;test. &lt;/em&gt;So you need to give different names for those files while restoring, as shown below&lt;/div&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;restore database testing from disk='h:\test.bak'     &lt;br /&gt;with       &lt;br /&gt;move 'test_dat' to 'h:\testing.mdf',      &lt;br /&gt;move 'test_log' to 'h:\testing.ldf'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The above code will work fine and new database will be created with the name testing.&lt;br /&gt;
&lt;br /&gt;
Just be aware of this point while restoring a backup of existing database!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-5356567576078500699?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/OH27OZ_u6NA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/OH27OZ_u6NA/restoring-sql-server-database-points-to.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-mgZZihAmCjE/TwrQnjwI24I/AAAAAAAAASk/W-944TfD-14/s72-c/sql-restore%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/01/restoring-sql-server-database-points-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4953101097210098380</guid><pubDate>Thu, 29 Dec 2011 17:50:00 +0000</pubDate><atom:updated>2011-12-29T09:52:26.522-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><category domain="http://www.blogger.com/atom/ns#">Link List</category><title>Most Popular SQL Server Articles in 2011</title><description>&lt;div align="justify"&gt;
With 2012 fast approaching and 2011 drawing to an end, we've put together our list of the Most Popular SQL Server articles on SQLServerCurry.com this year. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
I would like to thank each one of you who has visited my blog or contributed to it by submitting a Guest post, &lt;a href="http://feeds2.feedburner.com/sqlservercurry/blog" title="Subscribe to RSS"&gt;Subscribing to RSS Feed&lt;/a&gt;, by &lt;a href="http://twitter.com/suprotimagarwal"&gt;joining me on Twitter&lt;/a&gt;, retweeting posts or promoting the articles and giving regular feedbacks via rating, comments or Emails. A special note of thanks to Madhivannan and Pravin Kumar for their contributions.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Here are some articles that were liked the most by readers like you. Have a very Happy New Year 2012!&lt;/div&gt;
&lt;h4 align="justify"&gt;
SQL Server Administration Articles&lt;/h4&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/12/why-is-raid-important-for-databases.html"&gt;Why is RAID So Important for Databases?&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/09/sql-server-removing-deprecated-code-and.html"&gt;SQL Server: Removing Deprecated Code and Future Proofing your Queries&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/08/net-developer-database-knowledge.html"&gt;Every .NET Developer Should Know About the Database they are working with&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/08/sql-server-2008-move-data-to-different.html"&gt;SQL Server: Move Data to a Different Table using OUTPUT clause&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/08/localdb-denali-new-version-sql-express.html"&gt;LocalDB Denali: New version of SQL Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-top-10-cached-queries.html"&gt;SQL Server: Top 10 Cached Queries&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/troubleshoot-deadlocks-using-sql-server.html"&gt;Troubleshoot Deadlocks using SQL Server Profiler 2005/2008&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/04/monitor-running-processes-in-sql-server.html"&gt;Monitor Running Processes in SQL Server 2005/2008&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-move-table-to-new-file-group.html"&gt;SQL Server: Move Table to a new File Group&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-identify-memory-and.html"&gt;SQL Server: Identify Memory and Performance Issues in T-SQL Queries and Fix them&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/repair-sql-server-database-marked-as.html"&gt;Repair SQL Server Database marked as Suspect or Corrupted&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/fastest-way-to-update-rows-in-large.html"&gt;Fastest Way to Update Rows in a Large Table in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/sql-server-export-table-to-csv.html"&gt;SQL Server: Export Table to CSV&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/load-comma-delimited-file-csv-in-sql.html"&gt;Load Comma Delimited file (csv) in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/backup-compression-in-sql-server-2008.html"&gt;Backup Compression in SQL Server 2008&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 align="justify"&gt;
T-SQL Articles&lt;/h4&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-bit-data-type-and-how-it.html"&gt;SQL Server Bit Data Type and how it Stores Values&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-datetime-vs-datetime2.html"&gt;SQL Server: DateTime vs DateTime2&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-tutorials-on-date-time.html"&gt;SQL Server Tutorials on Date Time&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-row-summary-column-summary.html"&gt;SQL Server: Calculate Summary and Column Summary&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/concatenate-strings-in-sql-server.html"&gt;Concatenate Strings in SQL Server - Different ways&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/sql-server-clr-user-defined-function.html"&gt;SQL Server CLR User Defined Function using Visual Studio 2010&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/sql-queries-beyond-true-and-false.html"&gt;SQL Queries – beyond TRUE and FALSE&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/sql-clr-stored-procedure-using-visual.html"&gt;SQL CLR Stored Procedure using Visual Studio 2010&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/04/sql-server-search-similar-string-in.html"&gt;SQL Server: Search Similar String in a Table&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-count-based-on-condition.html"&gt;SQL Server: Count based on Condition&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-insert-date-and-time-in.html"&gt;SQL Server: Insert Date and Time in Separate Columns&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-combine-multiple-rows-into.html"&gt;SQL Server: Combine Multiple Rows Into One Column with CSV output&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/sql-server-first-and-last-sunday-of.html"&gt;SQL Server: First and Last Sunday of Each Month&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/sql-server-group-by-year-month-and-day.html"&gt;SQL Server: Group By Year, Month and Day&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/sql-server-return-multiple-values-from.html"&gt;SQL Server: Return Multiple Values from a Function&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/unpivot-example-in-sql-server.html"&gt;UNPIVOT example in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/date-difference-in-sql-server-in-days.html"&gt;Date Difference in SQL Server in Days, Hours, Minutes and Seconds&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/minus-keyword-in-sql-server.html"&gt;MINUS Keyword in SQL Server – Alternatives&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/rollback-nested-transactions-in-stored.html"&gt;Rollback Nested Transactions in Stored Procedure - SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/rollback-transaction-in-sql-server.html"&gt;Rollback Transaction in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/select-top-with-ties-in-sql-server.html"&gt;Select TOP With TIES in SQL Server&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-4953101097210098380?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/EMI4TEFMTNI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/EMI4TEFMTNI/most-popular-sql-server-articles-in.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/most-popular-sql-server-articles-in.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-804256898466864769</guid><pubDate>Sat, 24 Dec 2011 09:47:00 +0000</pubDate><atom:updated>2011-12-24T01:51:16.406-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Assign Result of Dynamic SQL to a Variable in SQL Server</title><description>&lt;div align="justify"&gt;
Suppose you have dynamic sql that returns a single value and you want to copy it to a variable. For eg: you want the total count of the table copied to a variable. This is possible in SQL Server using the following methods:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strong&gt;Method 1 : Use sp_executesql system stored procedure&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @counting int&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;execute sp_executesql      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N'select @count=count(*) from sys.objects',      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N'@count int output',      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @count =@counting output;      &lt;br /&gt;select @counting as counting&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the above method, count is assigned to the variable @count which outputs to @counting&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 2 : Use table variable&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @t table(counting int)     &lt;br /&gt;declare @counting int      &lt;br /&gt;insert into @t       &lt;br /&gt;exec('select count(*) from sys.objects')      &lt;br /&gt;select @counting=counting from @t       &lt;br /&gt;select @counting as counting&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
The above t-sql code copiesthe&amp;nbsp; resultset to table variable @t and the count is copied to variable @counting&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
OUTPUT   &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="dynamic-sql-variable" border="0" height="96" src="http://lh4.ggpht.com/-ou7bA6mAYNU/TvWftKqSh7I/AAAAAAAAASc/2kBn58s_yV0/dynamic-sql-variable%25255B6%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="dynamic-sql-variable" width="217" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-804256898466864769?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/dFSSpLVssUs" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/dFSSpLVssUs/sql-server-dynamic-sql-variable.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-ou7bA6mAYNU/TvWftKqSh7I/AAAAAAAAASc/2kBn58s_yV0/s72-c/dynamic-sql-variable%25255B6%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/sql-server-dynamic-sql-variable.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-420890892425818004</guid><pubDate>Mon, 19 Dec 2011 08:08:00 +0000</pubDate><atom:updated>2011-12-19T00:24:42.381-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Why is RAID So Important for Databases?</title><description>&lt;div align="justify"&gt;
A good server design has no, or very few, single points of failure. One of the most common server component that fails, are disks. So data redundancy becomes essential to recoverability. &lt;em&gt;Redundant Array of Independent/Inexpensive Disks (RAID)&lt;/em&gt; is a disk system that provides better fault-tolerance by making use of redundancy of disk(s).&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;RAID is a disk system that contains multiple disk drives, called an array, to provide greater performance, fault tolerance, storage capacity, at a moderate cost. While configuring your server system, you typically have to make a choice between hardware RAID and software RAID for the server’s internal disk drives&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="RAID System" border="0" height="311" src="http://lh6.ggpht.com/-MTz5YJrfO3I/Tu7w-cbKrWI/AAAAAAAACYE/oay_6w3Wujc/image%25255B2%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="RAID System" width="393" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
RAID systems are widely used as storage solutions to get the best I/O performance, depending&amp;nbsp;whether the application is write intensive or read intensive. DBA RayRankins mentions in his book that for database-related applications, in order to minimize disk head movement and maximize I/O performance, it’s a good practice to spread random I/O’s (data changes) and sequential I/O’s (for the transaction log) across different disk subsystems. I agree and endorse his view, as SQL Server, or for that matter any other database, is very much an I/O intensive system.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;&lt;strong&gt;I will be using SQL Server as an example to explain the importance of RAID in databases, however you can implement these concepts in your choice of database. The concepts more or less, remain the same&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Although RAID is not a part of a database like SQL Server, implementing RAID can directly affect the way SQL Server performs.There are many RAID arrays available such as RAID 0, RAID 1, RAID 3, RAID 4, RAID 5, RAID 6, RAID 10 and RAID 01. In this article, we will discuss the ones you will likely encounter as a SQL Server DBA&amp;nbsp;i.e. RAID levels 0, 1, 5 and 10 and also&amp;nbsp; discuss their advantages and disadvantages from a fault tolerance and performance perspective&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;Note: RAID is not a replacement for backups. Backups are very essential for any system.&lt;/em&gt;&lt;/div&gt;
&lt;h4 align="justify"&gt;

Different RAID Levels (Advantages and Disadvantages)&lt;/h4&gt;
&lt;div align="justify"&gt;
We will discussing only RAID 0, 1, 5 and 10 (database perspective)&amp;nbsp;. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 0 – &lt;/strong&gt;Also known as Disk Striping, RAID 0 does not provide redundancy or fault tolerance but instead writes data to two drives, in an alternating fashion. This provides the best read write I/O performance. If you had 8 chunks of data, for example, chunk 1, 3, 5, and 7 would be written to the first drive, and chunk 2, 4, 6, and 8 would be written to the second drive, but all in a fixed (sequential) order. RAID 0 has a simple design, easier to implement and no overheads for parity. The drawback is that any piece of data is on only one disk, so if one disk fails, data stored within those disks are lost.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 1&lt;/strong&gt;- Also known as Disk Mirroring, RAID 1 provides a redundant, identical copy of a selected disk and thus provides good fault tolerance. It can be implemented with 2 drives. The disadvantage is that it has a big storage overhead and a high cost/capacity ratio&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 5&lt;/strong&gt; - Also known as Disk Striping with Parity, stripes data across multiple drives and writes parity bits across drives. Data redundancy is provided by the parity information. It can be implemented with 3 or more disks and is a popular choice amongst DBA’s. Since data and parity information are arranged on the disk array, two types of information are always on different disks. If one disk fails, just replace it with a new disk and the array rebuilds itself. RAID 5 has a higher read rate and makes good use of capacity. The drawback of RAID 5 is slower write rates and slow rebuild times.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 10&lt;/strong&gt; - Also known as mirroring with striping, RAID 10 is a combination of RAID1 + RAID0. RAID 10 uses a striped array of disks that are then mirrored to another identical set of striped disks. This array level uses at least four hard disks and additional disks must be added in even numbers. The data is first placed into mirrored pairs at the lower level. Next, the controller selects a member from each mirrored pair and stripes the data into a new logical volume. Since RAID 10 writes in a random fashion, it provides best performance with a write-intensive application (like video editing). The drawback is that it is expensive.&lt;/div&gt;
&lt;h4 align="justify"&gt;

Which RAID is Suitable for my Database?&lt;/h4&gt;
&lt;div align="justify"&gt;
Now that you have an overview of RAID levels, let’s look at which RAID is suitable for a database. The answer to this question depends on a variety of factors. Do you want availability, performance or cost? What are your requirements for fault tolerance and performance? Here’s a quick snapshot of the performance and fault tolerance provided by RAID&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;&lt;img alt="sqlserver-raid-performance" border="0" height="125" src="http://lh3.ggpht.com/-fVgEGyw3YsE/Tu7w_m--jzI/AAAAAAAACYM/T1nfXm2n9do/sqlserver-raid-performance%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sqlserver-raid-performance" width="554" /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
When it comes to a database like SQL Server, no one RAID level will suit your need. In most cases, SQL Server performs large reads and small writes. So for databases, where write operations are more, RAID 5 is not a good choice. On the contrary, RAID 10 is a good option for databases with more write operations. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Here are some points and best practices to keep in mind while deciding the RAID system for your database. &lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Your Data, Logs, tempdb, and backups should be on separate physical drives or a set of disks (array).&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
RAID1 is often chosen to store operating systems, binaries, index file groups and database transaction log files. Critical to log and index performance is fault tolerance and a good write speed. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Since log file are written sequentially and are read only for recovery operations, recommended RAID for Log Files is RAID 1 or 10. If your RAID 1 is at a 100% usage, choose RAID 10 for better performance.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
For data files with random access and read heavy data volumes, striping is important. So recommended RAID is 5 or 10. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
For data files which require good write performance, RAID 10 is recommended. Use a battery backed caching RAID controller for better write performance&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
For tempdb files with good read/write performance RAID 0, 1 or 10 is recommended. Although tempdb contains temporary data and DBA’s often go in for RAID 0 for tempdb, keep in mind that SQL Server requires tempdb to carry out many of its activities. So if you want your system to be always available, rethink RAID 0.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Putting log files or tempdb on a RAID 5 array is not recommended, since RAID 5 does not perform well for write operations. DBA's although have conflicting opinions over this point.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Choose small fast drives, over large slow drives.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align="justify"&gt;
Make sure you do your own research, so you can make an informed decision! In a real scenario, you may not have the luxury of deciding your server configurations due to the cost associated with it. So keeping these points in mind, may help you in such scenarios.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Please use the comments section and share your opinions!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-420890892425818004?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/kjdS3uoPZus" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/kjdS3uoPZus/why-is-raid-important-for-databases.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-MTz5YJrfO3I/Tu7w-cbKrWI/AAAAAAAACYE/oay_6w3Wujc/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/why-is-raid-important-for-databases.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8474565477060713963</guid><pubDate>Wed, 14 Dec 2011 09:58:00 +0000</pubDate><atom:updated>2011-12-14T01:58:58.981-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Deleting Leading Zeros in a String (SQL)</title><description>A common issue while importing data from different source into SQL Server is often numbers get prefixed with zeroes. If you want to delete these leading zeroes in a string, you can use the following methods:&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;Method 1 : CAST to Bigint&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @s varchar(100)     &lt;br /&gt;set @s ='0000004007340007402100'       &lt;br /&gt;select cast(@s as bigint)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
CASTing the string to Bigint will automatically delete the leading zeroes&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-delete-zeroes" border="0" height="88" src="http://lh4.ggpht.com/-VtppH5HkDxc/TuhzJ-0QS3I/AAAAAAAAASM/mvSzBym1bJ4/sql-delete-zeroes%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-delete-zeroes" width="202" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 2 : Use Replace function&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @s varchar(100)     &lt;br /&gt;set @s ='0000004007340007402100'       &lt;br /&gt;select replace(ltrim(replace(@s,'0',' ')),' ','0')&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The T-SQL code shown above first replaces all zeroes to a single space. The LTRIM function deletes all leading spaces and the second replace function, replaces all spaces to zeroes, so that all leading zeroes are deleted.&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-delete-zeroes" border="0" height="88" src="http://lh5.ggpht.com/-vdS3KdIXhjY/TuhzKpPCW_I/AAAAAAAAASU/BFIweufVYOg/sql-delete-zeroes%25255B12%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-delete-zeroes" width="202" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-8474565477060713963?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/sGp_vh9J7xk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/sGp_vh9J7xk/deleting-leading-zeros-in-string-sql.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-VtppH5HkDxc/TuhzJ-0QS3I/AAAAAAAAASM/mvSzBym1bJ4/s72-c/sql-delete-zeroes%25255B8%25255D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/deleting-leading-zeros-in-string-sql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4542080520258343430</guid><pubDate>Fri, 09 Dec 2011 09:00:00 +0000</pubDate><atom:updated>2011-12-09T01:15:42.795-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Find Last Run Query in SQL Server</title><description>&lt;div align="justify"&gt;
Have you ever wondered what SQL query was last executed by your users across all SQL Server databases on your server? I have seen some solutions on the internet that use the sysprocesses view to retrieve this information. In this post, I will show you how this information can be retrieved better using &lt;a href="http://www.sqlservercurry.com/2010/12/list-dynamic-management-views-dmv-by.html"&gt;Dynamic Management Views&lt;/a&gt;.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Please use this query:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;SELECT &lt;/span&gt;conn&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id&lt;span style="color: grey;"&gt;, &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: magenta;"&gt;host_name&lt;/span&gt;&lt;span style="color: grey;"&gt;, &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;login_name&lt;span style="color: grey;"&gt;, 
 &lt;/span&gt;sqltxt&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;text&lt;/span&gt;&lt;span style="color: grey;"&gt;, &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;login_time&lt;span style="color: grey;"&gt;,  &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;status
FROM &lt;/span&gt;&lt;span style="color: green;"&gt;sys&lt;/span&gt;&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: green;"&gt;dm_exec_connections &lt;/span&gt;conn
&lt;span style="color: grey;"&gt;INNER JOIN &lt;/span&gt;&lt;span style="color: green;"&gt;sys&lt;/span&gt;&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: green;"&gt;dm_exec_sessions &lt;/span&gt;sson 
&lt;span style="color: blue;"&gt;ON &lt;/span&gt;conn&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id &lt;span style="color: grey;"&gt;= &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id
&lt;span style="color: grey;"&gt;CROSS APPLY &lt;/span&gt;&lt;span style="color: green;"&gt;sys&lt;/span&gt;&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: green;"&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;most_recent_sql_handle&lt;span style="color: grey;"&gt;) &lt;/span&gt;&lt;span style="color: blue;"&gt;AS &lt;/span&gt;sqltxt
&lt;span style="color: blue;"&gt;ORDER BY &lt;/span&gt;conn&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Here I&amp;nbsp;have utilized the &lt;a href="http://msdn.microsoft.com/en-us/library/ms181509.aspx" target="_blank"&gt;sys.dm_exec_connections&lt;/a&gt; Dynamic Management View, in conjunction with the &lt;a href="http://msdn.microsoft.com/en-us/library/ms176013.aspx" target="_blank" title="sys.dm_exec_sessions"&gt;sys.dm_exec_sessions&lt;/a&gt; DMV and &lt;a href="http://msdn.microsoft.com/en-us/library/ms181929.aspx" target="_blank"&gt;sys.dm_exec_sql_text&lt;/a&gt; Dynamic Management Function (DMF) to return the last query executed against all SQL Server databases, in that server.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Here’s a quick overview of what these DMV’s and DMF do&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;sys.dm_exec_connections&lt;/strong&gt; - Returns information about the connections established to this instance of SQL Server and the details of each connection&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;sys.dm_exec_sessions&lt;/strong&gt; - Returns one row per authenticated session on SQL Server. sys.dm_exec_sessions is a server-scope view that shows information about all active user connections and internal tasks. This information includes client version, client program name, client login time, login user, current session setting, and more&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;sys.dm_exec_sql_text&lt;/strong&gt; - Returns the text of the SQL batch that is identified by the specified sql_handle&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
In the last statement, we are passing the value contained in the most_recent_sql_handle column of this DMV to the sys.dm_exec_sql_text DMF.&amp;nbsp; The DMF returns the text of the sql query, whose sql_handle we passed to it.&amp;nbsp; This sql_handle that we passed, uniquely identifies the query.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Here’s the output&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;img alt="sql-last-run-query" border="0" height="147" src="http://lh6.ggpht.com/-XA5QuXjo4UY/TuGspwvRUgI/AAAAAAAACXY/fmB_2-J-Nzk/sql-last-run-query%25255B3%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-last-run-query" width="590" /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Also check out &lt;a href="http://www.sqlservercurry.com/2010/07/find-most-time-consuming-code-in-your.html"&gt;&lt;strong&gt;Find the Most Time Consuming Code in your SQL Server Database&lt;/strong&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-4542080520258343430?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/x0fz8Ds-d3s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/x0fz8Ds-d3s/last-run-query-in-sql-server.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-XA5QuXjo4UY/TuGspwvRUgI/AAAAAAAACXY/fmB_2-J-Nzk/s72-c/sql-last-run-query%25255B3%25255D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/last-run-query-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2227272651772067701</guid><pubDate>Sun, 04 Dec 2011 12:39:00 +0000</pubDate><atom:updated>2011-12-04T04:48:05.805-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Activity Monitor in SQL Server</title><description>Activity Monitor is a handy tool in SQL Server to quickly see performance hot spots in the server, network and database activity. One of the most commonly performed tasks in Activity Monitor is to kill a stubborn connection that will not release its resources. In SQL Server 2008, this tool has undergone a major facelift from the 2005 version and helps in tracking your SQL server performance even better than before. &lt;br /&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
To view Activity Monitor in SQL Server 2008, right-click the instance name and choose Activity Monitor or click the Activity Monitor icon on the standard toolbar. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="image" border="0" height="305" src="http://lh4.ggpht.com/-SybfpSLeUgI/TttqDjr6DxI/AAAAAAAACXI/6kMXda3aZcI/image%25255B13%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="422" /&gt; &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
In SQL Server 2005, Activity Monitor can be viewed in SSMS by connecting to the server with Object Explorer, expanding ‘Management’, and then double-click ‘Activity Monitor’.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Once opened, the Activity Monitor Dashboard View contains four graphs which can help identity any abnormal activity in the Database. The graphs include % Processor Time (SQL Server), Waiting Tasks, Database I/O and Batch Requests. The default graph refresh rate is 10 seconds, but you change that easily by right-clicking any of the four graphs and selecting the appropriate refresh interval.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
This snapshot is very helpful to get a quick performance snapshot without the need to use other monitoring tool for the same purpose.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="SQL Activity Monitor" border="0" height="201" src="http://lh4.ggpht.com/-UJioY8Q-PYg/TttqE5thC4I/AAAAAAAACXQ/z_J-v-xQaq4/image%25255B10%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Activity Monitor" width="590" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
This dashboard also contains expandable/collapsible panes to view detailed information about Processes, Resources, I/O and&amp;nbsp;Expensive Queries. Just click on the expand button to the right of each pane to view the detailed information. Here’s a quick overview of the different graphs and what they show.&lt;br /&gt;
&lt;/div&gt;
&lt;dl&gt;
&lt;dt&gt;     &lt;div align="justify"&gt;
&lt;strong&gt;Processor Time&lt;/strong&gt; - The percentage of elapsed time that the processor spends to execute non-idle threads for the instance across all CPUs. The Processes pane gives information on what processes are running, what resources are being utilized etc. You can even right-click a process and choose Trace Process in SQL Server Profiler. Presto!&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;/dt&gt;
&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;dt&gt;     &lt;div align="justify"&gt;
&lt;strong&gt;Waiting Tasks&lt;/strong&gt; - The number of tasks that are waiting for processor, I/O, or memory resources. The Resource Waits pane details the processes waiting for other resources on&amp;nbsp;the server.&amp;nbsp; It shows the latest information from several &lt;a href="http://www.sqlservercurry.com/2010/12/list-dynamic-management-views-dmv-by.html" title="List of SQL Server DMV"&gt;DMVs&lt;/a&gt; like the sys.dm_os_wait_stats&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;/dt&gt;
&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;dt&gt;     &lt;div align="justify"&gt;
&lt;strong&gt;Database I/O&lt;/strong&gt; – Information on data and log files for system and user databases. Provides the transfer rate in MB/Sec, of data from memory to disk, disk to memory, or disk to disk. The pane contains information to&amp;nbsp;quickly detect a contention in disk I/O.&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;/dt&gt;
&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;strong&gt;
&lt;/strong&gt;
&lt;dt&gt;&lt;strong&gt;     &lt;/strong&gt;&lt;div align="justify"&gt;
&lt;strong&gt;Batch Requests/sec&lt;/strong&gt; - The number of SQL Server batches that are received by the instance. The Expensive Query pane lets you find and tune expensive queries. You can even right-click any query and view its graphical execution plan. Also see &lt;a href="http://www.sqlservercurry.com/2010/07/find-most-time-consuming-code-in-your.html"&gt;Find the Most Time Consuming Code in your SQL Server Database&lt;/a&gt;&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;/dt&gt;
&lt;/dl&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note:&lt;/strong&gt; To view the Activity Monitor in SQL Server, a user must have VIEW SERVER STATE permission. Also make sure you close the Activity Monitor tool in SSMS when it is not required. &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-2227272651772067701?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/ldN2NIJaytc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/ldN2NIJaytc/activity-monitor-in-sql-server.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-SybfpSLeUgI/TttqDjr6DxI/AAAAAAAACXI/6kMXda3aZcI/s72-c/image%25255B13%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/activity-monitor-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-6955157414696007489</guid><pubDate>Wed, 30 Nov 2011 04:47:00 +0000</pubDate><atom:updated>2011-11-29T20:50:10.214-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server General</category><title>SQL Server Download Links All Editions</title><description>&lt;div align="justify"&gt;
With SQL Server 2012 RC0 recently announced and a plethora of previous editions already available, it can get tedious to keep track of all these downloads. In this post, I will attempt to share the download links of all SQL Server versions - SQL Server 2012, SQL Server 2008 &amp;amp; R2 and SQL Server 2005. I will update this post and the links as and when applicable.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2012 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;&lt;/strong&gt;Microsoft SQL Server 2012 RC0 enables a cloud-ready information platform.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28145" rel="nofollow" target="_blank" title="Download SQL Server 2012 Release Candidate"&gt;SQL Server 2012 Release Candidate 0 (RC0)&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28151" rel="nofollow" target="_blank" title="Download SQL Server 2012 Express RC0"&gt;SQL Server 2012 Express RC0&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28150" rel="nofollow" target="_blank" title="Download SQL Server 2012 PowerPivot"&gt;SQL Server 2012 PowerPivot for Microsoft Excel 2010 Release Candidate 0 (RC0)&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28149" rel="nofollow" target="_blank" title="Download SQL Server 2012 RC0 Master Data Services"&gt;SQL Server 2012 RC0 Master Data Services Add-in For Microsoft Excel&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28147" rel="nofollow" target="_blank" title="Download  SQL Server 2012 Release Candidate 0 (RC0) Manageability Tool Kit"&gt;SQL Server 2012 Release Candidate 0 (RC0) Manageability Tool Kit&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://msdn.microsoft.com/en-us/data/hh297027" rel="nofollow" target="_blank" title="Download SQL Server Data Tools CTP4"&gt;SQL Server Data Tools CTP4&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27721" rel="nofollow" target="_blank" title="Download SQL Server 2012 Developer Training Kit"&gt;SQL Server 2012 Developer Training Kit Web Installer Preview&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2008 R2 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=6362" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 Evaluation"&gt;SQL Server 2008 R2 Evaluation&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=26727" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 SP1"&gt;SQL Server 2008 R2 Service Pack 1&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=3743" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 Express"&gt;SQL Server 2008 R2 RTM – Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=26729" rel="nofollow" target="_blank" title="SQL Server 2008 R2 SP1 - Express Edition"&gt;SQL Server 2008 R2 SP1 - Express Edition&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=22985" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 Express SSMS"&gt;SQL Server 2008 R2 RTM - Management Studio Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=23650" rel="nofollow" target="_blank" title="SQL Server 2008 R2 RTM - Express with Management Tools"&gt;SQL Server 2008 R2 RTM - Express with Management Tools&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=25174" rel="nofollow" target="_blank" title="SQL Server2008 R2 RTM - Express with Advanced Services"&gt;SQL Server 2008 R2 RTM - Express with Advanced Services&lt;/a&gt;&lt;/div&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-2008-r2-vhd-available-for.html" title="SQL Server 2008 R2 VHD"&gt;SQL Server 2008 R2 VHD&lt;/a&gt;&lt;br /&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2008 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=8850" rel="nofollow" target="_blank" title="Download SQL Server 2008 Trial"&gt;SQL Server 2008 Trial&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27594" rel="nofollow" target="_blank"&gt;SQL Server 2008 Service Pack 3&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=1695" rel="nofollow" target="_blank"&gt;SQL Server 2008 Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27597" rel="nofollow" target="_blank" title="Download SQL Server 2008 Express SP3"&gt;SQL Server 2008 Express Edition Service Pack 3&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=7593" rel="nofollow" target="_blank"&gt;SQL Server 2008 Management Studio Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=22973" rel="nofollow" target="_blank"&gt;SQL Server 2008 Express with Tools&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2005 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
SQL Server 2005 Evaluation is no longer available. You can use the Express Edition&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=B953E84F-9307-405E-BCEB-47BD345BAECE&amp;amp;displaylang=en" rel="nofollow" target="_blank"&gt;SQL Server 2005 SP4 RTM&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=220549B5-0B07-4448-8848-DCC397514B41&amp;amp;displaylang=en" rel="nofollow" target="_blank"&gt;SQL Server 2005 Express Edition&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=15291" rel="nofollow" target="_blank"&gt;Microsoft SQL Server 2005 Express Edition Service Pack 3&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=8961" rel="nofollow" target="_blank" title="Download SQL Server 2005 SSMS Express"&gt;SQL Server Management Studio Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
Other SQL Server Download Links&lt;/h2&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-migration-assistant-for.html"&gt;SQL Server Migration Assistant for Oracle v5.1&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-driver-20-for-php-5.html"&gt;SQL Server Driver 2.0 for PHP 5&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Any important links that I missed out? Share it in the comments section and I will update the post!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-6955157414696007489?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/sIjpjSweohU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/sIjpjSweohU/sql-server-download-links-all-editions.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/11/sql-server-download-links-all-editions.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3939047287992986816</guid><pubDate>Thu, 24 Nov 2011 03:38:00 +0000</pubDate><atom:updated>2011-11-23T19:39:37.352-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server: Test Linked Server Connection</title><description>&lt;div align="justify"&gt;
If you want to have access to data stored in a different server, one of the ways to do so is to make that server &lt;a href="http://www.sqlservercurry.com/2008/03/how-to-set-up-your-database-for.html" title="How to Create Linked Server"&gt;as a Linked Server&lt;/a&gt; to the current server and then query using the linked server name. Let us assume that there is a server named &lt;em&gt;test&lt;/em&gt; available over the network and you want to get some data from there. System stored procedure &lt;a href="http://www.sqlservercurry.com/2008/03/how-to-set-up-your-database-for.html" title="Linked Server"&gt;sp_addlinkedserver&lt;/a&gt; can be used to create a linked server and fire distributed queries.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
The following code adds the server test as Linked server to the current server&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;EXEC sp_addlinkedserver test&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If you want to know if the connection to the linked server is accessible, you can make use of system stored procedure sp_testlinkedserver as shown below&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;EXEC sp_testlinkedserver test&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
It simply checks if the connection is successful. If the result is "Command(s) completed successfully.", it means the connection is successful. This is the simplest way to check if the linked server is accessible&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img alt="linked-server" border="0" height="66" src="http://lh6.ggpht.com/-EbGLIDwaE18/Ts28K9yxhLI/AAAAAAAAASE/So1QpyNhLR4/linked-server%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="linked-server" width="335" /&gt;&lt;br /&gt;
&lt;br /&gt;
Also check &lt;a href="http://www.sqlservercurry.com/2009/06/viewing-linked-server-information-in.html"&gt;Viewing Linked Server Information in SQL Server 2005/2008&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-3939047287992986816?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/FCXrThYom5c" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/FCXrThYom5c/sql-server-test-linked-server.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-EbGLIDwaE18/Ts28K9yxhLI/AAAAAAAAASE/So1QpyNhLR4/s72-c/linked-server%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/11/sql-server-test-linked-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8265953811873037809</guid><pubDate>Sat, 19 Nov 2011 03:29:00 +0000</pubDate><atom:updated>2011-11-18T19:33:29.804-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server: Create Objects Automatically in New Databases</title><description>&lt;div align="justify"&gt;
Suppose whenever a new database is created, you also want to create a stored procedure automatically (by default) in this new database. Let us see the simplest way to do this.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
The Model database is a system database which SQL Server uses as a model to create new databases. So if you create any object in the model database, that object will be created automatically in all newly-created databases. Let us see this with the following examples&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Create this test procedure in model database&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;use model     &lt;br /&gt;GO      &lt;br /&gt;create procedure test      &lt;br /&gt;as      &lt;br /&gt;select 100 as number&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now execute this procedure&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;EXEC test&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
It displays the value 100. &lt;br /&gt;
Now create another database named &lt;em&gt;test&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;CREATE database test     &lt;br /&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now execute the same procedure in test database&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;use test     &lt;br /&gt;GO      &lt;br /&gt;EXEC test&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-create-objects" border="0" height="92" src="http://lh3.ggpht.com/-udjNy2YB98A/Tscitbr6mKI/AAAAAAAAAR8/UAiGqHHk7oA/sql-create-objects%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-create-objects" width="183" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
As you can see, executing the above statement displays the value 100 since this value was taken from the stored procedure in the model database and is now available with test database too. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Feel free to share other approaches to create objects automatically in a database.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-8265953811873037809?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/_pasCT8aGtw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/_pasCT8aGtw/sql-server-create-objects-automatically.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-udjNy2YB98A/Tscitbr6mKI/AAAAAAAAAR8/UAiGqHHk7oA/s72-c/sql-create-objects%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/11/sql-server-create-objects-automatically.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3989348402263538742</guid><pubDate>Mon, 14 Nov 2011 04:58:00 +0000</pubDate><atom:updated>2011-11-14T04:49:53.984-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Generate AlphaNumeric Identity Column in SQL Server</title><description>&lt;div align="justify"&gt;
Let us suppose you have a requirement where you want to have an alphanumeric identity column that has characters and numbers and you want these numbers to be incremental. Eg: Customer ids like cus0000001, cus0000002, etc. You can use any of the following methods&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strong&gt;Method 1 : Store alphanumeric data in column&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @t table(id char(10), names varchar(100))     &lt;br /&gt;insert into @t       &lt;br /&gt;select 'cus0000001','ramesh' union all      &lt;br /&gt;select 'cus0000002','suresh' &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @id int     &lt;br /&gt;select @id=MAX(substring(id,4,len(id)))*1 from @t &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into @t      &lt;br /&gt;select 'cus'+RIGHT(REPLICATE('0',7)+cast(@id+1 as varchar(7)),7),'nilesh'      &lt;br /&gt;select * from @t&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
In the above code, @id is assigned the maximum value of the number excluding characters "cus", which is used again in the INSERT statement&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img alt="alphanumeric-sql" border="0" height="122" src="http://lh3.ggpht.com/--QOozbmit3c/TsCf7Vmh-3I/AAAAAAAAARs/uOcI3wzFkb0/alphanumeric-sql%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="alphanumeric-sql" width="200" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;Method 2 : Use only identity column of int datatype and append alphabets in select statement&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @t table(id int identity(1,1), names varchar(100))     &lt;br /&gt;insert into @t (names)      &lt;br /&gt;select 'ramesh' union all      &lt;br /&gt;select 'suresh' union all      &lt;br /&gt;select 'nilesh'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select 'cus'+right(replicate('0',7)+cast(id as varchar(7)),7) as id,names from @t&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
In the above code, an alphanumeric string is built dynamically in the select statement&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="alphanumeric-sql" border="0" height="130" src="http://lh3.ggpht.com/-KXez3yujbsE/TsCf8jHcCII/AAAAAAAAAR0/9jzw7MvER-M/alphanumeric-sql%25255B17%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="alphanumeric-sql" width="222" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-3989348402263538742?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/fwc-ZVOYaQk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/fwc-ZVOYaQk/generate-alphanumeric-identity-column.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/--QOozbmit3c/TsCf7Vmh-3I/AAAAAAAAARs/uOcI3wzFkb0/s72-c/alphanumeric-sql%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/11/generate-alphanumeric-identity-column.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1538784586608645821</guid><pubDate>Mon, 07 Nov 2011 06:46:00 +0000</pubDate><atom:updated>2011-11-06T22:46:52.821-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server: Find Common Rows in Tables using INTERSECT</title><description>&lt;div align="justify"&gt;
Suppose you have a SQL Server database with tables having similar structures and you want to find out similar rows among these tables. The most common method is to use a JOIN on all the columns.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Consider the following tables&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;create table #t1 (id int, names varchar(100))      &lt;br /&gt;create table #t2 (id int, names varchar(100))       &lt;br /&gt;create table #t3 (id int, names varchar(100)) &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into #t1      &lt;br /&gt;select 1,'test1' union all       &lt;br /&gt;select 2,'test2' &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into #t2      &lt;br /&gt;select 1,'test1' union all       &lt;br /&gt;select 3,'test2' &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into #t3      &lt;br /&gt;select 1,'test1' union all       &lt;br /&gt;select 20,'test2' &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-family: Courier New; font-size: small;"&gt;select t1.* from #t1 as t1      &lt;br /&gt;inner join #t2 as t2 on t1.id=t2.id      &lt;br /&gt;inner join #t3 as t3 on t1.id=t3.id&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
The above select statement joins all these three tables by all columns to get similar rows among these tables. Then these similar rows in all three tables are displayed. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="image" border="0" height="76" src="http://lh5.ggpht.com/-mDJjYgbWS2w/Trd9_poDK5I/AAAAAAAAARU/VVL19_VuZvk/image%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="172" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
The problem with this approach is that if the table has many columns, you will need to specify all the columns in the join statement, which will make maintaining these queries a nightmare.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Another easy method to find common rows without actually specifying any columns is to use the INTERSECT Operator&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="sql-server-intersect" border="0" height="108" src="http://lh6.ggpht.com/-gMbB90k-vsU/Trd-AIL2CoI/AAAAAAAAARc/yqBFM_fmA90/sql-server-intersect%25255B11%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-server-intersect" width="180" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
OUTPUT&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="Sql Server Intersect" border="0" height="76" src="http://lh4.ggpht.com/-zttY3L_SoPk/Trd-AQozJlI/AAAAAAAAARk/rkqUO31tD_M/image%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="Sql Server Intersect" width="172" /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-1538784586608645821?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/9ukRLQfR4ds" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/9ukRLQfR4ds/sql-server-find-common-rows-in-tables.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-mDJjYgbWS2w/Trd9_poDK5I/AAAAAAAAARU/VVL19_VuZvk/s72-c/image%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/11/sql-server-find-common-rows-in-tables.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-7130699307456965399</guid><pubDate>Thu, 03 Nov 2011 05:03:00 +0000</pubDate><atom:updated>2011-11-02T22:03:44.356-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server: Handling Multiple Result sets in a Procedure</title><description>&lt;div align="justify"&gt;
Suppose you have a SQL Server stored procedure that returns multiple result sets and you want to store all these results into another table. I will show you how to do so. Consider the following stored procedure&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;create procedure test     &lt;br /&gt;as      &lt;br /&gt;select 1 as id, 'test1' as name      &lt;br /&gt;select 2 as id, 'test2' as name&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
When you execute this procedure, it returns two result sets. The following code will copy these two result sets in a table variable&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @t table(id int, names varchar(100))     &lt;br /&gt;insert into @t       &lt;br /&gt;exec test&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select * from @t&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;img align="left" alt="sql-proc-multiple" border="0" height="104" src="http://lh3.ggpht.com/-vHBIFA7oe-k/TrIgOuFCycI/AAAAAAAAARE/M498JbD0XHY/sql-proc-multiple%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; float: left; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-proc-multiple" width="173" /&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note:&lt;/strong&gt; In order to copy multiple result sets from a stored procedure, both result sets should have the same number of columns and datatype which are compatible with the target table. If not, an error message will be thrown for invalid number of columns/data type&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-7130699307456965399?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/7IWbk91PHOM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/7IWbk91PHOM/sql-server-handling-multiple-result.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-vHBIFA7oe-k/TrIgOuFCycI/AAAAAAAAARE/M498JbD0XHY/s72-c/sql-proc-multiple%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/11/sql-server-handling-multiple-result.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-9159165689841351289</guid><pubDate>Sun, 30 Oct 2011 03:53:00 +0000</pubDate><atom:updated>2011-10-29T20:53:00.632-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Add Client Name in SQL Profiler Results</title><description>&lt;div style="text-align: justify;"&gt;
&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Often you may need to analyze results from the &lt;a href="http://www.sqlservercurry.com/2011/05/troubleshoot-deadlocks-using-sql-server.html" title="How to use SQL Server Profiler"&gt;SQL Server profiler&lt;/a&gt; to know blocking CPU time, Reads etc. By default the profiler shows you a set of columns in the grid. One of the columns is the ClientProcessId. This column gives information about the Id of the client that accesses the server.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
If you want to know the name of the client, you can add that column in the grid using the following steps&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Start SQL Server profiler &amp;gt; Navigate to File &amp;gt; Templates &amp;gt; Edit template&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;img alt="sql-profiler" border="0" height="252" src="http://lh4.ggpht.com/-5OMfRyzBpBE/TqEDZm1kTZI/AAAAAAAAAQs/Mh4ZvxOTGPA/sql-profiler%25255B12%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-profiler" width="590" /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
A new window will open. Click on "Events Selection" Tab. At the bottom, check the option labeled "Show all columns"&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;img alt="sql-profiler1" border="0" height="376" src="http://lh4.ggpht.com/-1JOHXJXMM4E/TqEDafDJTxI/AAAAAAAAAQ0/X81d6tkykVo/sql-profiler1%25255B7%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-profiler1" width="590" /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
In the window, check Hostname checkbox in the Audit Login event as shown above and click the Save button. Now Restart the Profiler. It will start showing the client name too in the results.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-9159165689841351289?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/TEWP46JgTFc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/TEWP46JgTFc/add-client-name-in-sql-profiler-results.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-5OMfRyzBpBE/TqEDZm1kTZI/AAAAAAAAAQs/Mh4ZvxOTGPA/s72-c/sql-profiler%25255B12%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/10/add-client-name-in-sql-profiler-results.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1684819916755723655</guid><pubDate>Thu, 27 Oct 2011 03:55:00 +0000</pubDate><atom:updated>2011-10-26T20:55:00.938-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server File Location: Points to Consider</title><description>&lt;div align="justify"&gt;
In SQL Server, file locations can be specified in some commands like during bulk insert, listing directory information, using OPENROWSET function, etc. When you specify file location, you should note the following points:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;1.&lt;/strong&gt; By default SQL Server searches for the file location in Server's directory. For example, in a Bulk insert command, if you specify D:\emp.txt, it is by default searched in the Server's    &lt;br /&gt;directory. If your server name is myServer, SQL Server will try to find out the file path in that server only.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;2.&lt;/strong&gt; SQL Server will not recognize client's file location until specified using the UNC path. For example say your Server name is &lt;em&gt;myServer&lt;/em&gt; and the SQL Server client is installed in your local system named &lt;em&gt;myClient &lt;/em&gt;that connects to Server. Say you have a file named emp.txt in your computer’s D drive and you want to specify that file location in the BULK INSERT command. In this case, the command should be&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="bulk insert file location" border="0" height="139" src="http://lh3.ggpht.com/-7pdeMSgM7VA/TqD-j5R_RNI/AAAAAAAAAQk/axJ9VR-2HgE/image%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="bulk insert file location" width="279" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;strong&gt;3.&lt;/strong&gt; When UNC path is specified, the file should be given atleast a read access to the server, in which &lt;/div&gt;
&lt;div align="justify"&gt;
the Query is executed. In the above example, D:\emp.txt file should be given atleast read only access on the server myServer. Otherwise an error "File not found error" will be thrown&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
The same applies to other functions as well which accept file locations - such as xp_cmdshell, OPENROWSET etc.   &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-1684819916755723655?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/cJTLHDTb86E" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/cJTLHDTb86E/sql-server-file-location-points-to.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-7pdeMSgM7VA/TqD-j5R_RNI/AAAAAAAAAQk/axJ9VR-2HgE/s72-c/image%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/10/sql-server-file-location-points-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-607857628728134889</guid><pubDate>Fri, 21 Oct 2011 04:53:00 +0000</pubDate><atom:updated>2011-10-20T21:53:11.737-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server: LPAD and RPAD functions equivalent</title><description>&lt;div align="justify"&gt;
Oracle has two formatting functions LPAD and RPAD which formats the number by appending leading and trailing zeros respectively. SQL Server does not have direct equivalent functions. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
However you can simulate these functions using other string functions available in SQL Server. Let us take a practical example. Suppose you want to export data to fixed format file and the requirement is that the number should be 10 digits long, in such a way that if the total number of digits is less than 10, the remaining digits should be filled with zeroes.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Consider the following example&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img alt="lpad-rpad-sqlserver" border="0" height="121" src="http://lh6.ggpht.com/-yYSB2TWW78s/TqD6FdK44OI/AAAAAAAAAQU/ylktb1iQn2c/lpad-rpad-sqlserver%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="lpad-rpad-sqlserver" width="564" /&gt;    &lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @num int     &lt;br /&gt;set @num=872382      &lt;br /&gt;select       &lt;br /&gt;right(replicate('0',10)+cast(@num as varchar(15)),10) aS lpad_number,      &lt;br /&gt;left(cast(@num as varchar(15))+replicate('0',10),10) as rpad_number&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
The above code shows numbers in two formats. The first is left padded and second is right padded. The replicate function is used to replicate 0 for 10 times and the actual number is converted to string and then appended with zeroes.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
The right function picks the last 10 digits from the result, so that it has 4 leading zeroes, as the original number shown in the code above has only 6 digits. We then reverse the same by converting a number to varchar and appending leading zeroes at the end. We then use the LEFT function which picks the number where last 4 digits are zeroes.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="lpad-rpad-sqlserver-demo" border="0" height="93" src="http://lh6.ggpht.com/-cHYJPWf4Qyo/TqD6F2BStPI/AAAAAAAAAQc/p7xe-NzpbNs/lpad-rpad-sqlserver-demo%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="lpad-rpad-sqlserver-demo" width="313" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-607857628728134889?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/4aTgBCeri8A" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/4aTgBCeri8A/sql-server-lpad-and-rpad-functions.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-yYSB2TWW78s/TqD6FdK44OI/AAAAAAAAAQU/ylktb1iQn2c/s72-c/lpad-rpad-sqlserver%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/10/sql-server-lpad-and-rpad-functions.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1099616426198594627</guid><pubDate>Sun, 16 Oct 2011 07:30:00 +0000</pubDate><atom:updated>2011-10-16T00:30:01.736-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server: Schedule Jobs every Alternate Week</title><description>&lt;div align="justify"&gt;
SQL Server agent is used to schedule a job that has to run periodically. If you are a DBA, one of your admin tasks is to schedule jobs. Suppose you are in a situation to run a set of codes every alternate Saturday, you can do it using the following steps:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Open SQL Server Management Studio (SSMS). Under SQL Server agent, create a new job named &lt;em&gt;test&lt;/em&gt;. Click on Schedules and button called New. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="sql-server-schedule" border="0" height="530" src="http://lh4.ggpht.com/-bqduyeTTdZE/TpP1LivKmvI/AAAAAAAAAQE/4squxVS_pcQ/sql-server-schedule%25255B3%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-server-schedule" width="590" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
A new window will popup. Under frequency section, select weekly for "Occurs" option. Select 2 on "Recurs every" option. Under days option choose "Saturday". Click ok.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="sql-agent-job" border="0" height="516" src="http://lh6.ggpht.com/-x8Yt8j6dWac/TpP1MrjM1jI/AAAAAAAAAQM/vhiE0MrEN8o/sql-agent-job%25255B3%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-agent-job" width="590" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Now the job will run every alternate Saturday on the time specified. &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-1099616426198594627?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/t4vf-4Bh-B8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/t4vf-4Bh-B8/sql-server-schedule-jobs-every.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-bqduyeTTdZE/TpP1LivKmvI/AAAAAAAAAQE/4squxVS_pcQ/s72-c/sql-server-schedule%25255B3%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/10/sql-server-schedule-jobs-every.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-672676303982183641</guid><pubDate>Tue, 11 Oct 2011 07:15:00 +0000</pubDate><atom:updated>2011-10-11T00:15:38.302-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server: Resolving DateTime Conversion Errors</title><description>&lt;div align="justify"&gt;
Sometimes your queries may return errors when using DateTime values. Consider the following select statement&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select cast('19/12/2000' as datetime)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
This statement will fail with the following error message:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="color: red;"&gt;Msg 242, Level 16, State 3, Line 2     &lt;br /&gt;The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
The reason is that SQL Server either does not recognize the input date format or the date is invalid. It is important to know the Server's date setting before using any date formats in your queries.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
There is a DBCC command which can be used to determine the current date format&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: &amp;quot;Courier New&amp;quot;, Courier, monospace;"&gt;DBCC USEROPTIONS&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="dbcc-useroptions" border="0" height="145" src="http://lh6.ggpht.com/-EV0VjACyiIs/TpPsbLjaR3I/AAAAAAAAAP0/Ov2NCdwK1Ow/dbcc-useroptions%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="dbcc-useroptions" width="268" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
When you run the code, the third row gives you the current dateformat. It is &lt;em&gt;mdy &lt;/em&gt;on my machine, so you should input dates in mm-dd-yyyy format. If it is &lt;em&gt;dmy&lt;/em&gt;, you should input it in dd-mm-yyyy format. The input format in most cases, should be based on the server's date format.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Now when you run the following code by formatting dates in &lt;em&gt;mdy&lt;/em&gt; format (in sync with your machine format), it works fine:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select cast('12/19/2000' as datetime)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-date-format" border="0" height="74" src="http://lh5.ggpht.com/-cC2oypenEvk/TpPscDs5PjI/AAAAAAAAAP8/iawxcskHKAs/sql-date-format%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-date-format" width="192" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Note:&lt;/strong&gt; It is also better to use an unambiguous date format YYYYMMDD HH:MM:SS which will work for all date settings.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-672676303982183641?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/h0NmyBMPc28" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/h0NmyBMPc28/sql-server-resolving-datetime.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-EV0VjACyiIs/TpPsbLjaR3I/AAAAAAAAAP0/Ov2NCdwK1Ow/s72-c/dbcc-useroptions%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/10/sql-server-resolving-datetime.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-7361706873791322738</guid><pubDate>Fri, 30 Sep 2011 06:52:00 +0000</pubDate><atom:updated>2011-09-30T04:55:09.179-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server: Removing Deprecated Code and Future Proofing your Queries</title><description>&lt;div align="justify"&gt;
New features are added with every release of SQL Server and as a result, some features get removed or deprecated. Deprecated features are features that are still operational (for backward compatibility) but will be removed in a future version. Deprecated features can be of two types: those that will be deprecated in a future version and those that will be deprecated in the next version.&lt;br /&gt;
&lt;br /&gt;
In this article, we will explore how to track deprecated code and correct it. I will also share our observations when we performed the same&amp;nbsp;operation on a live production database. Please feel free to &lt;a href="http://twitter.com/home?status=%22SQL%20Server%3A%20Removing%20Deprecated%20Code%20and%20Future%20Proofing%20your%20Queries%22%20%23sqlserver%20%23sql%20http%3A%2F%2Fbit.ly%2Fnam1fZ" target="_blank"&gt;&lt;strong&gt;retweet and share&lt;/strong&gt;&lt;/a&gt; this link with fellow developers.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Tracking Deprecated Code in your Database&lt;/strong&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Tracking deprecated features can help you identify potential future bugs and upgrade and compatibility problems in your database. SQL Server 2008 provides the following methods to detect deprecated code in your database:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
&lt;em&gt;SQL Server: Deprecated Features&lt;/em&gt; performance counter &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Deprecation category of trace events that includes the &lt;em&gt;Deprecation Announcement&lt;/em&gt; and &lt;em&gt;Deprecation Final Support&lt;/em&gt; event classes, which can be monitored using SQL Server Profiler or SQL Trace. You can use event notification and track these events via a stored procedure.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
sqlserver.deprecation_announcement and the sqlserver.deprecation_final_support Extended events&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Performance Monitor Tool (perfmon.exe) to display the value of counters for SQL Server: deprecated features object.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;SQL Trace Collector to automate the collection of deprecation events&lt;/li&gt;
&lt;/ul&gt;
We will discuss the &lt;em&gt;SQL Server: Deprecated Features&lt;/em&gt; performance counter&amp;nbsp; and PerfMon tool in this article. &lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Observe the following code which uses the Deprecated Feature performance counter and gives a feel of how the deprecation feature works:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img alt="sql deprecated features" border="0" height="72" src="http://lh5.ggpht.com/-aOSgsr3WRIQ/ToVmSUJ6yRI/AAAAAAAACKc/7nyxtBTYED4/image4.png?imgmax=800" style="background-image: none; border-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql deprecated features" width="456" /&gt;&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="SQL Deprecated Code" border="0" height="180" src="http://lh3.ggpht.com/-zg1vEWKEWOI/ToVmTOmJt3I/AAAAAAAACKg/SPCZ1yKmldY/image9.png?imgmax=800" style="background-image: none; border-width: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Deprecated Code" width="494" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
The sys.dm_os_performance_counters dynamic management view (DMV) maintains a count every time a deprecated feature is used. The code shown above is for a named instance MSSQL$&amp;lt;instancename&amp;gt;. For a SQL Server default instance, just replace the above code with this one&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;SELECT      &lt;br /&gt;object_name, instance_name, cntr_value       &lt;br /&gt;FROM sys.dm_os_performance_counters       &lt;br /&gt;WHERE object_name = 'SQLServer:Deprecated Features'       &lt;br /&gt;AND cntr_value &amp;gt; 0;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Another way of using this performance counter is by using PerfMon tool (Start &amp;gt; Run &amp;gt; Perfmon) on your machine. For example lets add the sp_dboption counter from the SQLServer:Deprecated Features performance object as shown below.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img alt="image" border="0" height="406" src="http://lh4.ggpht.com/-dtuezTpMcWE/ToVmUc5VAcI/AAAAAAAACKk/Higl12qMDzI/image%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="590" /&gt;&lt;br /&gt;
&lt;br /&gt;
To test the counter, fire the following statement in SSMS which uses the deprecated feature sp_dboption&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;USE master     &lt;br /&gt;EXEC sp_dboption 'Umbra', 'read only', 'FALSE'      &lt;br /&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;img alt="image" border="0" height="239" src="http://lh6.ggpht.com/-8bP3AihDDhQ/ToVmVXAzVPI/AAAAAAAACKo/et0BD0AyjfA/image%25255B12%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="590" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
and note the deviation and counter value when the query is fired. Microsoft recommends to use ALTER DATABASE instead of sp_dboption.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;ALTER DATABASE      &lt;br /&gt;SET READ_ONLY      &lt;br /&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
It’s always best to log and save your results and the SQL Profiler gives you options to do that.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note:&lt;/strong&gt; If you are upgrading your SQL Server 2000 installation to SQL Server 2008, then remember to specifically search for the features that were deprecated in SQL Server 2005, since those features may no more exist in SQL Server 2008. Google/Bing is your friend here and use them to search for removed features. Another way is to look at the Backwards Compatibility section in the SQL server 2005 Books Online Installation guide and remove the deprecated features listed in that section.    &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;What to do once Deprecated Code is detected?&lt;/strong&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Microsoft has &lt;a href="http://technet.microsoft.com/en-us/library/bb510662.aspx" rel="nofollow" target="_blank" title="SQL Server deprecated"&gt;created a page&lt;/a&gt; that describes all the deprecated features and has listed alternative techniques to future-proof your code. Although in the beginning of this article, I have shown only a partial screenshot of the deprecated features used in a live production database, we actually encountered over 50 deprecated features that we were using. I have attempted to list some important ones randomly and hope that this list will be useful to you too and would save you time if you were to deal with deprecated code in your database.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Here’s a list of our observations:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The COMPUTE or COMPUTE BY syntax is deprecated since it created a new summarized result set which was difficult for apps to consume. Instead now use GROUP BY with ROLLUP which occurs once per compilation.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
While using locks in SQL Server 2008, although sp_lock and sys.syslockinfo are still available, their use has been deprecated and instead SQL Server favors the Sys.dm_tran_locks. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
While performing locks use the rowversion data type instead of the timestamp data type&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The text, ntext, and image data types are being deprecated (since SQL 2005) in favor of varchar(max), nvarchar(max) and varbinary(max) data types.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Instead of sp_change_users_login, use ALTER USER WITH LOGIN&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Compatibility levels 60, 65, and 70 are deprecated and support is provided only for compatibility levels 80 and higher&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
DBCC DBREINDEX has been deprecated and replaced with the ALTER INDEX REBUILD command and DBCC INDEXDEFRAG has been replaced with ALTER INDEX REORGANIZE&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The sp_depends system stored procedure and the sys.sql_dependencies catalog view have been deprecated. Use sys.dm_sql_referencing_entities and sys.dm_sql_referenced_entities instead&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Use of WRITETEXT or UPDATETEXT is deprecated when modifying text, ntext, or image data types.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
In the CREATE FULLTEXT CATALOG arguments, rootpath has been deprecated.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Full-Text Search stored procedures are deprecated. Use CREATE FULLTEXT CATALOG, ALTER FULLTEXT CATALOG, and DROP FULLTEXT CATALOG instead&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
If you are using XML, XDR (XML Data Reduced) schemas is deprecated&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
SET SHOWPLAN_ALL and SET SHOWPLAN_TEXT options are deprecated and replaced with the SET SHOWPLAN_XML option&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
SET STATISTICS PROFILE option has been deprecated and replaced with the SET STATISTICS XML option&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The old syntax of RAISERROR syntax specifying the error number and message number is deprecated (RAISEERROR 50005 ‘Exception Occurred’). Instead use the new syntax RAISEERROR(50005, 10, 1) which allows you to specify the messageid, severity and state)&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
In earlier editions owner.tablename was supported. In SQL 2008, this is deprecated. If an object belongs to a schema, then it must be referenced with its associated schema name so the syntax changes to schemaname.tablename.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The sp_attach_db stored procedure has been deprecated and you should use CREATE DATABASE with FOR ATTACH clause&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
sp_addlogin is replaced with CREATE LOGIN. sp_defaultdb, sp_defaultlanguage, sp_password are all replaced with ALTER LOGIN&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
sp_droplogin and sp_revokelogin is replaced with DROP LOGIN&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
sp_adduser, sp_dropuser, sp_grantdbaccess, and sp_revokedbaccess are replaced with CREATE USER, DROP USER and ALTER USER&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
sp_addrole and sp_droprole are replaced with CREATE ROLE and DROP ROLE&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The ALL clause has been deprecated.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Log shipping is deprecated and replaced with Database Mirroring&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The SQL Server Surface Area Configuration tool is deprecated and replaced with Policy-Based Management.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Remote servers will soon be deprecated and you should no longer use sp_addserver, sp_addremotelogin, sp_remoteoption and sp_helpremotelogin. Use Linked server instead.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
DUMP and LOAD have been replaced with BACKUP and RESTORE&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
sp_helpdevice is replaced by sys.backupdevices catalog view&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
The FASTFIRSTROW table hint is deprecated&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
String literals as column aliases – Microsoft discourages the use of string as a column alias in a SELECT statement as it occurs per compilation. So instead of 'RetValue'= @ret_val, you should use @ret_val as ‘RetValue’ &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align="justify"&gt;
These were just some of the deprecated features we encountered and shared with you. For a detailed list, &lt;a href="http://technet.microsoft.com/en-us/library/bb510662.aspx" rel="nofollow" target="_blank"&gt;check this documentation&lt;/a&gt;.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Here’s a mantra - Rewrite any pre-written code that uses deprecated features, and avoid using any deprecated features in new development! It will make your life as a database guy, easier.&lt;/div&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-7361706873791322738?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/lhzn02Jj4lY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/lhzn02Jj4lY/sql-server-removing-deprecated-code-and.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-aOSgsr3WRIQ/ToVmSUJ6yRI/AAAAAAAACKc/7nyxtBTYED4/s72-c/image4.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/09/sql-server-removing-deprecated-code-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2336964837468162694</guid><pubDate>Mon, 26 Sep 2011 11:18:00 +0000</pubDate><atom:updated>2011-09-26T04:18:52.363-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server: Handling Divide By Zero Error</title><description>&lt;div align="justify"&gt;
Often SQL developers encounter "Divide by zero" error. This is because in an arithmetic   &lt;br /&gt;
expression, when the divisor is zero (0), the expression throws an error. If you want to simply return NULL instead of an error, you can use one of the following methods&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strong&gt;Method 1 : Use CASE expression&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @var1 int ,@var2 int     &lt;br /&gt;select @var1=763, @var2=0&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select @var1/case when @var2 =0 then null else @var2 end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="divide_zero3" border="0" height="72" src="http://lh3.ggpht.com/-nefgZmikBeU/ToBfFwQHp_I/AAAAAAAAAPw/7TAllJxVtpA/divide_zero3%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="divide_zero3" width="200" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 2 : Use NULLIF function&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @var1 int ,@var2 int     &lt;br /&gt;select @var1=763, @var2=0&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select @var1/nullif(@var2,0)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The NULLIF function compares the first parameter (var2) with second parameter (0). If they are same, it returns a NULL&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-2336964837468162694?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/WHwAxHOV5nw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/WHwAxHOV5nw/sql-server-divide-by-zero-error.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-nefgZmikBeU/ToBfFwQHp_I/AAAAAAAAAPw/7TAllJxVtpA/s72-c/divide_zero3%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/09/sql-server-divide-by-zero-error.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5169848254281442735</guid><pubDate>Thu, 22 Sep 2011 11:13:00 +0000</pubDate><atom:updated>2011-09-22T04:13:23.846-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server: Find Database File Path</title><description>&lt;div align="justify"&gt;
Suppose you want to find out the path where the SQL Server database files(mdf and ldf) are located, using a query. You can use system procedure sp_helpdb and system view sysfiles to obtain this information. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
The following methods shows the path of the database files for master database&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strong&gt;Method 1 : Use sp_helpdb&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;EXEC sp_helpdb master&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="db_file" border="0" height="82" src="http://lh4.ggpht.com/-nXWyZnXplo0/TnsW9WbjC-I/AAAAAAAAAPo/jkD71M2tIfI/db_file%25255B9%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="db_file" width="580" /&gt;&lt;br /&gt;
&lt;strong&gt;Method 2 : Use sys.sysfiles view&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select filename from master.sys.sysfiles&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;&lt;img alt="sql-db-filepath" border="0" height="75" src="http://lh6.ggpht.com/-T084ALUaPb0/TnsW-InyfTI/AAAAAAAAAPs/pau-1Dtg41Q/sql-db-filepath%25255B10%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-db-filepath" width="560" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-5169848254281442735?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/Yr_3jq6mwog" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/Yr_3jq6mwog/sql-server-find-database-file-path.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-nXWyZnXplo0/TnsW9WbjC-I/AAAAAAAAAPo/jkD71M2tIfI/s72-c/db_file%25255B9%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/09/sql-server-find-database-file-path.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1889217308673685756</guid><pubDate>Fri, 16 Sep 2011 23:34:00 +0000</pubDate><atom:updated>2011-09-16T16:34:31.496-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server: Get Object Last Modified Date</title><description>&lt;div align="justify"&gt;
Suppose you want to track when was the last time a particular SQL Server object (table, view, procedure etc) was modified. To do so, you can make use of the system views - sys.objects and sys.all_objects.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
Consider this example&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;create table test(id int, names varchar(100))     &lt;br /&gt;GO      &lt;br /&gt;select name, create_date, modify_date       &lt;br /&gt;from sys.objects       &lt;br /&gt;where name='test'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The above gives you the create_date and modify_date (in this case both will be the same).&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-modify-date" border="0" height="57" src="http://lh3.ggpht.com/-yJbgj-cy55Y/TnPchuKxkEI/AAAAAAAAAPg/7GcSYii_QIM/sql-modify-date%25255B10%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-modify-date" width="372" /&gt;&lt;br /&gt;
&lt;br /&gt;
Now alter the table to change the width of the column names&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;alter table test      &lt;br /&gt;alter column names varchar(150)      &lt;br /&gt;GO&lt;/span&gt;    &lt;br /&gt;
&lt;br /&gt;
Now execute the same select statement again:&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="SQL Server Modified date" border="0" height="63" src="http://lh4.ggpht.com/-QkZAruofHOU/TnPbASzvqvI/AAAAAAAAAPc/5kgNuuWyvoY/image%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Server Modified date" width="352" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Executing the above statement gives you the create_date and modify_date (in this case modify_date will be greater than the create_date). Whenever there is a column change in the table, the modify_date column gets updated.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="sql-modify-date-change" border="0" height="65" src="http://lh5.ggpht.com/-zsbGQAykFf8/TnPcihrHQLI/AAAAAAAAAPk/YKzzMOQ91C0/sql-modify-date-change%25255B9%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-modify-date-change" width="382" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note:&lt;/strong&gt; You can use sys.all_objects in place of sys.objects too.&lt;/div&gt;
&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-1889217308673685756?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/X4MZ3mAEJ3g" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/X4MZ3mAEJ3g/sql-server-object-last-modified-date.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-yJbgj-cy55Y/TnPchuKxkEI/AAAAAAAAAPg/7GcSYii_QIM/s72-c/sql-modify-date%25255B10%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/09/sql-server-object-last-modified-date.html</feedburner:origLink></item></channel></rss>

