How to restore a database then upgrade it in a single SQL script?
You could use dynamic SQL

    Set @SQL = 
   'USE MyDatabase -- The database exists by this point

    -- Apply patches ...'

    EXEC SP_ExecuteSql @SQL


You may need to do a wait to allow the database to restore before executing SQL on it.

WAITFOR DELAY '00:00:02';