1

I am preparing a group of sql queries for a dashboard. I want to declare the database name at the beginning so that the queries will work on the database specified on top without making any changes in the underlying code

Original query:

SELECT * FROM Check.dbo.Dates_table 

The query I want:

DECLARE @Databasename VARCHAR(200) SET @Databasename = 'Check.dbo' SELECT * FROM @Databasename.Dates_table 
1
  • 1
    Why don't you simply change the connection or issue a USE [somedb] command? Why try to pass it as a parameter? In any case, the database isn't a value so it can't be treated as a parameter. Commented Feb 22, 2016 at 11:15

1 Answer 1

2

You can use "USE" operator: https://msdn.microsoft.com/en-AU/library/ms188366.aspx

use Check SELECT * FROM dbo.Dates_table 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.