7

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

  • 3
    You can just add one more select at the end of your query: SELECT GetDate() – A-K Mar 21 '14 at 18:13
  • 1
    Add 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/Download – Shawn Melton Mar 21 '14 at 23:41
12

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.

Messages pane showing 1 row affected and Completion time

  • 1
    The 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. +1 – Jon Seigel Jun 26 '14 at 17:50
3

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

0

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.