<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://sqlblog.com/utility/FeedStylesheets/atom.xsl" media="screen"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><title type="html">Michael Zilberstein</title><subtitle type="html" /><id>http://sqlblog.com/blogs/michael_zilberstein/atom.aspx</id><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/default.aspx" /><link rel="self" type="application/atom+xml" href="http://sqlblog.com/blogs/michael_zilberstein/atom.aspx" /><generator uri="http://communityserver.org" version="2.1.61129.1">Community Server</generator><updated>2008-03-01T16:43:00Z</updated><entry><title>NHibernate and triggers</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/11/19/10000.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/11/19/10000.aspx</id><published>2008-11-19T09:24:05Z</published><updated>2008-11-19T09:24:05Z</updated><content type="html">Recently had an interesting experience with NHibernate (I was against working with it from the beginning for many reasons, but unfortunately I wasn't the one to decide). I added trigger to a table that performs several update / insert operation to other tables. Suddenly updating table via NHibernate returned error: NHibernate.AdoNet.TooManyRowsAffectedException: Unexpected row count: 6; expected: 1 So NHibernate doesn't support triggers that perform DML operations? Not quite. The solution is: add...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/11/19/10000.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=10000" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author></entry><entry><title>MERGE statement syntax modified</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/09/16/8920.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/09/16/8920.aspx</id><published>2008-09-16T18:03:47Z</published><updated>2008-09-16T18:03:47Z</updated><content type="html">This is probably known to all but I've just found out - MERGE command syntax had been modified since CTP6. In the beginning of 2008 I had built a system on CTP6 and this week, while preparing for production deployment, found out that procedures are failing with syntax error. It appears that in CTP6 MERGE contained the following blocks: WHEN MATCHED WHEN [TARGET] NOT MATCHED WHEN SOURCE NOT MATCHED In RTM, if you write WHEN TARGET NOT MATCHED, it fails compilation. New syntax is: WHEN MATCHED WHEN...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/09/16/8920.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=8920" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="SQL2008" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/SQL2008/default.aspx" /><category term="T-SQL" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/T-SQL/default.aspx" /></entry><entry><title>Query performance troubleshooting in SQL Server 2008: query_hash and query_plan_hash</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/09/09/Query-performance-troubleshooting-in-SQL-Server-2008_3A00_-query_5F00_hash-and-query_5F00_plan_5F00_hash.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/09/09/Query-performance-troubleshooting-in-SQL-Server-2008_3A00_-query_5F00_hash-and-query_5F00_plan_5F00_hash.aspx</id><published>2008-09-09T14:52:00Z</published><updated>2008-09-09T14:52:00Z</updated><content type="html">Recently I have noticed 2 new columns added to sys . dm_exec_query_stats and sys . dm_exec_requests DMVs in SQL Server 2008: query_hash and query_plan_hash. Those columns can greatly enhance performance monitoring process. In SQL 2005 main query I'm using for query performance monitoring is: SELECT TOP 10 qs.execution_count , ( qs.total_physical_reads + qs.total_logical_reads + qs.total_logical_writes ) AS [Total IO] , ( qs.total_physical_reads + qs.total_logical_reads + qs.total_logical_writes )...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/09/09/Query-performance-troubleshooting-in-SQL-Server-2008_3A00_-query_5F00_hash-and-query_5F00_plan_5F00_hash.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=8813" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="SQL2005" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/SQL2005/default.aspx" /><category term="Performance" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Performance/default.aspx" /><category term="SQL2008" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/SQL2008/default.aspx" /></entry><entry><title>Name resolution in SQL Server</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/08/28/Name-resolution-in-SQL-Server.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/08/28/Name-resolution-in-SQL-Server.aspx</id><published>2008-08-28T10:57:00Z</published><updated>2008-08-28T10:57:00Z</updated><content type="html">This post is inspired by recent post by Mladen Prajdić. Mladen had found out (as many did before him - hanging around in blogs and communities, I see questions about this issue from time to time) that name resolution works for temporary tables differs from the one for permanent tables. While for permanent table more than one CREATE TABLE statement with the same table name can be issued inside the same batch or procedure, for temporary table it will fail compilation saying: "There is already an object...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/08/28/Name-resolution-in-SQL-Server.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=8628" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="Misc" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Misc/default.aspx" /></entry><entry><title>Partitioning by GUID</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/06/11/7237.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/06/11/7237.aspx</id><published>2008-06-11T00:40:14Z</published><updated>2008-06-11T00:40:14Z</updated><content type="html">Recently I had to use GUID (Global Unique Identifier) as primary key in order to ensure cross-server uniqueness. In many cases surrogate key (some sort of concatenation between server id and intra-server identity) would give a better performance but in my case due to application requirements surrogate key wasn't an option. Next step is partitioning by GUID column. Say, you want 3 partitions. Ho do you build partition function? Sure, you can perform a calculation and find the exact 2 values that will...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/06/11/7237.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=7237" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="Misc" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Misc/default.aspx" /><category term="Scripts" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Scripts/default.aspx" /></entry><entry><title>Scalability features I would like to have in SQL Server</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/04/23/6361.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/04/23/6361.aspx</id><published>2008-04-23T16:03:26Z</published><updated>2008-04-23T16:03:26Z</updated><content type="html">1. Active-Active cluster. Like RAC in Oracle. Maximum that commodity server can provide by now is 4 quad-core CPU machine. If application requires more, we can split the database (which is not always possible; and even when it is - such a solution often looks very artificial) or buy high-end server with more CPUs and much higher price line. The only thing that keeps SQL Server alive in the market for applications with high scalability requirements is totally unreasonable (IMHO) price that Oracle...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/04/23/6361.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=6361" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="Misc" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Misc/default.aspx" /></entry><entry><title>How do we know that Microsoft conducts performance tests?</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/24/5757.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/24/5757.aspx</id><published>2008-03-24T07:31:00Z</published><updated>2008-03-24T07:31:00Z</updated><content type="html">Funny comment in undocumented system stored procedure - and undocumented syntax of DBCC SHOW_STATISTICS. ...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/24/5757.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5757" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author></entry><entry><title>How to find real database creation date</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/18/5629.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/18/5629.aspx</id><published>2008-03-18T05:56:30Z</published><updated>2008-03-18T05:56:30Z</updated><content type="html">Restoring database from backup or moving it to another server resets all the creation_date fields (in sys.databases , msdb..backupset etc'). So is there any way to find out the original creation date? Looks like there is one. It is stored in the boot page of the database (page 9) and you'll have to use DBCC PAGE in order to find it. DBCC TRACEON(3604) GO DBCC PAGE (YourDBName, 1, 9, 3) GO In the bottom of the page you'll find dbi_crdate field - it is the original creation date. Boot page fields are...(&lt;a href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/18/5629.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5629" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="Internals" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Internals/default.aspx" /></entry><entry><title>Have your database ever been backed up?</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx</id><published>2008-03-01T14:15:00Z</published><updated>2008-03-01T14:15:00Z</updated><content type="html">
&lt;p&gt;Recently I've been asked by one of my clients to write a simple script that would find out whether particular database had ever been backed up (full backup). The reason for a requirement was semi-automatic application which would enable end users to manage backup activity including manually backup transaction log - not via Management Studio but via much more friendly UI designed for non-DBAs. As you know, for database running in Full Recovery Mode, transaction log can't be backed up before full backup is done at least for the first time.
OK, I thought. Let's just check whether full backup exists for the database:
&lt;font color="#0033cc"&gt;&lt;/font&gt;&lt;/p&gt;

