對于一般的select操作,如果使用動(dòng)態(tài)的sql語句則需要進(jìn)行以下幾個(gè)步驟:
open cursor---> parse---> define column---> excute---> fetch rows---> close cursor;
而對于dml操作(insert,update)則需要進(jìn)行以下幾個(gè)步驟:
open cursor---> parse---> bind variable---> execute---> close cursor;
對于delete操作只需要進(jìn)行以下幾個(gè)步驟:
open cursor---> parse---> execute---> close cursor;
例一:
create table test(n_id number, v_name varchar2(50), d_insert_date date);
alter table test add constraint pk_id primary key(n_id);
declare
begin
end;
例二:
declare
begin
end;
例三:
declare
begin
end;
例四:
declare
begin
end;
--------------------------------------------------------------------------------------------------
PL/SQL中使用動(dòng)態(tài)SQL編程
在PL/SQL程序設(shè)計(jì)過程中,會(huì)遇到很多必須使用動(dòng)態(tài)sql的地方,oracle系統(tǒng)所tb提供的DMBS_SQL包可以幫助你解決問題。
(一)介紹
(二)一般過程
(三)具體案例
--**********************************
--procedure name:R_Ma_Main
--入口參數(shù):PID股票代碼,PEND時(shí)間,pinterval時(shí)間間隔,totab目標(biāo)數(shù)據(jù)表
--調(diào)用函數(shù):R_GetSql1,R_GetSql2
--功能:具體計(jì)算單支股票ma技術(shù)曲線
--時(shí)間:2001-06-20
--**********************************
create or replace procedure R_Ma_Main
(
pid varchar2,
pend varchar2,
pinterval varchar2,
totab varchar2
) is
--定義數(shù)組
type Date_type is table of varchar2(12) index by binary_integer;
type Index_type is table of number index by binary_integer;
TempDate Date_Type;--時(shí)間數(shù)組
TempIndex Index_Type;--股票收盤價(jià)數(shù)組
TempMa Index_Type;--ma技術(shù)曲線數(shù)據(jù)
cursor1 integer;--游標(biāo)
cursor2 integer;--游標(biāo)
rows_processed integer;--執(zhí)行游標(biāo)返回
TempInter integer;--參與計(jì)算數(shù)值個(gè)數(shù)
TempVal integer;--計(jì)算時(shí)間類型
TempSql varchar2(500);--動(dòng)態(tài)sql語句
MyTime varchar2(12);--時(shí)間
MyIndex number;--數(shù)值
MidIndex number;--中間變量
i integer := 999;
j integer;
begin
TempInter := to_number(substr(pinterval,1,4));
TempVal := to_number(substr(pinterval,5,2));
TempSql := R_GetSql1(pid, pend, TempVal);--得到選擇數(shù)據(jù)的sql語句
--得到當(dāng)天的即時(shí)數(shù)據(jù),并依次保存到數(shù)組中
cursor1 := dbms_sql.open_cursor; --創(chuàng)建游標(biāo)
dbms_sql.parse(cursor1, TempSql, dbms_sql.native); --解析動(dòng)態(tài)sql語句,取兩個(gè)字段,時(shí)間及價(jià)格,其中時(shí)間以14位的varchar2表示
dbms_sql.define_column(cursor1, 1, MyTime, 12); --分別定義sql語句中各字段所對應(yīng)變量
dbms_sql.define_column(cursor1, 2, MyIndex);
rows_processed := dbms_sql.execute(cursor1);
loop
if dbms_sql.fetch_rows(cursor1) > 0 then
begin
dbms_sql.column_value(cursor1, 1, MyTime);
dbms_sql.column_value(cursor1, 2, MyIndex);
TempDate(i) := MyTime;
TempIndex(i) := MyIndex;
i := i - 1;--按倒序的方法填入數(shù)組
end;
else
exit;
end if;
end loop;
dbms_sql.close_cursor(cursor1);
--如果取得的數(shù)據(jù)量不夠計(jì)算個(gè)數(shù),則跳出程序
if i > 999-TempInter then
goto JumpLess;
end if;
--初始化中間變量
MidIndex := 0;
TempIndex(i) := 0;
for j in i..i+TempInter-1 loop
MidIndex := MidIndex + TempIndex(j);
end loop;
--依次對當(dāng)天數(shù)據(jù)計(jì)算ma值,并保存到ma數(shù)組中
for j in i+TempInter..999 loop
MidIndex := MidIndex - TempIndex(j-TempInter) + TempIndex(j);
TempMa(j) := MidIndex/TempInter;
end loop;
if TempVal < 6 then--如果計(jì)算的是分鐘跟天的ma技術(shù)曲線
begin
cursor2 := dbms_sql.open_cursor;
TempSql := 'insert into ' || totab || ' values(:r_no, :i_interval, :i_time, :i_index)';
dbms_sql.parse(cursor2, TempSql, dbms_sql.native);
for j in i+TempInter..999 loop
dbms_sql.bind_variable(cursor2, 'r_no', pid);
dbms_sql.bind_variable(cursor2, 'i_interval', pinterval);
dbms_sql.bind_variable(cursor2, 'i_time', TempDate(j));
dbms_sql.bind_variable(cursor2, 'i_index', TempMa(j));
rows_processed := dbms_sql.execute(cursor2);--插入數(shù)據(jù)
end loop;
end;
end if;
commit;
dbms_sql.close_cursor(cursor2);
--數(shù)據(jù)量不足跳出
<<JumpLess>>
null;
--exception處理,無關(guān)本話題
end;
/
--procedure name:R_Ma_Main
--入口參數(shù):PID股票代碼,PEND時(shí)間,pinterval時(shí)間間隔,totab目標(biāo)數(shù)據(jù)表
--調(diào)用函數(shù):R_GetSql1,R_GetSql2
--功能:具體計(jì)算單支股票ma技術(shù)曲線
--時(shí)間:2001-06-20
--**********************************
create or replace procedure R_Ma_Main
--定義數(shù)組
type Date_type is table of varchar2(12) index by binary_integer;
type Index_type is table of number index by binary_integer;
TempDate Date_Type;--時(shí)間數(shù)組
TempIndex Index_Type;--股票收盤價(jià)數(shù)組
TempMa Index_Type;--ma技術(shù)曲線數(shù)據(jù)
cursor1 integer;--游標(biāo)
cursor2 integer;--游標(biāo)
rows_processed integer;--執(zhí)行游標(biāo)返回
TempInter integer;--參與計(jì)算數(shù)值個(gè)數(shù)
TempVal integer;--計(jì)算時(shí)間類型
TempSql varchar2(500);--動(dòng)態(tài)sql語句
MyTime varchar2(12);--時(shí)間
MyIndex number;--數(shù)值
MidIndex number;--中間變量
i integer := 999;
j integer;
begin
end;
/
(四)個(gè)人觀點(diǎn)
附個(gè)Oracle自帶的流程說明(強(qiáng)大啊):