Help: SQL Server

Sharing my knowlege about SQL Server Troubleshooting Skills

  • Blog Stats

    • 2,153,605 hits
  • Select GETDATE()

    December 2025
    M T W T F S S
    1234567
    891011121314
    15161718192021
    22232425262728
    293031  

Archive for the ‘Error’ Category

Tips and Tricks : Restore Using TRY…CATCH might fail with error

Posted by blakhani on July 22, 2014


Few days back one of my friend called me with an interesting problem. She told that when she does a regular restore of a database backup file, it works but when she does using TRY/CATCH block, it fails. The very first thing which I asked her– What is the error message? She sent me email with below message. I have changed some names to save the innocents.

<EMAIL>

Command which fails.

BEGIN TRY  
    RESTORE DATABASE [TestBackup] 
    FROM  DISK = N'C:\Temp\TestBackup.bak' 
    WITH  FILE = 1,  
    MOVE N'TestBackup' TO N'C:\Temp\TestBackup.mdf',  
    MOVE N'TestBackup_log' TO N'C:\Temp\TestBackup_log.LDF',  
    NOUNLOAD,  
    STATS = 5; 
END TRY 
BEGIN CATCH  
             THROW; 
END CATCH  

Here is the output

52 percent processed.

100 percent processed.

Processed 240 pages for database ‘TestBackup’, file ‘TestBackup’ on file 1.

Processed 2 pages for database ‘TestBackup’, file ‘TestBackup_log’ on file 1.

Converting database ‘TestBackup’ from version 611 to the current version 706.

Database ‘TestBackup’ running the upgrade step from version 611 to version 621.

Database ‘TestBackup’ running the upgrade step from version 621 to version 622.

Database ‘TestBackup’ running the upgrade step from version 622 to version 625.

Database ‘TestBackup’ running the upgrade step from version 625 to version 626.

Database ‘TestBackup’ running the upgrade step from version 626 to version 627.

Database ‘TestBackup’ running the upgrade step from version 627 to version 628.

Database ‘TestBackup’ running the upgrade step from version 628 to version 629.

Msg 102, Level 15, State 1, Procedure usp_logError, Line 11

Incorrect syntax near ‘200001’.

Msg 3013, Level 16, State 1, Line 11

RESTORE DATABASE is terminating abnormally

Command which works

RESTORE DATABASE [TestBackup] 
FROM  DISK = N'C:\Temp\TestBackup.bak' 
WITH  FILE = 1,  
MOVE N'TestBackup' TO N'C:\Temp\TestBackup.mdf',  
MOVE N'TestBackup_log' TO N'C:\Temp\TestBackup_log.LDF',  
NOUNLOAD,  
STATS = 5;

There is NO difference in restore command. Only difference is that it is wrapped in TRY block. If any exception raised, it would be caught and thrown.

<EMAIL>

Since I see version upgrade messages I asked if backup was taken on lower version and restore is performed on higher version? She informed that this backup is taken on SQL Server 2005 and she wants to restore on SQL Server 2012. I also asked her to share ERRORLOG and here is the snippet

2014-07-17 17:43:41.070 spid56 Error: 928, Severity: 20, State: 1.

2014-07-17 17:43:41.070 spid56 During upgrade, database raised exception 102, severity 25, state 1, address 00007FFCE2DC3E04. Use the exception number to determine the cause.


Exception 102 is “Incorrect Syntax” error. If we read the message in restore output carefully, we can see that there is a procedure usp_logError which seems to be a problem. I asked her to look at the code of usp_logError and search for 200001 and she found below:

raiserror 200001 'Debit and Credit Mismatch'

If we execute above piece of code in SQL 2005, it works and it’s valid whereas, it gives syntax error in SQL 2012. I looked into documentation and found below at http://msdn.microsoft.com/en-us/library/ms144262.aspx (Discontinued Database Engine Functionality in SQL Server 2014)

Transact-SQL

RAISERROR in the format RAISERROR integer ‘string’ is discontinued.

Rewrite the statement using the current RAISERROR(…) syntax.

 

I asked her to change the syntax and modify the stored procedure before taking backup. Once done, the restore with and without TRY CATCH worked like a charm.

You can easily reproduce this error by creating below database and procedure in SQL 2005.

Create database TestBackup 
go 
use TestBackup 
go 
Create Procedure FailureProc 
as 
begin 
RAISERROR 20001 'ERROR RAISED' 
end 

