3

Have no idea how to do this! I have two databases on the same instance of SQL server 2008

In Database1 I have a table that has a column that holds a productID.

In Database2 I have a table that holds product info, i want to grab the SKU from this table based on the productID from Database1.

Its pickling my head! Hope you can help!

Many thanks

Aaron Bertrand
  • 272,866
  • 37
  • 466
  • 490

3 Answers3

4

Use the fully qualified table name to access the table cross-database.

DatabaseName.Schema.TableName
Michael Fredrickson
  • 36,839
  • 5
  • 92
  • 109
2

join them as if they were on the same DB, just add the DB name in front of the query. Of course, you may have to deal with permissions to access one DB from another

Select D1.productID, D2.productINfo
from Database1.SchemaName.Table D1 join Database2.SchemaName.Table D2 on D1.key=D2.key
Diego
  • 34,802
  • 21
  • 91
  • 134
  • 1
    This should be the correct answer and provides an example, however would be more useful to include standard schema (as per answer below); usually it's Database1.[dbo].Table1, unless schema is not [dbo], of course. – Dmitriy Khaykin Feb 24 '12 at 17:59
0

maybe this:

TSQL: Create a view that accesses multiple databases

and

http://msdn.microsoft.com/en-us/library/ms187956.aspx

Community
  • 1
  • 1
Andrew
  • 7,619
  • 13
  • 63
  • 117
  • [As mentioned in one of your previous answers](http://stackoverflow.com/questions/9434972/google-maps-api-java-script/9435013#9435013), [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Michael Petrotta Feb 24 '12 at 18:02