A query in SQL Server Management studio could fail with Error: "An error occurred while executing batch. Error message is: The file exists

Below is one of the scenarios, I recently witnessed where a query was failing when executed from SSMS (SQL 2008) on windows client machine. There is no local SQL installation on the machine and had registered a remote SQL Server instance. When tried to run any query, we encountered below mentioned error message :
"An error occurred while executing batch. Error message is: The file exists."
In case you come across such situation, you might get the same error message even if you are clicking on any database and trying to expand its property.
You may think at this point that there could be some corruption with some dll files and that reinstalling the Shared components will resolve the issue. However, if you try to uninstall the shared component using :

· Add/Remove Programs or

· %program files%\Microsoft SQL server\100\setup bootstrap\release\x64\setuparp.exe ( for 32 bit, it will be under x32) to uninstall the components, the un-installation may fail with the below mentioned error message on the GUI window :

Message: One or more command-line switches were invalid.
If we drill down and look for the detail message we will see the below message :

2010-05-05 15:33:43 Slp: Installing WMI Provider
2010-05-05 15:33:43 Slp: Sco: Attempting to create base registry key HKEY_LOCAL_MACHINE, machine
2010-05-05 15:33:43 Slp: Sco: Attempting to open registry subkey SOFTWARE\Microsoft\Microsoft SQL Server\100
2010-05-05 15:33:43 Slp: Sco: Attempting to get registry value SharedCode
2010-05-05 15:33:43 Slp: SharedDirPath is C:\Program Files\Microsoft SQL Server\100\Shared\
2010-05-05 15:33:43 Slp: Attempting to install MOF/MFL file for WMI Provider C:\Program Files\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof
2010-05-05 15:33:43 Slp: Sco: Attempting to install MOF file
2010-05-05 15:33:43 Slp: Running: C:\Windows\system32\WBEM\mofcomp.exe "C:\Program Files\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof"
2010-05-05 15:33:43 Slp: Microsoft (R) MOF Compiler Version 6.1.7600.16385
2010-05-05 15:33:43 Slp: Copyright (c) Microsoft Corp. 1997-2006. All rights reserved.
2010-05-05 15:33:43 Slp: Parsing MOF file: C:\Program Files\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof
2010-05-05 15:33:47 Slp: C:\Program Files\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof (1): error SYNTAX 0X8004402f: Error creating temporary file
2010-05-05 15:33:47 Slp:
2010-05-05 15:33:47 Slp: Compiler returned error 0x8004402f
2010-05-05 15:33:47 Slp: Sco: Compile operation for mof file C:\Program Files\Microsoft SQL Server\100\Shared\sqlmgmproviderxpsp2up.mof failed. Exit code 2
2010-05-05 15:33:47 Slp: Configuration action failed for feature CommonFiles during timing ConfigNonRC and scenario ConfigNonRC.
2010-05-05 15:33:47 Slp: One or more command-line switches were invalid.
2010-05-05 15:33:47 Slp: Configuration action failed for feature CommonFiles during timing ConfigNonRC and scenario ConfigNonRC.
2010-05-05 15:33:47 Slp: Microsoft.SqlServer.Configuration.Sco.ScoException: One or more command-line switches were invalid.
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.Sco.WmiProvider.InstallMof()
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.Slp.SlpConfigurationPrivate.Install_ConfigWMIProvider(Dictionary`2 actionData)
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.Slp.SlpConfigurationPrivate.Install_PostMSI(Dictionary`2 actionData, PublicConfigurationBase spcb)
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.Slp.SlpConfigurationPrivate.InstallImpl(ConfigActionTiming timing, Dictionary`2 actionData, PublicConfigurationBase spcb)
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.Slp.SlpConfigurationPrivate.Repair(ConfigActionTiming timing, Dictionary`2 actionData, PublicConfigurationBase spcb)
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.SqlConfigBase.PrivateConfigurationBase.Execute(ConfigActionScenario scenario, ConfigActionTiming timing, Dictionary`2 actionData, PublicConfigurationBase spcbCurrent)
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.SqlConfigBase.SlpConfigAction.ExecuteAction(String actionId)
2010-05-05 15:33:47 Slp: at Microsoft.SqlServer.Configuration.SqlConfigBase.SlpConfigAction.Execute(String actionId, TextWriter errorStream)
2010-05-05 15:33:47 Slp: Exception: Microsoft.SqlServer.Configuration.Sco.ScoException.
2010-05-05 15:33:47 Slp: Source: Microsoft.SqlServer.Configuration.Sco.
2010-05-05 15:33:47 Slp: Message: One or more command-line switches were invalid..
2010-05-05 15:33:47 Slp: Watson Bucket 1
 Original Parameter Values

The error message is self explanatory and says that we are failing to create temporary files. This can occur if WMI is unable to access the TEMP and/or TMP location, either because of permissions or because the path is invalid.
We checked the environment variable and changed it to "C:\Windows\Temp" .
Ran the un-installation and this time it went on fine.
Reinstalled the client component and we were now able to get the output of the results. However later when we were trying to register a new server it was failing with the below mentioned error message.

“Unhandled exception has occurred in a component in your application. If you click Continue, the application will ignore this error and attempt to continue.” Key not valid for use in specified state.

 

Followed the below 2 step :

1. Close SSMS;

2. Go to the folder %appdata%\Microsoft\Microsoft SQL Server\100\Tools\ and check if there is a Shell folder, if so, rename it to Shell_Old;

 Each time SSMS starts it creates this folder and will use old files. Hence by renaming the folder we are re-creating all the required components.

Regards,

Gaurav Srivastava

SE, SQL support

Reviewed by:

Nickson Dicson & Saket Suman

TL, SQL support