Where is SQLQuery1.sql saved? - sql-server

Whenever I open up SSMS (SQL Server 2012), I get all of the scripts that I had open in my previous session. This includes both saved queries and any unsaved queries, which are listed as SQLQuery1.sql, SQLQuery2.sql, etc.
I can't seem to find where these "unsaved" queries are being saved. I've checked Documents\SQL Server Management Studio\Backup Files\ (and every other directory in that vicinity). I've also checked the C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\ directory and subdirectories (as best I could). Finally, I did a complete search on my computer for the filename SQLQuery1.sql and it turned up nothing (not that I trust the Windows search function in file explorer).
In case it changes things at all, I also have installed the Redgate SQL Prompt add-on for SSMS.
Any other suggestions?
Thanks!

It's very simple :)
Create new query, then right click on the tab and then choose Open containing folder option.
For example, I got C:\Users\PKuderov\AppData\Local\Temp folder and my SQLQuery4.sql is named ~vs3C67.sql.
So #BenjaminSmith is right but these files have unique temporary names. Until you explicitly save them where you want. It seems they survive ssms reload and you can further filter them by .sql extension and Date Modified.

How about the TEMP folder? Try searching for %TEMP% from your start menu and searching in there.

I got my saved query in the following way.
I made another query and selected to save it.
It popped out the save as dialogue box from where I got all the things which I required.
I copied the file path which was "C:\Users\Shafiq Ur Rehman\Documents\SQL Server Management Studio"

Related

Change SSMS Default File Location (Specifically Templates)

TLDR: How do I change the default location for SQL Server Management Studios 2014 without uninstalling and reinstalling?
I recently installed SQL Server Management Studio 2014, and the default file locations aren't where I want them. I was able to change the default query location and default project location through Tools -> Options, but I cannot for the life of me figure out how to change the template location. Even if I delete the folder, every time I run SSMS, it creates a new folder where it used to be.
I'm guessing I just missed an option somewhere (possibly the XML section?), but any pointers would be appreciated.
In case it matters, I want to move the default location from "Documents/SQL Server Management Studios" to "Documents/Programming/SQL Server Management Studios"
Edit: I already changed the file path on the Import and Export settings, and the Query Result -> SQL Server tabs.
Don't know about moving the entire location for SSMS, but if you are looking to modify the core templates that keep reloading, see below.
I have always kept my go-to scripts in the Templates and to keep things clean, I've always first bundled all the shipped templates to a separate folder. You have to do this not only in your local instance first, but also the location they pull from each time you open SSMS, or it will always keep adding the default templates (and their folders) whenever you open SSMS.
If you have already opened your Template Explorer, edit one of the files in there (Right Click > Edit) to open it. On the instance that opened, right click on its tab and select Open Containing Folder.
Close SSMS and then delete all but your custom templates from the location that opened. Make a backup of your custom templates somewhere else on your PC, just to be safe.
DO NOT OPEN SSMS YET!! Doing so will rebuild the core templates in your SSMS instance.
Open the following folder path: C:\Program Files (x86)\Micorost SQL Server\120\Tools\Binn\ManagementStudio\SqlWorkbenchProjectItems\Sql
That is where the default templates reside. What I do is create a new folder called "__SQLDefault", then move everything except Sql.vsdir and SQLFile.sql to the __SQLDefault folder (Or whatever you called it). This is in case I ever want to reference any of the core templates...they are still there just in a separate, single folder. If you don't want any of the core templates to be preserved, the other option would just be to delete everything (except Sql.vsdir and SQLFile.sql) instead of putting them in a folder.
Once the files are moved or deleted, now open SSMS and your template explorer should just have the _SQLDefault folder in it (or not if you deleted everything), along with your custom templates that you didn't delete earlier. If your custom ones are gone, just add them back from your backup.
Try changing the Import and Export Settings.
What helped me was editing the registry and Value for DefaultFileOpenLocation
HKEY_USERS\S-1-5-21-1428394981-1396028598-1721912238-1000\Software\Microsoft\SQL Server Management Studio\18.0_IsoShell

