How to run queries properly after restoring a db in Sql Server 2012?
The following query runs line 1 then fails on line 2 because it cannot find the db "because it does not exist". In SSMS 2012, i can get this to work by refreshing the database manually but i need to do it via a query. is there some way to refresh or whatever is necessary to make the db appear to exist?
RESTORE DATABASE [MyDb] FROM DISK = N'C:\backup_2014_01_13_000002_3436250.bak' TRUNCATE TABLE dbo.MyTable;
You are not setting the context to [MyDb], before executing line 2. Try this:
RESTORE DATABASE [MyDb] FROM DISK = N'C:\backup_2014_01_13_000002_3436250.bak' GO USE [MyDb] GO TRUNCATE TABLE dbo.MyTable; GO