Database Stuck in "Restoring" in SSMS After Running From an Outside Process I am running a RESTORE DATABASE procedure with RECOVERY and REPLACE from an outside process (Windows Batch using a Python script) and when I do so, it makes my database stuck in "Restoring..." mode every time.  But if I run the procedure manually, it works just fine.  Any thoughts on what might cause this?  I have come up blank in my research [I wasn't sure whether to put this here or the Python community, so I started here].

For example:

Python executes this:

exec DB_RESTORE 'MyDatabase','MyDatabase_BACKUP'


Which runs this:

USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE DB_RESTORE
    @FROMDB VARCHAR(MAX) = '',
    @TODB VARCHAR(MAX) = ''
AS
BEGIN
    DECLARE @SQL VARCHAR(MAX)
    DECLARE @FILENAME VARCHAR(MAX)
    DECLARE @PATH VARCHAR(MAX)

    SET @PATH = 'C:\temp\'
    SET @FILENAME = @PATH + @FROMDB + '_' + 'backup.bak'
    SET @SQL = ''

    SET @SQL ='RESTORE DATABASE ' + @TODB
    SET @SQL = @SQL + ' FROM DISK = ''' + @FILENAME + ''''  
    + ' WITH RECOVERY, REPLACE, STATS=10'

    PRINT @SQL
    EXECUTE(@SQL)
END
GO
The problem is that you need to give it time so the database restores.

When you run your python code to execute the stored procedure, don't close the connection instantly, but pop a time.sleep(10) after it. This will give the database some time to prepare it to the online state (the time you need to sleep will vary based on db size).