首先声明:非同一服务器,切数据库不能做任何更改!
代码:  DataSe := TADOQuery.Create(self);
  DataSe.ConnectionString:='Provider=OraOLEDB.Oracle.1;Password=pass;Persist Security Info=True;User ID=db;Data Source=oracle1';
  DataSe.SQL.Clear;
  DataSe.SQL.Add('select count(t.id) as sj from table1 t where t.id in (select c.id from table2 c where c.typeid = ''9'') and t.time between to_date('''+DateTimeToStr(DateTimePicker1.DateTime)+''',''yyyy-mm-dd hh24:mi:ss'') and to_date('''+DateTimeToStr(DateTimePicker2.DateTime)+''',''yyyy-mm-dd hh24:mi:ss'') and t.memo like ''%深圳%''');
  DataSe.Open;
  Edit1.Text := DataSe.FieldByName('sj').AsString;
  DataSe.Free;上边代码在同一个数据库中不同表,可以正常统计数据.
问题:
  现在在另一个数据库中统计数据,同样需要 t.id in (select c.id from table2 c where c.typeid = ''9'')条件
  另一个数据库配制驱动DataSe.ConnectionString:='Provider=OraOLEDB.Oracle.1;Password=word;Persist Security Info=True;User ID=db2;Data Source=oracle2';
  怎么样才能像上边那样查询出统计数据呀??

解决方案 »

  1.   

    跨库查询统计可以在一条sql中实现,需要把另一个库的连接信息写到sql中,比如驱动,数据源,用户密码等,具体的格式是[这里就是连接信息].表名,搜一下看看具体的写法吧
      

  2.   

    在oracle中,有database links,可以在一个数据库A中,建立到另外数据库B的链接,那么在A中可以用类似于
    select f1, f2 from table1@B这样的方式来访问,跟同一数据库一样操作
      

  3.   

    跨数据库可以直接在sql语句中写 sql server有opendatasource函数 oracle不知道有没有
      

  4.   

    TO: bdmh 的回复:
    我还真的需要这个的,可是就是查不到[这里就是连接信息].表名,也不知道搜索关键字是什么?  麻烦知道的话告诉我一下TO: sonicer(极品瘦子) :我上边说了,不能对数据库有任何更改..... 不能去操作数据库的呀.TO:vivai2010(vivai2010) :opendatasource函数我也写了,不起左右,不知道在DELPHI+ORACLE中到底怎么写......
      

  5.   

    是有这个,可以搜一下这个,或去Oracle版问一下
      

  6.   

    有没有直接用ADOQuery 实现?   
      

  7.   

    建立一个DBLink-- Create database link 
    create database link DBLINK名称
      connect to 用户名 identified by 密码
      using '(description =
      (address_list =
      (address = (protocol=tcp)(host=192.168.1.33)(port=1521))
      )
      (connect_data =
      (service_name=ora9i)
      )
    )';这样访问时就可以
    select * from c@dblink名称如果嫌这样写麻烦,还可以针对"c@dblink名称"建立一个同义词或视图
      

  8.   

    有两个服务器A,B现在要在B数据库上访问A数据库上的表
    就在B上建立DBLINK。
      

  9.   

    谁知道用 openrowset或openquery或opendatasource  的写法呀?  查了半天都不知道在我这里到底要怎么写~  麻烦帮忙看下,我要怎么写?
    我这样写不行.
    DataSe.SQL.Add('select count(t.id) as sj from table1 t where t.id in (select w.id from openrowset(''OraOLEDB.Oracle.1'',''oracle1'';''db1'';''pass'',oracle1.db1.table1) w where w.card_type_id = ''4'') ');
    要怎么写呀?
      

  10.   

    错了,是
    DataSe.SQL.Add('select count(t.id2) as sj from table2 t where t.id2 in (select w.id1 from openrowset(''OraOLEDB.Oracle.1'',''oracle1'';''db1'';''pass'',oracle1.db1.table1) w where w.type = ''9'') ');
      

  11.   

    opendatasource 是sqlserver数据库中的方法。
      

  12.   

    我用那DBLINK 要设置数据库了,  不用DBLINK 还有什么方法呀??
      

  13.   

    oracle跨数据库访问不修改数据库直接用SQL应该不能实现,除了db_link,其余两种方法参见:
    http://www.ej38.com/showinfo/Oracle-155399.html