Restoring this backup in SQL 2012 using TRY CATCH would fail.

Tip: Whenever I see any “Error: 928, Severity: 20, State: 1.” I always look for the exception code. In this case it was 102. To convert code to text, we can use sys.messages catalog view (check image below) . While searching further, I was able to find below variations.

2014-07-17 17:43:41.070 spid56 Error: 928, Severity: 20, State: 1.

2014-07-17 17:43:41.070 spid56 During upgrade, database raised exception 156, severity 25, state 1, address 00007FFCE2DC3E04. Use the exception number to determine the cause.


image

Error 156 – Incorrect syntax near the keyword ‘%.*ls’.

There might be many exceptions raised even without try…catch, you need to read the message and look for explanation of exception code.

You might ask, What should be done before upgrade to avoid such situations? You should run upgrade advisor. http://msdn.microsoft.com/en-us/library/ee210467.aspx (SQL Server 2014 Upgrade Advisor). There are versions available for SQL 2012 and earlier as well. They mostly catch all errors which might come after upgrade. Interestingly, RAISERROR problem is not caught by upgrade advisor.

http://connect.microsoft.com/SQLServer/feedback/details/708167/somewhat-incompatible-t-sql-on-sql-server-2012-rc0

http://connect.microsoft.com/SQLServer/feedback/details/694484/denali-ctp3-upgrade-advisor-misses-deprecated-raiserror-syntax

