日期:2014-05-17  浏览次数:21538 次

Oracle动态拼接SQL语句???
表名不确定,需要动态拼接。

表名格式为”car_state_123“,后面的123需要动态获取。

获取语句如下:
select car_model_id from car_info where no='XXXX'

最后需要的sql语句为:

select * from car_state_123,请问该语句如何写???

------解决方案--------------------
SQL code
FOR RPTT IN (select car_model_id from car_info where no='XXXX') LOOP

 select * car_state_'||RPTT.CORP_HEAD||';

end loop;

------解决方案--------------------
SQL code

--在存储过程里面完成呗
--例:
create or replace procedure yyp_cwdh(table_name varchar)
is
  str_sql varchar2(400):='';
begin
 str_sql:='select * from '||table_name||' where 1=1';
 execute immediate str_sql;
end yyp_cwdh;

------解决方案--------------------
动态语句,就是在存储过程中用到的啊,难道你想一个SQL语句就出来????
------解决方案--------------------
探讨
引用:

SQL code
begin
FOR RPTT IN (select car_model_id id from car_info where no='XXXX') LOOP

select * from car_state_'||RPTT.id||';

end loop;
end

报错了。

------解决方案--------------------
declare
str_l_sql varchar2(1000);
begin
for c in (select car_model_id from car_info where no='XXXX') loop

str_l_sql := 'select * from car_state_'||c.car_model_id;
execute immediate str_l_sql into ......
end loop;
end;
------解决方案--------------------
declare 
tabName varchar(1000);
id number;
tabSql varchar(1000);
begin

select car_model_id into id from car_info where car_lic_plate='XXXXX';
--id:=select car_model_id from car_info where car_lic_plate='XXXXX';
--tabSql:=tabName||id
tabSql := 'select * from :a';
EXECUTE IMMEDIATE tabSql USING 'car_state_'||to_char(id); 
commit;
end;
------解决方案--------------------
大概这样就行咯。。具体我就不写了。。希望你能看懂,我很懒,所以就不写全咯。

SQL code
 CURSOR TASKLIST  IS
    Select JOB_NUM,
           JOB_TYPE,
           JOB_SERVICE_NAME,
           JOB_NAME,
           JOB_STATUS,
           ERR_CODE,
           ERR_MESSAGE,
           DATELASTMAINT,
           JOB_VALIDATE
      From RP_TASKLIST
     WHERE JOB_VALIDATE = 1
       AND JOB_TYPE = 'GL'
       And JOB_STATUS < IN_JOB_STATUS
     ORDER BY JOB_NUM;
For R_TASKLIST IN TASKLIST LOOP
    JOB_SERVICE_NAME_ := R_TASKLIST.JOB_SERVICE_NAME;
    JOB_NUM_          := R_TASKLIST.JOB_NUM;

    execute immediate 'BEGIN ' || R_TASKLIST.Job_Service_Name ||
                      '(:1,:2,:3);END;' --执行子任务
      using in in_yyyymmdd, out out_ErrorNbr, out out_ErrorMsg;