Danny
Danny

Reputation: 510

SSMS -- how to load saved queries? My saved .sql file is not finding the referenced table

I'm very new to SSMS. This seems simple but I'm finding nothing.

I can open a table and right-click to query the top 1000 rows, then successfully update that query and get the information I want. When I later open the saved query in SSMS and try to run it, it gives me an error: Invalid object name 'TABLE_NAME'.

However, if I right-click, select top 1000 rows, and then copy-paste my saved query into that text field, then when I run it that way, it works.

How can I skip the copy-paste step and just open files that run the query successfully the first time?

Upvotes: 0

Views: 1373

Answers (2)

eshirvana
eshirvana

Reputation: 24633

In SSMS , each query window has connection with its own session.

so whenever you open a query , you have to make sure you have connected to the right SQL server instance and It's pointed to the right database

I recommend you always use below command before all your queries:

USE [databasename]

Upvotes: 2

lareb
lareb

Reputation: 92

Please select database name from the top or open new query tab by clicking on database from the left panel and paste your query there and execute.

Upvotes: 1

Related Questions