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;

Answers


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

Need Your Help

Show and hide table row on hover, initially hidden, not hidden if input

javascript jquery html-table jquery-hover

I have an HTML table with a row that has an input. When I hover over the row, I want to expand the row and show the input and then collapse if not hovered. However, I want the row to start of colla...

Continuous Fractions

java math recursion

My understanding of continuous fractions was that it will always give a representation of a decimal in fraction form. I thought that continuous fraction would always return value less than or equal...