&lt;p&gt;&lt;font color="#0033cc"&gt;IF&lt;/font&gt; &lt;font color="#808080"&gt;EXISTS&lt;/font&gt;( &lt;font color="#0033cc"&gt;SELECT&lt;/font&gt; 1 &lt;font color="#0033cc"&gt;FROM&lt;/font&gt; msdb..backupset &lt;font color="#0033cc"&gt;WHERE&lt;/font&gt; database_name = &lt;font color="#ff0000"&gt;'MyDB'&lt;/font&gt; &lt;font color="#808080"&gt;AND&lt;/font&gt; [type] = &lt;font color="#ff0000"&gt;'D'&lt;/font&gt; )
...&lt;br&gt;&lt;br&gt;&amp;nbsp;Here comes the first catch. Imagine following scenario:
&lt;/p&gt;

&lt;p&gt;1. Create database MyDB.
&lt;/p&gt;

&lt;p&gt;2. Create full backup of 'MyDB'.
&lt;/p&gt;

&lt;p&gt;3. Drop database 'MyDB' (without deleting backup history).&amp;nbsp;
&lt;/p&gt;

&lt;p&gt;4. Create new database with the name 'MyDB' or restore from backup.
&lt;/p&gt;

&lt;p&gt;What do we have now? Query will return true while actually backup we have belongs to another non-existing database. Try to backup transaction log of the new DB - you'll receive error.
&lt;/p&gt;

