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 */