锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
Oracle涓嶅儚SQLServer閭f牱鍦ㄥ瓨鍌ㄨ繃紼嬩腑鐢⊿elect灝卞彲浠ヨ繑鍥炵粨鏋滈泦錛岃屾槸閫氳繃Out鍨嬬殑鍙傛暟榪涜緇撴灉闆嗚繑鍥炵殑銆傚疄闄呬笂鏄埄鐢≧EF CURSOR
--procedure榪斿洖璁板綍闆嗭細(xì)
----------------------澹版槑涓涓狿ackage--------------
CREATE OR REPLACE PACKAGE pkg_test
AS
TYPE myrctype IS REF CURSOR;
PROCEDURE get (p_id NUMBER, p_rc OUT myrctype); --Package涓0鏄庡悕涓篻et 鐨凱rocedure(鍙湁鎺ュ彛娌″唴瀹?
END pkg_test;
--------------------------------------------------------
-----------------澹版槑Package Body錛屽嵆涓婇潰Package涓殑鍐呭錛屽寘鎷琍rocedure get---------------------
CREATE OR REPLACE PACKAGE BODY pkg_test
AS
PROCEDURE get (p_id NUMBER, p_rc OUT myrctype)
IS
sqlstr VARCHAR2 (500);
BEGIN
IF p_id = 0 THEN
OPEN p_rc FOR
SELECT ID, NAME, sex, address, postcode, birthday
FROM student;
ELSE
sqlstr :=
'select id,name,sex,address,postcode,birthday
from student where id=:w_id'; --w_id鏄釜鍙傛暟錛?/p>
--浠ヤ笅 p_rc鏄釜REF CURSOR娓告爣綾誨瀷錛岃屼笖鏄疧UT鍨嬪弬鏁幫紝鍗沖彲榪斿洖涓涓褰曢泦浜?jiǎn)銆俇SING p_id灝辨槸鏇挎崲涓婇潰SQL涓?w_id鍊兼媺:)
OPEN p_rc FOR sqlstr USING p_id;銆
END IF;
END get;
END pkg_test;
--function榪斿洖璁板綍闆嗙殑渚嬪瓙錛屽師鐞嗗拰涓婇潰鐩稿悓錛岃屾槸鐢╢unction鐨剅eturn鍊兼潵榪斿洖璁板綍闆嗐?/strong>
鍑芥暟榪斿洖璁板綍闆?
寤虹珛甯ef cursor瀹氫箟鐨勫寘鍜屽寘浣撳強(qiáng)鍑芥暟錛?
CREATE OR REPLACE
package pkg_test as
/* 瀹氫箟ref cursor綾誨瀷
涓嶅姞return綾誨瀷錛屼負(fù)寮辯被鍨嬶紝鍏佽鍔ㄦ乻ql鏌ヨ錛?
鍚﹀垯涓哄己綾誨瀷錛屾棤娉曚嬌鐢ㄥ姩鎬乻ql鏌ヨ;
*/
type myrctype is ref cursor;
function get(intID number) return myrctype;
end pkg_test;
/
CREATE OR REPLACE
package body pkg_test as
--鍑芥暟浣?
function get(intID number) return myrctype is
rc myrctype; --瀹氫箟ref cursor鍙橀噺
sqlstr varchar2(500);
begin
if intID=0 then
--闈?rùn)鎬佹祴璇曪紝鐩存帴鐢╯elect璇彞鐩存帴榪斿洖緇撴灉
open rc for select id,name,sex,address,postcode,birthday from student;
else
--鍔ㄦ乻ql璧嬪鹼紝鐢?w_id鏉ョ敵鏄庤鍙橀噺浠庡閮ㄨ幏寰?
sqlstr := 'select id,name,sex,address,postcode,birthday from student where id=:w_id';
--鍔ㄦ佹祴璇曪紝鐢╯qlstr瀛楃涓茶繑鍥炵粨鏋滐紝鐢╱sing鍏抽敭璇嶄紶閫掑弬鏁?
open rc for sqlstr using intid;
end if;
return rc;
end get;
end pkg_test;
瀛樺偍榪囩▼閲屼笉鑳界洿鎺ヤ嬌鐢―DL璇彞錛屾墍浠ュ彧鑳戒嬌鐢ㄥ姩鎬丼QL璇彞鏉ユ墽琛?/span>
--ON COMMIT DELETE ROWS 璇存槑涓存椂琛ㄦ槸浜嬪姟鎸囧畾錛屾瘡嬈℃彁浜ゅ悗ORACLE灝嗘埅鏂〃錛堝垹闄ゅ叏閮ㄨ錛?
--ON COMMIT PRESERVE ROWS 璇存槑涓存椂琛ㄦ槸浼?xì)璇濇寚瀹氬Q屽綋涓柇浼?xì)璇濇椂ORACLE灝嗘埅鏂〃銆?/p>
CREATE OR REPLACE PROCEDURE temptest
(p_searchDate IN DATE)
IS
v_count INT;
str varchar2(300);
BEGIN
v_count := 0;
str:='drop table SETT_DAILYTEST';
execute immediate str;
str:='CREATE GLOBAL TEMPORARY TABLE SETT_DAILYTEST (
NACCOUNTID NUMBER not null,
NSUBACCOUNTID NUMBER not null)
ON COMMIT PRESERVE ROWS';
execute immediate str; ----浣跨敤鍔ㄦ丼QL璇彞鏉ユ墽琛?/span>
str:='insert into SETT_DAILYTEST (select naccountid,nsubaccountid from sett_dailyaccountbalance)';
execute immediate str;
END temptest;
涓婇潰寤虹珛涓涓復(fù)鏃惰〃鐨勫瓨鍌ㄨ繃紼?/p>
涓嬮潰鏄墽琛屼竴浜涙搷浣?鍚戜復(fù)鏃惰〃鍐欐暟鎹?/p>
CREATE OR REPLACE PROCEDURE PR_DAILYCHECK
(
p_Date IN DATE,
p_Office IN INTEGER,
p_Currency IN INTEGER,
P_Check IN INTEGER,
p_countNum OUT INTEGER)
IS
v_count INT;
BEGIN
v_count := 0;
IF p_Date IS NULL THEN
dbms_output.put_line('鏃ユ湡涓嶈兘涓虹┖');
ELSE
IF P_Check = 1 THEN
insert into SETT_DAILYTEST (select naccountid,nsubaccountid from sett_dailyaccountbalance
where dtdate = p_Date);
select
count(sd.naccountid) into v_count
from sett_subaccount ss,sett_account sa,sett_dailytest sd
where sd.naccountid = sa.id and sd.nsubaccountid = ss.id and sa.id = ss.naccountid
AND sa.nofficeid = p_Office AND sa.ncurrencyid = p_Currency
and rownum < 2;
COMMIT;
p_countNum := v_count;
dbms_output.put_line(p_countNum);
END IF;
IF P_Check = 2 THEN
insert into SETT_DAILYTEST (select naccountid,nsubaccountid from sett_dailyaccountbalance
where dtdate = p_Date);
select
count(sd.naccountid) into v_count
from sett_cfsubaccount ss,sett_account sa,sett_dailytest sd
where sd.naccountid = sa.id and sd.nsubaccountid = ss.id and sa.id = ss.naccountid
AND sa.nofficeid = p_Office AND sa.ncurrencyid = p_Currency
and rownum < 2;
COMMIT;
p_countNum := v_count;
dbms_output.put_line(p_countNum);
END IF;
END IF;
END PR_DAILYCHECK;
璇峰弬鑰?lt;<Oracle PL/SQL Programming>> Chapter 11銆丆hapter 12銆?/p>
---------------------- 鍗曠淮鏁扮粍 ------------------------
DECLARE
TYPE emp_ssn_array IS TABLE OF NUMBER INDEX BY BINARY_INTEGER; ----娉細(xì)澹版槑涓涓狢ollection
best_employees emp_ssn_array;
worst_employees emp_ssn_array;
BEGIN
best_employees(1) := '123456';
best_employees(2) := '888888';
worst_employees(1) := '222222';
worst_employees(2) := '666666';
FOR i IN 1..best_employees.count LOOP
DBMS_OUTPUT.PUT_LINE('i='|| i || ', best_employees= ' ||best_employees(i)
|| ', worst_employees= ' ||worst_employees(i));
END LOOP;
END;
---------------------- 澶氱淮鏁扮粍 ------------------------
DECLARE
TYPE emp_type IS RECORD ---------娉細(xì)澹版槑涓涓猂ecord綾誨瀷銆emp_type
( emp_id employee_table.emp_id%TYPE, 銆銆銆銆銆銆銆銆銆銆----Record綾誨瀷涓殑鎴愬憳...
emp_name employee_table.emp_name%TYPE,
emp_gender employee_table.emp_gender%TYPE );
TYPE emp_type_array IS TABLE OF ----娉細(xì)澹版槑涓涓狢ollection綾誨瀷 emp_type_array 錛屽叾涓厓绱犱負(fù)emp_type綾誨瀷
emp_type INDEX BY BINARY_INTEGER;
emp_rec_array emp_type_array;
emp_rec emp_type;
BEGIN
emp_rec.emp_id := 300000000;
emp_rec.emp_name := 'Barbara';
emp_rec.emp_gender := 'Female';
emp_rec_array(1) := emp_rec;
emp_rec.emp_id := 300000008;
emp_rec.emp_name := 'Rick';
emp_rec.emp_gender := 'Male';
emp_rec_array(2) := emp_rec;
FOR i IN 1..emp_rec_array.count LOOP
DBMS_OUTPUT.PUT_LINE('i='||i
||', emp_id ='||emp_rec_array(i).emp_id
||', emp_name ='||emp_rec_array(i).emp_name
||', emp_gender = '||emp_rec_array(i).emp_gender);
END LOOP;
END;
-------------- Result --------------
i=1, emp_id =300000000, emp_name =Barbara, emp_gender = Female
i=2, emp_id =300000008, emp_name =Rick, emp_gender = Male