&lt;p&gt;First try failed - let's give another one. Both &lt;font color="green"&gt;sys.databases&lt;/font&gt; and msdb..backupset contain creation date of the database. In &lt;font color="green"&gt;sys.databases&lt;/font&gt; column name is create_date, in msdb..backupset it is database_creation_date. So let's just compare database_creation_date from the latest full backup row with database's create_date. If they're equal - we have full backup. Otherwise backup belongs to the previous incarnation of our database while full backup of currently existing db had never been taken. Sure, it should also work for the normal scenario - without deleted and restored databases on the way. So first let's check whether our theory works after second step of the scenario described above. But what happened? How comes, our database creation times are different?&amp;nbsp;&lt;/p&gt;

&lt;p&gt;
Here comes second catch. For some strange reason,&amp;nbsp;database_creation_date column in&amp;nbsp;msdb..backupset doesn't contain milliseconds! Minutes, seconds - everything is there but for milliseconds its always 000. While&amp;nbsp;create_date in&amp;nbsp;&lt;font color="green"&gt;sys.databases&lt;/font&gt; contains full date including milliseconds. Don't know who and for what reason cut milliseconds from the backup history table, but that's the way it works now.
&lt;/p&gt;

&lt;p&gt;To make long story short - it was the last barrier. After removing milliseconds part from database creation date in&amp;nbsp;&lt;font color="green"&gt;sys.databases&lt;/font&gt;, everything began to work. Here is the full script that will tell you whether some particular database had ever been backed up (full backup).&lt;/p&gt;

&lt;p&gt;
&lt;font color="blue"&gt;DECLARE&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;SYSNAME&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;SET&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#ff0000"&gt;'YourDatabase'&lt;/font&gt;
&lt;font color="blue"&gt;&lt;/font&gt;
&lt;/p&gt;

