如果数据库有表aa和bb
那么select aa.*,bb.* from aa,bb where aa.id=bb.id很容易实现。
但如果
aa表在数据库A,bb表在数据库B,怎么实现查询啊。
我习惯用ado,如果连接2个数据库,要放2个adoconnection?

解决方案 »

  1.   

    如果是SQLserver,且两个库在同一个sqlserver实例中,并有两个库的用户那么
    1.
    select a.* from database1.user1.table1 as a,database2.user2.table2 as b 
    where a.id=b.id2.
    sql里有opendatasource()和openrowset ()
    如:
    SELECT a.*
    FROM table1 as a ,OPENDATASOURCE(
    'SQLOLEDB',
    'Data Source=ServerName;User ID=MyUID;Password=MyPass'
    ).Northwind.dbo.Categories as b
    where a.id=b.id详细可参照SQL的联机帮助
      

  2.   

    如下:  with adoquery1 do
      begin
          sql.Text:='select * from [数据库1].[dbo].[表1] a,'+
                  '[数据库2].[dbo].[表2] b '+
                  'where a.id=b.id';
          open;
      end;