There has been multiple times when I’ve been running part of a large query script and
someone something has distracted me.
Next thing I know I look back at my screen and my results pane shows:
(2089 row(s) affected)
Now that might be fine if you’ve ran what I just ran:
SELECT TOP (2089) * FROM sys.all_objects;
but what happens when you take another look at the code and what you thought you’ve just ran is in the middle of the following
INSERT INTO [very_important_table] SELECT [id], [random_string_column], [random_int_column] FROM [user_input] as [ui] -- if it's in not in the table, then insert it WHERE NOT EXISTS ( SELECT 1 FROM [user_input] as [exists] WHERE [exists].[id] = [ui].[id] ); SELECT TOP (2089) * FROM sys.all_objects; DELETE FROM [very_important_table]; --WHERE [important_table_Id] = 1 -- commented out WHERE CLAUSE
Suddenly you’re not sure if you really ran the SELECT statement at all.
Maybe you ran the insert statement and 2089 rows were marked to never be seen again!
Or maybe that other table only had 2089 rows in it and you’ve now deleted every one!!
Now this blog post is not going to deal with fail-safe’s for preventing those scenarios because 1) you should already know how to do that, and b) if you don’t know, then maybe back away until you research it… It’s only going to deal with a nice little way you can figure out what it was that you just ran.
In SSMS, there are 4 different ways to deal with query results.
- Results to Grid, the default (Ctrl + D),
- Results to Text, quite handy for copying text (Ctrl + T),
- Results to File, cause sometimes you just want a really big file (Ctrl + Shift + F), and
- No Results, very useful when you’re trying to performance tune something as sending results to the client is actually a performance hit!.
Now you may have noticed that for that list, I gave you the keyboard shortcuts for each option apart from the final one, and there’s a very important reason for that…
…I don’t actually know if it has a shortcut…
Honestly, I’m not sure if there is one (apart from declaring a variable as shown by Kendra Little).
What I do know is that where you go to set No Results to on is also where you go to set WHERE_WAS_I to on.
- Tools –> Options
- Query Results –> SQL Server –> Results to Grid
…check some of these other options out too, they’re pretty useful…
From here we can already see that I have that magical little checkbox “Include the query in the result set” ticked, and 3 check boxes down from that is the No Results checkbox “Discard results after execution”.
…if you want it to take affect, you’ll need to open a new Query Window…
Now seeing as I had that checkbox ticked, all I have to do is open the Messages tab in the results pane and there will be the text that you ran, your little WHERE_WAS_I.
…okay so I cheated, I ran the whole thing and only showed you part of the results pane. Sorry I’m not sorry 🙂 …
The good news is that now we know what was ran…EVERYTHING…but seeing as this is a massively contrived example…I mean who calls a table [very_important_table]…it works for what we want.
From this we can see
- 3 queries ran
- 3 results were returned.
Like I said, it’s not a fail-safe but just a little nudge to let you know where you are. Hopefully it should help you out with knowing what it was that you just ran…even if what it tells you wasn’t what you wanted to see!
P.S. this is closest that I’ve found to a shortcut for this. If you click the Query Options button below and click the checkboxes that way it will also work but only for the session window that you are in!
- Query Options –> Results