SQL Server Script shortcuts

I have numerous scripts to run in SQL Server.
Currently I've made many scripts which have been saved as file and can be loaded in SSMS when needed.
I want to know if we have any shortcut(bookmark) in SQL server to script files.
So far I've found nothing on internet. (note that creating stored procedures for these ad-hoc scripts is not an option)
By pressing Cntrl+K, Cntrl+W combination you can see the Bookmark manager.
Here you can see all bookmarks in locally saved sql queries.
Like in the below screenshot I have a bookmark for getLocationBasedMemberVisitsData.Sqlfile.
On how to add a bookmark
goto Edit>Bookmarks>toggle bookmark when you are inside your file with your edit cursor at the line you want to bookmark
P.S.: You can also create a folder hierarchy much like in browsers like Chrome
Store all needed files inside a single solution. Call it Utilities. Much like VS you can browse through the solution files using Solution explorer.
Caveat: The bookmarks on loose sql files are not persisted. So on a restart(close/open) for SSMS tool these are lost.
If you do need to persist bookmarks, create bound files i.e. store the files in a solution first.
Also the bookmarks in Management Studio are consist with Visual Studio.
Addendum:
Based on asker's comment
This just bookmark a line on a script. Is these any way to give these bookmarks a name?
You can rename both the folders as well as bookmark name by double clicking on it or by right click>rename option
On another comment
What do you mean by saving as as solution. I cannot find such option in SSMS. Are you referring to VS?
SQL server management studio like VS supports creation of Solutions(project) which are essentially collection of related queries/SP with connection information and other needed stuff.
To create a new solution press cntrl+shift+N. see screen shot below
Any bookmarks on solution files are sticky. Next time you open the solutions, bookmarks are presented back to you.
The SQL solution can be opened in VS too. The bookmark information along with other user information is stored in a *.ssms_suo file much like a *.suo file of VS.
It is a good idea to store all helpful files in a utilities solution which can be open all the time so that you can easily access it.
I think a Snippet is what you are asking for:
A Transact-SQL.code snippet is a template containing the basic
structure of a Transact-SQL statement or block. You can use snippets
as a starting point when adding statements in the Database Engine
Query Editor. You can insert the pre-defined snippets supplied with
SQL Server, or create your own.
more info here
In SQL Server Management Studio you could use menu -> Tools -> External Tools.
Do you use SQL Prompt? If so, this has a Tab History feature that lets you search for query windows, regardless of whether they have been saved or not.
If not, I'd use the snipppet feature that VV5198722 has referred to, or the Template Browser (invoked from the view menu).

You do not have the necessary permissions to use msAccess 2000 db

I'm trying to open an old MS Access 2000 Database that was working on windows XP, so that i can migrate the old DB in a new application. The DB was created by the previous old WPF application and I need to find a way to open it. I installed MS Access 2000 and used a program that generated the right password for the db but there is still this message that pops up:
You do not have the necessary permissions to use C:/pathToDB.MDB object. Have your system administrator or the person who created this object establish the appropriate permissions for you.
Is there a way to solve this problem?
Many thanks.
Earlier versions before 2007 had something called as the MDW (Microsoft Access Workgroup) file with the MDB .
If you cannot find this, you can create one. To create this file, first create a new shortcut Right click New > Shortcut>. When asked type the location of the item. You will need to enter the following, replacing MDB/MDW> to where your files are located and the version of Office you are using. It may be worth opening notepad so you can get the path right and then copy and paste.
"C:\Program Files\Microsoft Office\Office14\MSACCESS.EXE" ".mdb" /wrkgrp ".mdw" /user
Replace Office14 with your version of Microsoft office that is installed. To find this, browse to “C:\Program Files\Microsoft Office\ and note the “Office” folder.
Use this new shortcut to open your file. It should hopefully sort it out.
delete the .ldb file and this should solve your problem. The .ldb file is created when a new session is created. If the program crashes, the .ldb file will not be deleted.
Hope this helps you.

