Search code examples
sql-serversqlcmd

SQLCMD Restore is not restoring all sprocs


I am using the following command in order to restore a database as a part of an installation of a program.

SqlCmd -S .\<SqlInstance> -h -1 -U <username> -P <password> -Q "RESTORE DATABASE <Database> FROM DISK=<FilePath>"

When I run this command, the database restores successfully, but there is a single sproc that does not exist. However, when I take the same file and restore the database through SSMS, that sproc does exist and the database restores correctly. This is the only difference between the two methods of restoring the database and I cannot manage every install manually, as this will be installed to over 300 machines.

If there is any other information I can give in order for clarification I am happy to provide it.

EDIT:

AS per the comments I found the T-SQL command being executed

USE [master]
RESTORE DATABASE [ErplyAnalysis] FROM  DISK = N'<FilePath>' WITH  FILE = 2,  NOUNLOAD,  STATS = 5

GO

Solution

  • You're restoring different backup sets in the different commands. If we solely look at the 2 statements you have:

    RESTORE DATABASE <Database> FROM DISK=<FilePath>
    

    And then in SSMS:

    RESTORE DATABASE [ErplyAnalysis] FROM  DISK = N'<FilePath>' WITH  FILE = 2,  NOUNLOAD,  STATS = 
    

    Notice in SSMS you have FILE = 2.

    By default, if FILE is omitted then it will use its default value of 1 (see RESTORE Arguments (Transact-SQL)). As a result, in sqlcmd you are restoring an older version of your database.

    You have 2 sets as you didn't reinitialise the backup when you made it. That isn't a problem, but it means you need to ensure you check which value you need to pass for FILE is you are using backup sets.