请看黄色部分
IF CUR_MONTH_BILL_ID > 0 --区县指标 THEN TVALUE_SQL := 'SELECT TO_CHAR(' || CUR_ROW.MAIN_FIELD || ') FROM ' || CUR_ROW.END_TABLE || '_' || CUR_MONTH || ' WHERE TO_CHAR(DISTRICT_ID)=''' || CUR_ROW.E_DISTRICT_ID || ''''; ELSE --客户经理指标 TVALUE_SQL := 'SELECT TO_CHAR(' || CUR_ROW.MAIN_FIELD || ') FROM ' || CUR_ROW.END_TABLE || '_' || CUR_MONTH || ' WHERE MANAGER_NO=''' || CUR_ROW.MANAGER_NO || ''''; END IF; BEGIN EXECUTE IMMEDIATE TVALUE_SQL INTO KPI_VALUE; --执行sql,得到完成值kpi_value EXCEPTION WHEN OTHERS THEN RESULT_STR := RESULT_STR || '<br/> sql执行错误:' || TVALUE_SQL || ' INTO KPI_VALUE'; END;
完整文章参考:Oracle 在存储过程或函数中执行字符串sql