Saranya J

Saranya J

  • NA
  • 15
  • 0

SQL: Creating Dynamic database connection

Jul 24 2010 1:53 PM

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)