You can vote up if you think it would help.

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Error, Upgrade | Tagged: , , , , , | 2 Comments »

    Solution: The file "MDF" is compressed but does not reside in a read-only database or filegroup. The file must be decompressed.

    Posted by blakhani on May 22, 2014


    Recently I was trying to create a database on my SQL Instance using below command

    CREATE DATABASE [MyDatabase]
    ON   
    ( NAME = N'MyDatabase', FILENAME = N'E:\BlogContent\DatabaseFiles\MyDatabase.mdf')
    LOG ON 
    ( NAME = N'MyDatabase_log', FILENAME = N'E:\BlogContent\DatabaseFiles\MyDatabase_log.ldf')
    GO

    But encountered below error

    Msg 5118, Level 16, State 1, Line 1
    The file "E:\BlogContent\DatabaseFiles\MyDatabase.mdf" is compressed but does not reside in a read-only database or filegroup. The file must be decompressed.
    Msg 1802, Level 16, State 4, Line 1
    CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

    Error giving hint about some kind of compression. So if we look at the folder which is specified E:\BlogContent\DatabaseFiles it was in blue color.

    image

    Blue color actually indicates that folder is compressed. Right Click on folder, go to “Properties” and click on “Advanced…”

    image

    The checkbox “Compress contents to save disk space” is checked which means compression is enabled. Once we uncheck the box, we should be able to create database.

    Here is official stand about compression http://support.microsoft.com/kb/231347/en-us (SQL Server databases are not supported on compressed volumes). There is a great blog by Ken Henderson on this topic detailing the ramifications and I would encourage you to read it http://blogs.msdn.com/khen1234/archive/2005/04/25/411852.aspx 

    If we try to restore a database and keep files on compressed folder, same error is raised (in SQL 2014 Management Studio)

    TITLE: Microsoft SQL Server Management Studio
    ------------------------------
    Restore of database 'MyDatabase' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)
    ------------------------------
    ADDITIONAL INFORMATION:
    System.Data.SqlClient.SqlError: The file "E:\BlogContent\DatabaseFiles\MyDatabase.mdf" is compressed but does not reside in a read-only database or filegroup. 
    The file must be decompressed. (Microsoft.SqlServer.SmoExtended)
    For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=12.0.2000.8+((SQL14_RTM).140220-1752)&LinkId=20476 ------------------------------ BUTTONS: OK ------------------------------

    Hope this helps!

     

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Error | Tagged: , , | 33 Comments »

    Troubleshooting : Msg 3201 – Cannot open backup device .. Operating system error 3(The system cannot find the path specified.)

    Posted by blakhani on March 13, 2014


    Data recovery is a time consuming and expensive process. Taking regular backups can save a lot of time as well as make sure that the data can be restored in the case of disaster. There could be many situation where backup of the database taken in SQL Server might fail with 3201 error. The text of error id as below (you can use sys.messages catalog view to get it)

    Cannot open backup device ‘%ls’. Operating system error %ls.

    Same message is applicable for restore as well, it’s all about unable to open device. If we notice %ls is placeholder which would be inserted when message is raised. Some sample errors are as below.

    Message # 1

    Msg 3201, Level 16, State 1, Line 1
    Cannot open backup device ‘E:\Backup\SQLServerHelp.bak’. Operating system error 3(The system cannot find the path specified.).

    Cause: Backup folder not created on E Drive.

    Message # 2

    Msg 3201, Level 16, State 1, Line 1
    Cannot open backup device ‘C:\SQLServerHelp.bak’. Operating system error 5(Access is denied.).

    Cause: SQL Server Service account is not having permission on root of C Drive.

    Message # 3

    Msg 3201, Level 16, State 2, Line 1
    Cannot open backup device ‘\\NetworkShare\Backup\SQLServerHelp.BAK’. Operating system error 53(error not found).

    This one is interesting because it doesn’t tell the exact message for OS error 53. Sometime this could happen with any other OS error as well. Refer my earlier post here where I showed how to get text for an operating system error number. 53 = The network path was not found.

    Cause: Network Path: \\NetworkShare\Backup was incorrect.

    OK. Let me stop here.. I didn’t plan to discuss all the possible errors but this blog is to present one interesting scenario which helped me uncovering an operating system concept.

    Let’s create a database using below script.

    Create Database [SQL Server Help ]

    Once database is created I have created maintenance plan to take full backup of the database.

    image

    When I executed above maintenance plan, it failed with error:

    image

    TITLE: Execute Maintenance Plan

    ——————————

    Execution failed. See the maintenance plan and SQL Server Agent job history logs for details.

    ——————————

    ADDITIONAL INFORMATION:

    Job ‘SQLServerHelp.Subplan_1’ failed. (SqlManagerUI)

    ——————————

    BUTTONS:

    OK

    ——————————

    If we look at the folder, the folder got created but backup failed. Looked into ERRORLOG and found below

    2014-03-13 06:20:31.66 spid65      Error: 18204, Severity: 16, State: 1.

    2014-03-13 06:20:31.66 spid65      BackupDiskFile::CreateMedia: Backup device ‘E:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\SQL Server Help \SQL Server Help _backup_2014_03_13_062031_6415729.bak’ failed to create. Operating system error 3(The system cannot find the path specified.).

    2014-03-13 06:20:31.66 Backup      Error: 3041, Severity: 16, State: 1.

    2014-03-13 06:20:31.66 Backup      BACKUP failed to complete the command BACKUP DATABASE SQL Server Help . Check the backup application log for detailed messages.

    when I looked at folder structure, I found that there is a folder “SQL Server Help” got created under “E:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup” but still backup was failing. I ran profiler to see the command being fired.

    BACKUP DATABASE [SQL Server Help ] 
    TO  DISK = N'E:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\SQL Server Help \SQL Server Help _backup_2014_03_13_062920_6305771.bak' 
    WITH NOFORMAT, NOINIT,  NAME = N'SQL Server Help _backup_2014_03_13_062920_6305771', 
    SKIP, REWIND, NOUNLOAD,  STATS = 10
    

     

    When I executed the command, it failed with below error.

    Msg 3201, Level 16, State 1, Line 1

    Cannot open backup device ‘E:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\SQL Server Help \SQL Server Help _backup_2014_03_13_062920_6305771.bak’. Operating system error 3(The system cannot find the path specified.).

    Msg 3013, Level 16, State 1, Line 1

    BACKUP DATABASE is terminating abnormally.


    Again, something wrong with the path where I am taking backup. Do you see any problem in above message. Look closer.. there is a space at the end in the folder name where backup is attempted. “SQL Server Help “. If you try to create a folder with the whitespace at the end, operating system would trim the space at the end. This is documented here

    The root cause of my problem was name of the database where I have added an space at the end intentionally. Go back and have a look at create database statement which I have used. I also participated in this discussion where same problem was posted long back.

    Hope you have learned something new!

  • Cheers,
  • Balmukund Lakhani
  • Twitter @blakhani
  • Author: SQL Server 2012 AlwaysOnPaperback, Kindle
  • Posted in Error, SQL Server | Tagged: , , , , , , , , , | 8 Comments »