SQL> declare
2 v_avg_sal number(6,2);
3 begin
4 select avg(sal) into v_avg_sal from emp;
5 dbms_output.put_line('v_avg_sal: ' || v_avg_sal);
6 end;
7 / ---表示执行
v_avg_sal: 2530.88
PL/SQL procedure successfully completed. 二.控制语句
7.if语句
if语法:
IF condition THEN
statements;
[ELSIF condition THEN
statements;]
[ELSE
statements;
]
END IF;
//如果工资小于2000,把雇员的薪水加薪50
setserveroutput on;
declare
v_sal emp.sal%type;
begin
select sal into v_sal from emp where lower(ename)=lower('&name');
if v_sal 100;
i := i+1;
end loop;
end;
//while循环 while condition loop statement1; statement2; ...... end loop;
//例子
declare i number :=1;
begin
while i
type name_table_type is table of varchar2(10) index by binary_integer;
id_table>
name_table name_table_type ;
start_time number;
end_time number;
begin
for i in 1..5000 loop
id_table(i):=i;
name_table(i):='name' || i;
end loop;
start_time := DBMS_UTILITY.GET_TIME;
forall i in 1..5000
insert into tmp01 values(id_table(i),name_table(i));
commit;
end_time := DBMS_UTILITY.GET_TIME;
dbms_output.put_line('total time is: ' || to_char((end_time-start_time)/100));
end;
5.索引表
---语法 TYPE type_name IS TABLE OF element_type [NOT NULL] INDEX BY key_type; indentifer type_name; type_name指定用户自定义数据类型名称 element_type 指定索引表元素的数据类型 key_type指定索引表元素下标的数据类型(varchar2,binary_integer,pls_integer等) indentifer指定索引表变量 索引表的下标可以为负值,而且元素个数没有限制。
---code
set serveroutput on;
declare
type ename_table_type is table of emp.ename%type index by binary_integer;
ename_table ename_table_type;
begin
select ename into ename_table(1) from emp where empno=&no;
dbms_output.put_line('employee name : ' || ename_table(1));
end;
---索引表中使用varchar2
set serveroutput on;
declare
type city_table_type is table of number index by varchar2(10);
city_table city_table_type;
begin
city_table('zhouzq') := 1;
city_table('lidd') := 2;
city_table('lijian') := 3;
dbms_output.put_line('the first element: ' || city_table.first);
dbms_output.put_line('the last element: ' || city_table.last);
end;
6.游标 使用显示游标包括定义游标,打开游标,提取游标和关闭游标。 (1)定义游标 CURSOR cursor_name IS select_statement; (2)打开游标 OPEN cursor_name; (3)提取游标 ---提取一行 FETCH cursor_name into var1,var2...; ---提取多行 FETCH cursor_name BULK COLLECT INTO collect1,collect2...[LIMIT rows]; ---collect1用于指定接受游标的集合变量 (4)关闭游标 CLOSE cursor_name; 显示游标属性:%isopen,%found,%notfound,%rowcount ---显示游标中使用fetch...into
//提取雇员名,薪水
set serveroutput on;
declare
cursor emp_cursor is select ename,sal from emp where deptno=10;
end;
---显示游标中使用fetch...bulk collect into
###显示部门10的所有雇员名
set serveroutput on;
declare
cursor ename_cursor is select ename from emp where deptno=10;
type ename_table_type is table of varchar2(10);
ename_table ename_table_type;
begin
open ename_cursor;
fetch ename_cursor bulk collect into ename_table;
for i in 1..ename_table.count loop
dbms_output.put_line(ename_table(i));
end loop;
close ename_cursor;
end;
---游标FOR循环
语法: FOR record_name IN cursor_name loop statement1;... end loop; cursor_name:已定义的游标名称 record_name:隐含定义的记录变量名 在执行循环体内容之前,oracle会隐含地打开游标,并且每循环一次提取一次数据,提取完所有数据后,会自动退出循环并隐含关闭游标。
---FOR循环游标
set serveroutput on;
declare