--打开控制台显示程序
set serveroutput on;
--最基本的匿名块
begin
dbms_output.put_line('hello world');
end;
--带有声明部分的匿名块
declare
v_info varchar2(30);
begin
v_info:='hello world';
dbms_output.put_line(v_info);
end;
declare
empname varchar(50);
begin
select ename into empname from emp where empno=7788;
dbms_output.put_line(empname);
end;
--if语句
declare
v_sal number(5,0);
v_level nvarchar2(1);
begin
select sal into v_sal from emp where empno=7788;
if v_sal<3000 then
v_level:='低';
elsif v_sal>5000 then
v_level:='高';
else
v_level:='中';
end if;
dbms_output.put_line(v_sal||' '||v_level);
end;
--case语句
declare
v_sal number(5,0);
v_level nvarchar2(1);
v_area nvarchar2(20);
begin
select sal into v_sal from emp where empno=7788;
case
when v_sal < 3000 then
v_level:='低';
when v_sal > 5000 then
v_level:='高';
else
v_level:='中';
end case;
case v_level
when '低' then
v_area:='3000以下';
when '高' then
v_area:='5000以上';
else
v_area:='3000-5000';
end case;
dbms_output.put_line(v_sal||' '||v_level||' '||v_area);
end;
--for循环
begin
for i in 1..10 loop
dbms_output.put_line(i);
end loop;
end;
--while循环
declare
i number(2):=0;
begin
while i < 10 loop
i:=i+1;
dbms_output.put_line(i);
end loop;
end;
--loop循环
--将emp表中所有工资低于2000的加上100,直到都高于2000;
declare
count_low number(5);
begin
loop
select count(*) into count_low from emp where sal < 2000;
if count_low > 0 then
update emp set sal=sal+100 where sal < 2000;
else
exit;
end if;
end loop;
end;
--声明与表中变量相同类型的变量
declare
e_name emp.ename%type;
e_sal emp.sal%type;
begin
select ename into e_name from emp where empno=7788;
select sal into e_sal from emp where empno=7788;
dbms_output.put_line(e_name||' '||e_sal);
end;
--声明一个与表中一行类型相同的变量
declare
row_emp emp%rowtype;
begin
select * into row_emp from emp where empno=7839;
dbms_output.put_line(row_emp.ename||' '||row_emp.sal);--打印出该行变量其中两个值
end;
--声明一个复合变量
declare
type emp_record is record--定义一个类型
(
e_name emp.ename%type,
e_sal emp.sal%type
);
e_info emp_record;--定义一个该类型的变量
begin
select ename,sal into e_info from emp where empno=7788;
dbms_output.put_line(e_info.e_name||' '||e_info.e_sal);--打印出变量中的元素
end;
--索引表
declare
type name_emp is table of emp.ename%type
index by binary_integer;--定义一个索引类型
e_name name_emp;--定义一个索引变量
begin
select ename into e_name(-1) from emp where empno=7788;
select ename into e_name(1) from emp where empno=7839;
dbms_output.put_line(e_name(-1)||' '||e_name(1));--打印出变量中的值
end;
--游标
declare
type emp_cur is ref cursor;--定义游标类型
e_cur emp_cur;--声明游标变量
e_name emp.ename%type;
e_sal emp.sal%type;
begin
open e_cur for select ename,sal from emp;--打开游标
loop
fetch e_cur into e_name,e_sal;--从游标变量中取值
exit when e_cur%notfound;--判断游标变量里的值是否被找到,找不到时退出loop循环
dbms_output.put_line(e_name||' '||e_sal);
end loop;
end;
--建立一个测试表student;
create table student(
stid number(4) primary key,--系统编号
sno nvarchar2(5) not null,--将系统编号前加一S作为学生编号
sname nvarchar2(10) not null,
sex number(1) not null,
cid number(3) not null--班级编号
);
--建立一个序列seq_stu;
create sequence seq_stu
start with 1
increment by 1;
commit;
--建立触发器
create or replace trigger ad_stu
before insert on student
FOR EACH ROW
declare
begin
SELECT seq_stu.nextval INTO :new.stid FROM dual;
SELECT LPAD(:new.stid,5,'S0000') INTO :new.sno FROM dual;
end;
--测试
INSERT INTO student VALUES('','','小波',1,1);
--查询
SELECT * FROM student;
--存储过程
--定义包
CREATE OR REPLACE PACKAGE pack_emp IS
TYPE cur_emp IS REF CURSOR;--定义一个游标
END;
--定义存储过程
CREATE OR REPLACE PROCEDURE proc_sal(sal_result OUT pack_emp.cur_emp)
AS
BEGIN
OPEN sal_result FOR SELECT d.dname, SUM(sal), ROUND(AVG(sal)) FROM dept d LEFT JOIN emp e ON d.deptno = e.deptno GROUP BY dname;
END;
--JAVA代码
Connection conn = null;
CallableStatement cs = null;
ResultSet rs = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection("jdbc:oracle:thin:@10.25.129.6:1521:Orcl", "SCOTT", "TIGER");
cs = conn.prepareCall("{call proc_sal(?)}");
cs.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR);
cs.execute();
rs = (ResultSet)cs.getObject(1);
while (rs.next()) {
System.out.println(rs.getString(1) + ": " + rs.getInt(2) + " "
+ rs.getInt(3));
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
rs.close();
cs.close();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
--建立分页
--声明一个包
CREATE OR REPLACE PACKAGE pack_page IS
TYPE cur_page IS REF CURSOR;--游标,返回分布结果集
PROCEDURE pro_page (
p_currentPage IN NUMBER,--当前页
p_recoredInPage IN NUMBER,--每页记录条数
p_tableName IN VARCHAR2,--表名
p_pageSize OUT NUMBER,--总页数
p_result OUT pack_page.cur_page--结果集
);
END;
--建立包体
CREATE OR REPLACE PACKAGE BODY pack_page IS
PROCEDURE pro_page(
p_currentPage IN NUMBER,--当前页
p_recoredInPage IN NUMBER,--每页记录条数
p_tableName IN VARCHAR2,--表名
p_pageSize OUT NUMBER,--总页数
p_result OUT pack_page.cur_page--结果集
)
IS
v_recordSize NUMBER(5);--总记录数
v_sql VARCHAR2(300);
v_begin NUMBER(5);--每页起始记录
v_end NUMBER(5);--每页的终止记录
BEGIN
v_sql:='select count(*) from '||p_tableName;
EXECUTE IMMEDIATE v_sql INTO v_recordSize;
p_pageSize := CEIL(v_recordSize/p_recoredInPage);
v_begin := (p_currentPage-1) * p_recoredInPage + 1;
v_end := p_currentPage * p_recoredInPage;
v_sql := 'select * from (select t.*, rownum rn from '||p_tableName||' t where rownum<='||v_end||') where rn>='||v_begin;
OPEN p_result FOR v_sql;
END;
END;