I'm using Sql Server Management Studio 2012, and I'm wondering if there is a way to have a query show me at what time it was run and/or completed at. Basically just some kind of timestamp so that I know if I actually ran the query recently or not.
Thanks
-
3You can just add one more select at the end of your query: SELECT GetDate()A-K– A-K2014年03月21日 18:13:54 +00:00Commented Mar 21, 2014 at 18:13
-
1Add on to SSMS has query history even after you close the query window or SSMS, free for SQL Server 2008 and 2005, a bit of cost for 2012: ssmstoolspack.com/Downloaduser507– user5072014年03月21日 23:41:24 +00:00Commented Mar 21, 2014 at 23:41
3 Answers 3
Hitting F4 also enables properties view, which shows the start time, finish time, and run time.
SSMS 2012 Properties View
Also, if you Include Client Statistics (under Query menu), you can get a history of when you ran the query, along with various statistics such as run time and number of rows.
SSMS 2012 Client Statistics Pane
Update, 2019年10月14日: SSMS 18 now includes the completion time in the Messages pane of the results tab.
-
1The Client Statistics window doesn't include a date with the time, which is unfortunate, but the times in the Properties window are hidden gems I didn't know about. +1Jon Seigel– Jon Seigel2014年06月26日 17:50:29 +00:00Commented Jun 26, 2014 at 17:50
You can do something like below :
declare @timer datetime
select @timer = getdate()
select @timer as QueryStartTime
--- here goes actual query
select * from sysdatabases
--- end of query
select getdate() as QueryEndTime
select datediff(ms, @timer, getdate()) as TimeInMS
enter image description here
Even though SSMS will show you how much time it took to execute the query, I have included the datediff
.
enter image description here
A possible solution could be to run your query in a stored procedure (which usually has the added benefit of speeding up subsequent runs of your query, since the execution plan will be stored) and at the end of your SP, write something into a log table you created for that purpose. (Classic candidates could be name of the SP and getdate(), or even a result summary, if applicable)