在学习plsql编程时写的部分存储过程
create or replace procedure test as
names varchar2(20) := 'hello,world,china' ;
begin
dbms_output.put_line(replace(names,',',chr(10)));
end test;
SET SERVEROUTPUT ON
set serveroutput on
BEGIN
DBMS_OUTPUT.PUT_LINE('Hey look, Ma!');
END;
create or replace procedure test as
names varchar2(20) := 'hello' ;
begin
dbms_output.put_line(names);
end test;
create or replace procedure test as
value number;
begin
value :=66;
dbms_output.put_line(value);
end;
VARIABLE x VARCHAR2(10)
BEGIN
:x := 'hullo';
END;
CREATE OR REPLACE procedure test as
CURSOR icur IS SELECT name FROM bill.test_user;
names bill.test_user.name%TYPE;
BEGIN
OPEN icur;
loop
FETCH icur INTO names;
EXIT WHEN icur%NOTFOUND;
if trim(names) = 'wang'
then
dbms_output.put_line(names);
end if;
end loop;
close icur;
END test;
CREATE OR REPLACE procedure test as
CURSOR icur IS SELECT name,age FROM bill.test_user;
names icur%ROWTYPE;
BEGIN
OPEN icur;
loop
FETCH icur INTO names;
EXIT WHEN icur%NOTFOUND;
if trim(names.age) > '28'
then
dbms_output.put_line(names.name||names.age);
end if;
end loop;
close icur;
END test;
CREATE OR REPLACE procedure test as
CURSOR icur IS SELECT name FROM bill.test_user;
names bill.test_user.name%TYPE;
BEGIN
OPEN icur;
loop
FETCH icur INTO names;
EXIT WHEN icur%NOTFOUND;
if trim(names) in ('wang','li')
then
dbms_output.put_line(names);
end if;
end loop;
close icur;
END test;
declare
x number := 45;
begin
dbms_output.put_line(to_char(x,'L999G999'));
end;
/
out:$45
declare
x number :=1;
begin
loop
dbms_output.put_line(to_char(x));
exit when x > 20 ;
x := x + 1;
end loop;
end;
/
declare
x number :=1;
begin
while x < 20
loop
dbms_output.put_line(to_char(x));
x:=x + 1;
end loop;
end;
/
create or replace procedure test
as
begin
for cur in (select * from bill.test_user)
loop
dbms_output.put_line(trim(cur.name) || cur.age);
end loop;
end test;
begin
for x in reverse 1..10
loop
dbms_output.put_line(to_char(x));
end loop;
end;
create or replace package mytest as
type testCur is ref cursor;
procedure test(outCur out testCur);
end mytest;
create or replace package body mytest as
procedure test(outCur out testCur) as
begin
open outCur for
select * from bill.test_user;
end test;
end mytest;
CREATE OR REPLACE procedure test as
value number;
str varchar(60) := 'create' ;
begin
value :=66;
str := str || ' table test_'||value || '(name varchar(10))';
EXECUTE IMMEDIATE str;
dbms_output.put_line(str);
end;
/
CREATE OR REPLACE procedure test(value in number) as
str varchar(60) := 'create' ;
begin
str := str || ' table test_'||value || '(name varchar(10))';
EXECUTE IMMEDIATE str;
dbms_output.put_line(str);
end;
/
EXCEPTION
WHEN OTHERS THEN
log_checkout_error (checked_out_rec);
WHEN NO_DATA_FOUND -- system exception
在程序开发中,常用到返回结果集的存储过程,这个在mysql和sql server
里比较好处理,直接返回查询结果就可以了,
但在oracle里面 要 out 出去,就多了一个步骤,对于不熟悉的兄弟们还得出
上一头汗:),这里我简单介绍一下,以供参考,
1 定义包
oracle 返回的结果集需要自定义一个 CURSOR (游标变量)性质的变量,
这个要在包头定义,所以要建立一个包,如下是包头
CREATE OR REPLACE PACKAGE PAK_rstest
IS
TYPE retcursor IS REF CURSOR;
PROCEDURE pro_read
(
outcurse IN OUT retcursor
);
END; -- Package spec
上面是建立了一个名称为PAK_rstest的包头,里面定义了一个CURSOR 类型,
类型名为retcursor ,有了这个定义我们就可以用他来
返回结果集了,比如该包里面的 pro_read 过程就是 一个返回结果集的过
程,下面是他的包体,
CREATE OR REPLACE PACKAGE BODY PAK_rstest IS
PROCEDURE pro_read
(
outcurse IN OUT retcursor
)
IS
begin
OPEN outcurse FOR
select * from tbl_test
where rownum<6;
return;
end;
END;
这样就定义好了一个包,这个包里面有个返回结果集的过程 pro_read
2 在程序里面调用,
下面就是如果在程序里面调用了,这里用java为例子简单介绍一下,
假设你现在已经有一个Connection conn 对象连接上了数据库(如何连接
数据库我这里就不详细说了),
则用下面的代码调用过程,
if(conn !=null){
String sqlstr = "{call PAK_SMS2_ROUTE.MO_ISSUE(?)}";
CallableStatement cstmt = conn.prepareCall(sqlstr);
cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.CURSOR); //outcurse
cstmt.executeUpdate();
ResultSet rs = (ResultSet) cstmt.getObject(1); // 这里吧信息已经读入rs结果集里面,剩下的大家都熟悉了吧
while (rs.next()) {
System.out.println(rs.getString("s_date1")); //tbl_test 表里的字段名称或是结果集的列名称
System.out.println(rs.getString("s_date2"));
}
conn.close();
conn = null;
}
好了到这里就可以看到返回的结果集内容了,是不是比较简单啊,:)
1. 关于oracle和结果集
其实在大多数情况下,我们并不需要从oracle存储过程里返回一个或多个结果
集,
除非迫不得已。
如果大家用过MS SQL Server或Sybase SQL Server,那么从存储过程返回一个
动态的结果集是一件非常容易的事情,只要在存储过程结束时写上
“select column1,column2,.... from table_list where condition“
就可以了。
但在Oracle中不能这样做. 我们必须使用Oracle Cursor.
在Oracle PL/SQL中,Cursor用来返回一行或多行记录,借助Cursor,我们可以从
结果集中取得所有记录.
Cursor并不难,但是要从Oracle存储过程中返回结果集, 就需要用到Cursor变量,
Cursor变量Oracle PL/SQL
的类型是REF CURSOR, 我们只要定义了REF CURSOR 类型就可以使用Cursor变量.
比如我们可以这样定义:
TYPE ref_cursor IS REF CURSOR;
了解了Cursor以及Cursor变量,下面就介绍如何使用Cursor变量给JDBC返回结果集.
2. 定义表结构
在以下例子里,我们要用到一张表Hotline.
Create table hotline(country varchar2(50),pno varchar2(50));
3. 定义存储过程
create or replace package PKG_HOTLINE
is
type HotlineCursorType is REF CURSOR;
function getHotline return HotlineCursorType;
end;
create or replace package body PKG_HOTLINE
is
function getHotline return HotlineCursorType
is
hotlineCursor HotlineCursorType;
begin
open hotlineCursor for select * from hotline;
return hotlineCursor;
end getHotline;
end;
在这个存储过程里,我们定义了HotlineCursorType 类型,并且在存储过程中
简单地查找所有的记录并返回HotlineCursorType.
4. 测试存储过程
在Oracle SQL/Plus里登陆到数据库. 按以下输入就看到返回的结果集.
SQL> var rs refcursor;
SQL> exec :rs := PKG_HOTLINE.getHotline;
SQL> print rs;
5. Java调用
简单地写一个Java Class.
public void openCursor(){
Connection conn = null;
ResultSet rs = null;
CallableStatement stmt = null;
String sql = “{? = call PKG_HOTLINE.getHotline()}“;
try{
conn = getConnection();
stmt = conn.prepareCall(sql);
stmt.registerOutParameter(1,OracleTypes.CURSOR);
stmt.execute();rs = ((OracleCallableStatement)stmt).getCursor(1);
while(rs.next()){
String country = rs.getString(1);
String pno = rs.getString(2);
System.out.println(“country:“+country+“|pno:”+pno);
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
finally
{
closeConnection(conn,rs,stmt);
}
}
select * from all_tab_columns where table_name='TEST_USER' and owner='BILL' and column_name in ('NAME','AGE');
select table_name from user_tables;
select name,rownum from bill.test_user where rownum <= 10
select count(1) from bill.test_user
select * from tab where tname like 'T_BL%'
select table_name from cat where table_name like 'T_BL%'
select * from user_tables
select * from cat
select * from user_indexes where trim(table_owner)='INFO'
select tablespace_name,sum(bytes) 总字节数,max(bytes),count(*) from dba_free_space group by tablespace_name
select * from dict order by table_name