Monday, March 26, 2012

How do I join tables from different databases (under same server though)

Hey, I have two databases (db1 and db2) under the same server. How do I combine tables from both of them?

I searched the forum and tried

SELECT

t1.col1

,t2.col2

FROM

db1.dbo.table1 t1

INNER JOIN

db2.dbo.table2 t2

ON t1.somecol = t2.somecol

I get Error Message: Invalid object name 'db1.dbo.table1
Error source: .Net SqlClient Data Provider

I read somewhere that the connectionstring could cause the problem since I'm trying to access two databases. I'm using SQL Server Express

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db1.mdf;Integrated Security=True;User Instance=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT t1.col1, t2.col2 FROM db1.dbo.table1 t1 INNER JOIN db2.dbo.table2 t2 ON t1.somecol = t2.somecol"></asp:SqlDataSource>


Anyway, how can I solve this?ahhh comon, someone must know a solution|||

I guess that may because the 2 databases can not 'recognize' each other. Have you ever tried register the 2 databases into SQL Express by Detach/Attach? You can firstly detach the databases from your project, and then attach them in Management Studio:

http://msdn2.microsoft.com/en-us/library/ms190209.aspx

|||thanks for the idea, i'll look into it and see what I can find out.sql

No comments:

Post a Comment