Opening .sql files in SSMS 2012 as default program and with existing instance

Installation of SSMS 2012 alongside any other existing SSMS installation, such as SSMS 2008 R2 has a problem with setting the default program for opening .SQL files to the new SSMS 2012 version.
Dragging .sql file to the query window works successfully. However, when a user double clicks on the .sql file a new instance of 2008 R2 can open as the .sql associated entry. Changing with Default Open With makes no difference when doubleclicking on new files. Additionally, altering the command line script for the opening can perhaps achieve changing the .sql to opening in SSMS 2012, but it still causes repeated instances of ssms instead of using the existing instance (causing additional load and memory usage).
Adapted from this post on how to how to fix .sql file not opening with SSMS 2008, you should start by opening up the registry (regedit)
Go to HKEY_CLASSES_ROOT\.sql and change the default value to ssms.sql.11.0 (SSMS 2012 is v11 of the product). This tells your computer the application string it should look for when trying to open files of type .sql
The registry takes that name you just entered and then looks up what to actually go do in another registry setting.
Go HKEY_CLASSES_ROOT\ssms.sql.11.0\Shell\Command and change the default value to:
"C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\ssms.exe" "%1" /dde
Turn your computer off and on again for good measure.
If it still doesn't work for whatever reason. Right click on any SQL file. Go to Properties and click to change the file it opens with. You may have two SQL options available. Try one then the other.
To change to default entry I utilized an application that displayed the DDE (dynamic data exchange) values. I personally used FileTypesMan.
I then changed the command line for SSMS to:
"C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe" /dde "%1"
The reason a new instance is always opened with editing seems to be that windows needs to associate the file with a program so that it can identity if the existing program is running and use this instance. This seems to be achieved by using DDE. /dde indicates that the command line should use DDE. "%1" passes the first string (ssms.exe path) to the DDE protocol for usage as a parameter.
DDE Message: was kept at the SSMS 2008 R2's value I found: Open("%1")
DDE Application: Change the Application to ssms.11.0
Why? This was the difficult part. Where is this found? I could not find a tool for displaying the DDE application name. What I found was when I reviewed the registry editor that sqlwb.sql.9.0 is the entry to open a new .sql file with 2008 R2. This matched the registry entry for sqlwb.9.0 that SSMS 2008 R2 entry was showing before I started making my changes in FileTypesMan. I removed the .sql and found that the HKEY_CLASSES_ROOT had an entry for ssms.sql.11.0 .
= File association now set to new installation, and if SSMS.EXE is open, it should use the existing instance with no additional work.
Tip: To further speed up the initial load if you don't have it open, you can use other command line options on the load. Just go to the RUN dialog and type: ssms.exe -? for a list of startup parameters. I personally use -nosplash to eliminate the splash screen load.
Additionally, I use SSMSBoost, the single greatest tool I've had to help with using SSMS daily (basic code formatting, snippet w/cursor placement, preferred connections setup, full keyboard shortcut programming (along with the option for multiple steps in once shortcut, ie basic macros". Andre and his team are extremely responsive to new ideas in their forum, its very much developers helping improve other developer's life.
For SQL 2014 you can use the following *.reg-file (for 2012 replace the 12 with 11 and for 2016 (I guess) with 13) :
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\sql_auto_file]
#=""
"EditFlags"=hex:00,00,00,00
"FriendlyTypeName"="SQL"
[HKEY_CLASSES_ROOT\sql_auto_file\shell]
[HKEY_CLASSES_ROOT\sql_auto_file\shell\open]
[HKEY_CLASSES_ROOT\sql_auto_file\shell\open\command]
#="\"C:\\Program Files\\Microsoft SQL Server\\120\\Tools\\Binn\\ManagementStudio\\Ssms.exe\" /dde \"%1\""
[HKEY_CLASSES_ROOT\sql_auto_file\shell\open\ddeexec]
#="Open(\"%1\")"
[HKEY_CLASSES_ROOT\sql_auto_file\shell\open\ddeexec\Application]
#="ssms.12.0"
[HKEY_CLASSES_ROOT\sql_auto_file\shell\open\ddeexec\Topic]
#="system"
I had this same issue. Using "restore file associations" in ssms 2012 had no effect. I was able to resolve it by right clicking and choosing "open with" -> choose default program. Now I know you've said this didn't work, but there is a trick. Rather than selecting the pre-provided icon for ssms 2012, choose browse and go to c:\program files(x86)\Microsoft SQL server\110\tools\binn\managementStudio\ and click on ssms.exe. Click open, click ok. Once I did this, ssms 2012 was the default, and double clicking more .sql files loads into tabs in the same instance.
Another solution: https://stackoverflow.com/a/24945327/813599
I went looking for a more proper way to link to other Q/A's and couldn't find it after 15 minutes of searching... would love to be educated and will edit this out if so.
I was having a similar but slightly different issue. I wanted to open .sqlplan file in a new tab in SSMS 2014. I tried all of the above but none of them worked.
Looking at the registry, I found that ssms.sql.12.0 has "DdeExec" node, while ssms.sqlplan.12.0 does not.
enter image description here
So I export the "DdeExec" node from ssms.sql.12.0 and rename sql to sqlplan, then import it back in. After that, I was able to open .sqlplan file in a new tab.
Here is the registry file:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\ssms.sqlplan.12.0\Shell\Open\DdeExec]
#="Open(\"%1\")"
[HKEY_CLASSES_ROOT\ssms.sqlplan.12.0\Shell\Open\DdeExec\Application]
#="ssms.12.0"
[HKEY_CLASSES_ROOT\ssms.sqlplan.12.0\Shell\Open\DdeExec\Topic]
#="system"
Management Studio 18:
"C:\Program Files (x86)\Microsoft SQL Server Management Studio 18\Common7\IDE\Ssms.exe" /dde "%1"

