1 create or replace procedure procedure_name --存儲過程名字 2 ( 3 --進行輸入/輸出的量 量_name in out 量_類型 4 --e.g. 5 username in varchar2, --varchar2類型無需標明長度 6 id out number(38) 7 ) 8 is/as 9 --基本認為is和as是一致的,但是略有區別,在這個部分進行變量和常量的聲明 //類似PL/SQL語句塊中的declare部分 10 11 --變量/常量_name (constant) 1.變量/常量_類型; 12 -- 2.table_name.col_name%type //這個變量沿用這個表中的這個列的類型 13 -- 3._name //某個變量需要從外部傳入,&**_name作為一個占位符 14 -- 4.自定義類型 //使用對象的概念理解 15 -- 5.table_name%rowtype 16 17 --e.g. 18 --1 19 i constant number:=5; 20 username varchar2(40); 21 --2 22 username tb_user.username%type; 23 --3 24 username varchar2:=&name; 25 --4 26 type type_name is record 27 ( 28 id number(38); 29 username varchar2(40); 30 ); 31 type_obj type_name; --(將某行的部分需要的值放進去)使用的時候:select tb.id,tb.username into type_obj from tb_user tb 32 --5 33 users tb_user%rowtype; --(將某行的值放進去)使用的時候:select * into users from tb_user where id='1'; 34 35 36 --聲明游標,獲取查詢語句的結果集(多行多列); //游標申明中不要使用into子句 cursor cur_name is select col_name,col_name,, from table_name; 37 --e.g. 38 cursor temCur is select * from tb_user; 39 40 begin 41 --放置sql語句和pl/sql語句塊 42 43 --賦值 44 a:=b; 45 46 --輸出語句 47 dbms_output.put_line(''||''||''); --如果需要顯示結果,set serveroutput on 48 49 --sql語句 50 --增 51 insert into table_name(col_name,col_name,,) 52 values(val_val,val_val,,); 53 54 --改 55 update table_name set col_name = val_val; 56 57 --刪 58 delete from table_name …… 59 60 --游標 61 --1.顯式游標 62 --2.隱式游標 select * into emp from table_name …… 63 64 65 --打開游標 66 --open cur_name; 67 --將游標中的值賦值到某值中 68 --fetch cur_name into get_name,get_name,,; 69 --關閉游標 70 --close cur_name; 71 72 --游標狀態信息 73 --%isoipen //boolean 74 --%notfound // 75 --%found // 76 --%rowcount //目前為止,總行數 77 78 select col_name into temp_name from table_name --將表中的值(一行一列的值)取出放到這個“臨時變量中” 79 80 --循環 81 --1. 82 loop 83 *** 84 exit when *** 85 end loop; 86 87 --2. 88 while *** 89 loop 90 *** 91 end loop; 92 93 --3. 94 for index in[reverse] *** 95 loop 96 *** 97 end loop; 98 99 --判斷 100 if *** then *** 101 elsif *** then *** 102 else *** 103 end if; 104 105 --提交事務 106 commit; 107 108 exception 109 --//異常處理 110 when too_many_rows then 111 *** 112 when no_data_found then 113 *** 114 *when others then 115 *** 116 --rollback; 117 --commit 118 end procedure_name; 119 120 121 122 --調用存儲過程 123 exec pro_name(); 124 125 126 127 128 129 130 131 132 133 /* 134 --使用循環獲取游標數據 135 declare 136 -- 定義emp類型 137 emp employees%rowtype; 138 -- 定義一個游標:拿到所有員工 139 cursor my_corsor is select * from employees; 140 begin 141 -- 打開游標 142 open my_corsor; 143 --循環開始打印游標 144 loop 145 -- 移動光標並獲取相應的數據 146 fetch my_corsor into emp; 147 -- 如果沒有相應數據則離開 148 exit when my_corsor%notfound; 149 -- 沒有離開代表有數據,則可以打印展示出來 150 dbms_output.put_line(emp.first_name||' '|| emp.salary); 151 end loop; 152 --關閉游標 153 close my_corsor; 154 end; 155 156 157 --使用游標的for循環 158 PL/SQL語言提供了游標的for循環語句。 159 自動的執行游標的open,fetch和close。 160 當進入循環后,游標for循環語句會自動的打開游標,並提取第一行數據, 161 當程序處理完當前數據后,自動提取下一行數據。 162 當結果集中的內容提取完畢后,自動關閉游標。 163 164 格式: 165 FOR variables IN cursor_name(value,value...) LOOP 166 --處理語句 167 END LOOP; 168 169 declare 170 171 --定義一個游標:拿到所有員工 172 cursor my_corsor is select * from employees; 173 begin 174 --循環開始打印游標 175 for emp in my_corsor loop 176 -- 沒有離開代表有數據,則可以打印展示出來 177 dbms_output.put_line(emp.first_name||' '|| emp.salary); 178 end loop; 179 end; 180 181 182 183 */