&lt;font color="blue"&gt;DECLARE&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@LatestBackupDate&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;DATETIME&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@BackedUpDBCreationDate&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;DATETIME&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;DECLARE&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBCreationDateNoMilliseconds&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;DATETIME&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="green"&gt;--&amp;nbsp;In&amp;nbsp;msdb..backupset&amp;nbsp;table,&amp;nbsp;database_creation_date&amp;nbsp;column&amp;nbsp;is&amp;nbsp;in&amp;nbsp;datatime&amp;nbsp;format&lt;br&gt;--&amp;nbsp;but&amp;nbsp;without&amp;nbsp;milliseconds,&amp;nbsp;so&amp;nbsp;we&amp;nbsp;remove&amp;nbsp;milliseconds&amp;nbsp;from&amp;nbsp;database&amp;nbsp;creation&amp;nbsp;date.&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;SELECT&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBCreationDateNoMilliseconds&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;= &lt;/font&gt;&lt;font color="blue"&gt;&lt;font color="black"&gt;	&lt;/font&gt;&lt;/font&gt;&lt;font color="blue"&gt;&lt;br&gt;&lt;font color="magenta"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; DATEADD&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="blue"&gt;millisecond&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;-&lt;/font&gt;&lt;font color="magenta"&gt;DATEPART&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="blue"&gt;millisecond&lt;/font&gt;&lt;font color="gray"&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;create_date&lt;/font&gt;&lt;font color="gray"&gt;),&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;create_date&lt;/font&gt;&lt;font color="gray"&gt;)&lt;/font&gt;&lt;/font&gt;&lt;br&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="green"&gt;sys.databases&lt;/font&gt;&lt;font color="#434343"&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;[name]&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&lt;/font&gt; &lt;br&gt;
&lt;font color="#434343"&gt;&lt;/font&gt;&lt;font color="blue"&gt;IF&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBCreationDateNoMilliseconds&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;IS&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINT&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'Database&amp;nbsp;'&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'&amp;nbsp;doesn'&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;CHAR&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;39&lt;/font&gt;&lt;font color="gray"&gt;)&amp;nbsp;+&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'t&amp;nbsp;exist&amp;nbsp;on&amp;nbsp;the&amp;nbsp;server'&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;RETURN&lt;br&gt;&amp;nbsp;&amp;nbsp; END&lt;br&gt;&lt;br&gt;SELECT&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@LatestBackupDate&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;backup_finish_date&lt;/font&gt;&lt;font color="gray"&gt;,&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="#434343"&gt;@BackedUpDBCreationDate&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;database_creation_date&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;msdb..backupset&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;WHERE&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;backup_set_id&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;SELECT&lt;/font&gt;&amp;nbsp;&lt;font color="magenta"&gt;MAX&lt;/font&gt;&lt;font color="gray"&gt;(&lt;/font&gt;&lt;font color="black"&gt;backup_set_id&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;FROM&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;msdb..backupset&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;WHERE&amp;nbsp;&lt;/font&gt;&lt;font color="black"&gt;database_name&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;AND&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="black"&gt;[type]&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'D'&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;)&lt;br&gt;&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;IF&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@LatestBackupDate&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;IS&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;NULL&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;BEGIN&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; PRINT&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'Full&amp;nbsp;backup&amp;nbsp;had&amp;nbsp;never&amp;nbsp;been&amp;nbsp;taken&amp;nbsp;for&amp;nbsp;database&amp;nbsp;'&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;RETURN&lt;br&gt;&amp;nbsp;&amp;nbsp; END&lt;br&gt;&lt;br&gt;IF&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@BackedUpDBCreationDate&amp;nbsp;&lt;/font&gt;&lt;font color="blue"&gt;=&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBCreationDateNoMilliseconds&lt;br&gt;&amp;nbsp;&amp;nbsp; &lt;/font&gt;&lt;font color="blue"&gt;PRINT&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'Full&amp;nbsp;backup&amp;nbsp;exists&amp;nbsp;for&amp;nbsp;database&amp;nbsp;'&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'; &lt;/font&gt;&lt;font color="red"&gt;Transaction&amp;nbsp;log&amp;nbsp;backups&amp;nbsp;can&amp;nbsp;be&amp;nbsp;taken'&lt;br&gt;&lt;/font&gt;&lt;font color="blue"&gt;ELSE&lt;br&gt;&amp;nbsp;&amp;nbsp; PRINT&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'Full&amp;nbsp;backup&amp;nbsp;exists&amp;nbsp;for&amp;nbsp;the&amp;nbsp;current&amp;nbsp;version&amp;nbsp;of&amp;nbsp;'&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="#434343"&gt;@DBName&amp;nbsp;&lt;/font&gt;&lt;font color="gray"&gt;+&amp;nbsp;&lt;/font&gt;&lt;font color="red"&gt;'&amp;nbsp;database&amp;nbsp; &lt;/font&gt;&lt;font color="red"&gt;had&amp;nbsp;never&amp;nbsp;been&amp;nbsp;taken. Existing&amp;nbsp;backup&amp;nbsp;refers&amp;nbsp;to&amp;nbsp;non-existing&amp;nbsp;database &lt;/font&gt;&lt;font color="red"&gt;with&amp;nbsp;the&amp;nbsp;same&amp;nbsp;name'&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;/font&gt;
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=Have your database ever been backed up?&amp;amp;body=Seen on SQLblog.com: %0A%0A%09Have your database ever been backed up?%0A%0Ahttp://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx" target="_blank" title = "Email Have your database ever been backed up?"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx&amp;amp;title=Have+your+database+ever+been+backed+up%3f" target="_blank" title = "Submit Have your database ever been backed up? to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx&amp;amp;phase=2" target="_blank" title = "Submit Have your database ever been backed up? to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx&amp;amp;title=Have+your+database+ever+been+backed+up%3f" target="_blank" title = "Submit Have your database ever been backed up? to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx&amp;amp;title=Have+your+database+ever+been+backed+up%3f" target="_blank" title = "Submit Have your database ever been backed up? to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/Have-your-database-ever-been-backed-up_3F00_.aspx&amp;amp;title=Have+your+database+ever+been+backed+up%3f&amp;amp;;top=1" target="_blank" title = "Add Have your database ever been backed up? to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5378" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="Misc" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Misc/default.aspx" /><category term="Scripts" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Scripts/default.aspx" /></entry><entry><title>1001st way to find SQL Server service start time</title><link rel="alternate" type="text/html" href="http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx" /><id>http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx</id><published>2008-03-01T13:43:00Z</published><updated>2008-03-01T13:43:00Z</updated><content type="html">There're a lot of ways to discover when SQL Server service had been last restarted. That's not a pure curiosity issue since almost all the DMVs and performance counters reset at service restart. So in order to calculate averages (IOs per minute, Waits per minute) etc, one needs to know the starting point. There're a lot of ways to find it. One is to take a look in Error Log (if log hadn't been deleted by numerious DBCC ERRORLOG commands). Another way is to look at &lt;span&gt;sys.dm_exec_requests&lt;/span&gt; for start_time or in master..sysprocesses for login_time of system processes like LAZY WRITER. Recently I've found one more way: look in &lt;span&gt;sys.databases&lt;/span&gt; for create_date of tempdb database. Since tempdb is re-created from scratch every time SQL Server restarts, it works. BOL reference about create_date column also says:&lt;br&gt;&lt;span style="font-style:italic;"&gt;-- Date the database was created or renamed. For &lt;span style="font-weight:bold;"&gt;tempdb&lt;/span&gt;, this value changes every time the server restarts.&lt;/span&gt;&lt;br&gt;&lt;br&gt;P.S. Results you receive trying different ways can vary - half minute here or there - because service restart doesn't happen in a second. But when you need to measure time since startup, 30 seconds usually won't be serious factor.
&lt;BR&gt;&lt;div class = "shareblock"&gt;&lt;span class = "shareblockTitle"&gt;Share this post:&lt;/span&gt;&lt;span class = "shareblockLink"&gt; &lt;a href = "mailto:?subject=1001st way to find SQL Server service start time&amp;amp;body=Seen on SQLblog.com: %0A%0A%091001st way to find SQL Server service start time%0A%0Ahttp://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx" target="_blank" title = "Email 1001st way to find SQL Server service start time"&gt;email it!&lt;/a&gt; |  &lt;a href = "http://del.icio.us/post?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx&amp;amp;title=1001st+way+to+find+SQL+Server+service+start+time" target="_blank" title = "Submit 1001st way to find SQL Server service start time to del.icio.us"&gt;bookmark it!&lt;/a&gt; |  &lt;a href = "http://www.digg.com/submit?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx&amp;amp;phase=2" target="_blank" title = "Submit 1001st way to find SQL Server service start time to digg.com"&gt;digg it!&lt;/a&gt; |  &lt;a href = "http://reddit.com/submit?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx&amp;amp;title=1001st+way+to+find+SQL+Server+service+start+time" target="_blank" title = "Submit 1001st way to find SQL Server service start time to reddit.com"&gt;reddit!&lt;/a&gt; |  &lt;a href = "http://www.dotnetkicks.com/submit/?url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx&amp;amp;title=1001st+way+to+find+SQL+Server+service+start+time" target="_blank" title = "Submit 1001st way to find SQL Server service start time to DotNetKicks"&gt;kick it!&lt;/a&gt; |  &lt;a href = "https://favorites.live.com/quickadd.aspx?marklet=1&amp;amp;mkt=en-us&amp;amp;url=http://sqlblog.com/blogs/michael_zilberstein/archive/2008/03/01/1001st-way-to-find-sql-server-service-start-time.aspx&amp;amp;title=1001st+way+to+find+SQL+Server+service+start+time&amp;amp;;top=1" target="_blank" title = "Add 1001st way to find SQL Server service start time to Live Bookmarks"&gt;live it!&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;&lt;img src="http://info.sqlblog.com/a.aspx?ZoneID=0&amp;BannerID=12&amp;AdvertiserID=1&amp;CampaignID=12&amp;Task=Get&amp;Mode=TEXT&amp;SiteID=1&amp;RandomNumber=463323" width="1" height="1" border="0"&gt;&lt;img src="http://sqlblog.com/aggbug.aspx?PostID=5377" width="1" height="1"&gt;</content><author><name>mz1313</name><uri>http://sqlblog.com/members/mz1313.aspx</uri></author><category term="Misc" scheme="http://sqlblog.com/blogs/michael_zilberstein/archive/tags/Misc/default.aspx" /></entry></feed>