现有一个表tb_compare_script,其中一个字段存储的是一条sql查询语句,这条sql语句从rim_account_2012,bims_account_2012两个表中查询数据。比如:1. select count(1) from rim_account_2012 where city='崇左'
          2.  SELECT b.*  FROM bims_account_2012 b WHERE NOT EXISTS (SELECT 1 FROM rim_account_2012 a WHERE upper(a.account)=upper(b.account) and a.city=b.city) and b.city='崇左'这样,有的是统计,有的是查询详细的数据。 现在要写一个存储过程,从tb_compare_script表中查询出sql语句,执行,从结果中取得数据,插入tb_compare 和tb_output 数据表中。tb_compare 表存储的是统计数据,而tb_oupt表则要存储统计和详细数据。现在我不懂的地方是:从tb_compare_script中查询出来的sql语句怎么在存储过程中执行,再取得数据,进而把数据插入tb_compare表 和tb_output表中。还有要怎么分辨哪条数据该存在哪个表中呢,不知道我描述清楚了没有?希望大神指教,刚毕业,压力很大啊!

解决方案 »

  1.   


    --帮你写了个例子,只是不知道你的字符串的SELECT b.* FROM bims_account_2012 b WHERE NOT EXISTS (SELECT 1 FROM rim_account_2012 a WHERE upper(a.account)=upper(b.account) and a.city=b.city) and b.city='崇左',如何进行更新或新增到你的表,其实都差不多的,不懂再问吧create table t(
    id int,
    val varchar2(2000)
    );create table t1(
    id int,
    empcou int,
    empdescr varchar2(2000)
    );insert into t values(1,'select count(1) from emp');insert into t1 values(1,0,'');declare 
    strsql varchar2(2000);
    cou varchar2(20);
    cursor c is select * from t;
    begin
    for i in c
        loop 
    if instr(i.val,'count')>1 then
    execute immediate i.val
    into cou;
    update t1 set empcou=cou where t1.id=1;
    else
    dbms_output.put_line('对于字符串你打算如何统计呢?');
    end if;
        end loop;
    end;
    /select * from t1;
      

  2.   


    谢谢!tb_compare_script 表中的sql语句是生成之后导进去的
      

  3.   


     update t1 set empcou=cou where t1.id=1;   这里好像不能这么写啊,因为查询出来的数据可能有多个字段
      

  4.   

    declare 
        cou varchar(5000);
        cursor c is select * from tb_compare_script t;
    begin
        for i in c
        loop 
            if instr(i.script,'count')>1 then
                execute immediate i.script
                    into cou;
                insert into tb_output(result) values(cou);
            end if;
        end loop; 
    end;
    报错 ORA-00932:数据类型不一致:应为-,但却获得-
               ORA-06152:在 line 8
      

  5.   

    嗯,是啊,但是execute immediate i.script查询出来的数据有好几列,我要把他合并成一个字段,然后再插入tb_compare表中,这该怎么做啊?
      

  6.   

    用大SQL 
    execute immediate 'insert into '|| 目标表 || i.script||' ';
    要把他合并成一个字段: 这个规则建议写在script里。
      

  7.   

    这样写报错:在此上下文中不允许表,视图或序列的引用‘tb_output’declare 
       cursor c1 is select * from tb_compare_script t;
    begin
        for i in c1
        loop 
            if instr(i.script,'count')>1 then
                 execute immediate 'insert into '|| tb_output || i.script||' ';
            end if;
        end loop; 
    end;
      

  8.   


    请问下这个语句怎么拼接declare 
       cursor c1 is select * from tb_compare_script t;
    begin
        for i in c1
        loop 
            if instr(i.script,'count')>1 then
                 execute immediate 'CREATE TABLE tmp12345 AS' ||'|| i.script||';
            end if;
        end loop; 
    end;
    报错:缺少select关键字create table tmp12345 as sql查询语句 ,这样是可以执行的,但是这里不知道怎么拼接
      

  9.   


    --例子,你写的有问题,有问题可以给我留言,一般不是私信我会回的
    set serveroutput on;
    declare 
    strsql varchar2(50) := 'select * from dept';
    result number:=0;
    begin
    execute immediate 'create table tmp12345 as '||strsql;
    execute immediate 'select count(1) from tmp12345'
    into result;
    dbms_output.put_line(result);
    end;
    /