Open Alternate Extension in 'Single Instance' of SQL Server Management Studio

I know how to add additional extensions to SQL Server Management Studio, to have them treated just like .sql files. But, when opening them, a new instance of Management Studio is created.
I also see numerous issues posted about double clicking on .SQL files and getting them to open in the existing instance of SQL server. This already works for me.
What doesn't work is the additional extensions I have added. I'm using .PRC. All of the fixes propose changing various registry settings, all which are related to the default .sql extension.
I don't see see how any of these fixes can be applied to make other extensions work or if there is anything else I can do to solve this?
EDIT:
What i've done so far...
Copied over all the same settings from [HKEY_CLASSES_ROOT.sql] to [HKEY_CLASSES_ROOT.prc].
Checked all the settings mentioned in the article on sql server central to fix this issue for .SQL extensions.
Configured SSMS file type .PRC with editing experience "SQL Query Editor".
I even tried creating a batch file to open the file for me:
"c:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\VSShell\Common7\IDE\ssms.exe" /dde %1
Even the 4th thing I tried opens a new instance, although this is the same call that is configured in the registry for sqlweb.sql.9.0.
I resolved problem of opening PRC file by double click. I simply exported this key from regedit
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.sql
Modified this file to look like this
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.PRC]
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.PRC\OpenWithList]
"a"="ssms.exe"
"MRUList"="a"
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.PRC\OpenWithProgids]
"ssms.sql.11.0"=hex(0):
"ssms.sql.12.0"=hex(0):
[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.PRC\UserChoice]
"Progid"="ssms.sql.12.0"
And applied it over registry. The original PRC key I backuped and removed from registry before I applied that new keys. Now PRC file is opened by double click on PRC file in opened instance of SSMS.
But highlighting doesn't work still for me :(
You seem to be going through a lot of hassle to deal with your source control system. Have you considered making your source control system recognize a different extension for procedures, tables, views, etc. which will prevent you having to make these registry changes on every developer's computer?
.proc.sql -> stored procedure
.view.sql -> view
.fn.sql -> function
etc. etc. Surely your source control system can handle longer extensions that contain ., and now Management Studio will behave properly with all your file types.

Resources