用的方法是:
用if else 拼出sql文,然後用
“open cursor for sql”來執行。
-- 大概代碼如下
create or replace....
is
var_sql varchar2(4000);
TYPE typCur IS REF CURSOR;
cur typCur;
rec aType.aRecord%type; -- aRecord是一個record
begin
var_sql := '';
if...then
var_sql :=var_sql|| 'select a, b, c'..||..||..;
elsif... then
var_sql :=var_sql||..||..||..;
end if;
if...then
var_sql := var_sql||'..';
end if;
open cur for var_sql
loop fetch cur into rec
exit when cur%notfound;
...
end loop;
close cur;
exception
when others then
close cur;
end;
/*
* 1. 由於拼的東西sql文太長,當我將拼好的var_sql塞給var_sql時,會報06502的錯,如果定義時將var_sql定義爲varchar2(32000)
* 【oracle db裏面varchar2最長爲4000bytes,而plsql在處理時,能處理的最長的是32000bytes】
*2. 1的問題完了之後,程序能執行到“fetch cur into rec”,rec在定義的時候定義的type與select檢索到的不一致,比如是b,a,c
*/
ORA-06502 PL/SQL: numeric or value error string
Cause: An arithmetic, numeric, string, conversion, or constraint error occurred. For example, this error occurs if an attempt is made to assign the value NULL to a variable declared NOT NULL, or if an attempt is made to assign an integer larger than 99 to a variable declared NUMBER(2).
Action: Change the data, how it is manipulated, or how it is declared so that values do not violate constraints.