Recently I was working with one of my friends who handles production server databases as a senior DBA. He told that recently one of the Jr. DBA was fired from his company because he has run a drop table command on a production server. He was scared and from that time onwards, every DBA has been asked to run select @@servername commands before executing any query on any server. Similar to this situation, many times a DBA wants a set of command, which they use very frequently, should be available as soon as SQL Server Management Studio is opened. Let us learn about Customization of New Query in SQL Server Management Studio.
Both of the above situation can be handled by the trick explained in this blog.
Whenever we launch SQL Server Management Studio, it loads “SQLFile.sql” from the operating system and shows the text in the New Query Window. The location of this file depends on location of SSMS.exe and version of SQL Server. My machine has SQL Server 2012 and SQL Server 2014.
Here is the location of SSMS.exe on my 64 bit machine for SQL 2012
E:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio
If you are running 32 bit operating system then it would be
<Drive>:\Program Files\Microsoft SQL Server\110\Tools\Binn\ManagementStudio
Here is the mapping of SQL versions and internal version numbers (which is 110 from SQL 2012 in the above example).
|Microsoft SQL Server 2014||120|
|Microsoft SQL Server 2012||110|
|Microsoft SQL Server 2008 R2||100|
|Microsoft SQL Server 2008||100|
Once you find SSMS.exe, then we can navigate to SqlWorkbenchProjectItems\sql folder as shown below.
The file highlighted “SQLFile.sql” is the file which is loaded as a new query window.
On my machine, I have modified the file and written below
/* select @@version go select @@servername go Select * from sys.dm_exec_requests where blocking_session_id <> 0 */
After modifying, whenever you open new windows, it would be as below.
As we can see that this is EXACTLY same text which we have written in the SQLFile.sql file.
Be Aware: This file is used by all user profiles on the machine. If you delete this file by mistake, you would get below error whenever a new query window is attempted.
————————— Microsoft SQL Server Management Studio ————————— Cannot find template file for the new query (‘E:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\ManagementStudio\SqlWorkbenchProjectItems\SQL\SQLFile.sql’). ————————— OK —————————
To fix this, you can create an empty file with the same path and name in the location as per error message. The path of the error message would vary based on installation on your machine. Hope you found this tip on SSMS useful and do let me know if you will be using the same.
Reference: Pinal Dave (https://blog.sqlauthority.com)