r/SQLServer • • Sep 10 '26

Question SSMS 22 error when clicking New Query

Post image

I always seem to hit this STG_E_FILEALREADYEXISTS error when clicking "New Query" right after I open SSMS after a reboot where it didn't shut down gracefully and it recovers files. The only way to fix it is to close out any tabs that are named SQLQuery#.sql.

Is there something else I can do to solve this?

4 Upvotes

13 comments sorted by

2

u/ZedGama3 Sep 11 '26

There's a tool that will monitor every system call that you can use to see what it's trying to open. I always get the two confused, but I think it's process monitor, if not then process explorer. It's a pain to filter out the noise, but it's generally good for this kind of thing.

My guess is that there's an existing file wherever SSMS stores it's recovery files and that if you clear those out the problem might go away.

That's my two cents...

4

u/ouchmythumbs Sep 10 '26

I wish they would start including error codes with these messages that we could then search for.

1

u/Leiothrix Sep 11 '26

Well there is an error code.

What would be helpful is stating which file already exists so you could go and delete it.

The more baffling thing is that I figured out logging useful things was useful as a teenager a long time ago, why can't an extremely large company figure that out?

3

u/ouchmythumbs Sep 11 '26

Well there is an error code.

Huh, well would you look at that

0

u/TrollingForFunsies Sep 11 '26

Do you have any add-ons?

2

u/Disastrous-Money2395 Sep 11 '26

I do. Redgate sql prompt and sql search.

2

u/TrollingForFunsies Sep 11 '26

Maybe both tools are trying to recover the same files as SSMS and they are interfering with the file recovery and new file creation

1

u/L0nelyR0gue 29d ago

I too gotten into this problem. Happened after an upgrade to SSMS 22.x

Found that its because the way SSMS is now saving autorecover temp files in %Temp% .

Earlier file name was a random string like 1u5epmb0..sql but this SSMS is saving it as proper "SQLQuery1.sql" and when we open a new window , it is failing. What a nasty bug from such tech giant !?
Can't understand how it even passed their testing !?

1

u/L0nelyR0gue 29d ago

after I posted this , I found that : " Ctrl +N " which is File > New > Query with Current Connection fails but Database Engine Query works well and opens new query windows.

Happy that I contributed !

1

u/navicat-gu 6d ago

That's the recovered tabs and the new tab fighting over the same temporary name. After a crash, SSMS restores SQLQuery1.sql, SQLQuery2.sql and so on, then "New Query" tries to create a file with a name that's already in use, and you get STG_E_FILEALREADYEXISTS.

What's helped:

  • Update SSMS. Microsoft's SSMS 22 release notes list a fix (22.12) for unsaved query windows reusing temporary file names across sessions, and 22.12.1 is the current build. Check under Help > Check for Updates or the Visual Studio Installer.
  • Save recovered tabs with a real name (or close them) before opening a new query. Once nothing is called SQLQuery#.sql, there's no collision.
  • Avoid two SSMS instances after a crash. They draw from the same pool of generic names.
  • AutoRecover settings: Tools > Options > Environment > AutoRecover controls this. If you'd rather keep your own work saved to a folder, you can lower or turn off AutoRecover so fewer orphaned recovery files come back.

If it still happens on 22.12.1, it's worth filing on the SSMS feedback site with the repro, because it's clearly meant to be fixed.

Disclosure: I'm RC, Director of Digital Strategy @ Navicat. Navicat 18 also connects to SQL Server if you ever want a second client, and Navicat Premium Lite is free to try. SSMS is still the right tool here, though. 

1

u/Pytzamarama Sep 11 '26

I do not know if of any relevance but I updated to the last SSMS and there were cases that I had 3 or 4 SSMS instances open and it seemed like the generic files like SQLQuery1.sql etc seem to be shared between them! I was doing edits on SQLQuery1 on SSMS1 and SSMS2 later was asking to apply the modifications in its own SQLQuery1 (somewjetre in the AppData/Temp space)

1

u/az987654 Sep 11 '26

Turn off your add ons

1

u/CanProfessional766 Sep 15 '26

Looks like SSMS is getting stuck with the recovered SQLQuery#.sql files after an improper shutdown. Since closing those tabs fixes it, I’d try clearing the old recovery/temp files and updating SSMS 22. I wouldn’t worry about the database itself this seems to be an SSMS issue