1
Answer

SQL: Creating Dynamic database connection

Saranya J

Saranya J

14y
1.9k
1

How to connect with tables in different database without hardcoding the database name in the query. I would like to have the query to get the database name in a variable or parameter and use anywhere within the script.

For example:
select * from titles cross join northwind.dbo.orders

Here northwind dbo name is hardcoded. But this northwind may change to any database name at run time. I would like to keep that in a variable or parameter and get that according to the requirements anywhere in the script. May be in a single transaction or from the cursor... Any help much appreciated... Thanks in advance.

Answers (1)