oracle 存储过程基本语法
来源:互联网 发布:淮北交警酒驾查处数据 编辑:程序博客网 时间:2024/05/16 10:29
关键字: oracle 1.基本结构
CREATE OR REPLACE PROCEDURE 存储过程名字
(
参数1 IN NUMBER,
参数2 IN NUMBER
) IS
变量1 INTEGER :=0;
变量2 DATE;
BEGIN
END 存储过程名字
2.SELECT INTO STATEMENT
将select查询的结果存入到变量中,可以同时将多个列存储多个变量中,必须有一条
记录,否则抛出异常(如果没有记录抛出NO_DATA_FOUND)
例子:
BEGIN
SELECT col1,col2 into 变量1,变量2 FROM typestruct where xxx;
EXCEPTION
WHEN NO_DATA_FOUND THEN
xxxx;
END;
...
3.IF 判断
IF V_TEST=1 THEN
BEGIN
do something
END;
END IF;
4.while 循环
WHILE V_TEST=1 LOOP
BEGIN
XXXX
END;
END LOOP;
5.变量赋值
V_TEST := 123;
6.用for in 使用cursor
...
IS
CURSOR cur IS SELECT * FROM xxx;
BEGIN
FOR cur_result in cur LOOP
BEGIN
V_SUM :=cur_result.列名1+cur_result.列名2
END;
END LOOP;
END;
7.带参数的cursor
CURSOR C_USER(C_ID NUMBER) IS SELECT NAME FROM USER WHERE TYPEID=C_ID;
OPEN C_USER(变量值);
LOOP
FETCH C_USER INTO V_NAME;
EXIT FETCH C_USER%NOTFOUND;
do something
END LOOP;
CLOSE C_USER;
8.用pl/sql developer debug
连接数据库后建立一个Test WINDOW
在窗口输入调用SP的代码,F9开始debug,CTRL+N单步调试
关于oracle存储过程的若干问题备忘
1.在oracle中,数据表别名不能加as,如:
select a.appname from appinfo as a; -- 错误
2.在存储过程中,select某一字段时,后面必须紧跟into,如果select整个记录,利用游标的话就另当别论了。
select af.keynode from APPFOUNDATION af where af.appid = aid and af.foundationid= fid; -- 没有into,编译报错,提示:Compilation
Error: PLS -00428 : an INTO clause is expected in this SELECT statement
3.在利用select...into...语法时,必须先确保数据库中有该条记录,否则会报出"no data found"异常。
4.在存储过程中,别名不能和字段名称相同,否则虽然编译可以通过,但在运行阶段会报错
select af.keynode into kn from APPFOUNDATION af where af.appid= appid and af.foundationid= foundationid ;-- 运行阶段报错,提示
ORA - 01422 :exact fetch returns more than requested number of rows
5.在存储过程中,关于出现null的问题
id varchar2 ( 50 ) primary key not null ,
vcount number ( 8 ) not null ,
bid varchar2 ( 50 ) not null -- 外键
);
fcount:= 0 ;
end if ;
6.Hibernate调用oracle存储过程
new HibernateCallback() ... {
public Object doInHibernate(Session session)
throws HibernateException, SQLException ... {
CallableStatement cs = session
.connection()
.prepareCall(" {call modifyapppnumber_remain(?)}" );
cs.setString(1 , foundationid);
cs.execute();
return null ;
}
});
public class DBDeclareTest{
private Connection con = null;// 数据库连接
private java.sql.CallableStatement cs = null;//执行存储过程的接口
private int key;
private String name;
//在构造函数中构造连接
public DBDeclareTest(){
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://127.0.0.1:1433;databasename=pubs","sa","");
System.out.println ("con ok");
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//调用无返回值的存储过程
public void getOK1(){
try {
String sql="{call pro_mypro1(?,?)}";
cs = con.prepareCall(sql);
cs.setInt(1,key);
cs.setString(2,name);
cs.executeUpdate();
System.out.println ("执行成功,可以休息了。。。。");
cs.close();
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//调用有返回值的存储过程
public void getOK2(){
try {
String sql = "{call pro_mypro2(?,?)}";
cs = con.prepareCall(sql);
//设置output外部传参
cs.registerOutParameter(1,java.sql.Types.BIT);
cs.setString(2,name);
cs.execute();
//获得output外部传参的值
int numOut = cs.getInt(1);
if(numOut ==1){
System.out.println ("数据表存在");
}else{
System.out.println ("数据表不存在!!!");
}
cs.close();
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
//返回结果集的存储过程
public void getOK3(){
try {
String sql = "{call pro_mypro3(?,?)}";
cs = con.prepareCall(sql);
//设置output外部传参
cs.registerOutParameter(1,java.sql.Types.BIT);
cs.setString(2,name);
boolean flag = cs.execute();
System.out.println (flag);
if(flag){
System.out.println ("OKOKOKO");
ResultSet rs = cs.getResultSet();
ResultSetMetaData rsmd =rs.getMetaData();
int numberOfColumns = rsmd.getColumnCount();
while(rs.next()){
for(int i=0;i<numberOfColumns;i++)
{
System.out.println (rsmd.getColumnName(i)+" ");
System.out.println (rs.getObject(i)+" ");
}
}
}else
{
System.out.println ("表名不存在:"+cs.getBoolean(1));
}
cs.close();
con.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public void setKey(int k){
this.key = k;
}
public int getKey(){
return this.key;
}
public void setName(String n){
this.name = n;
}
public String getName(){
return this.name;
}
}
- oracle 存储过程基本语法
- oracle存储过程基本语法
- ORACLE存储过程----基本语法
- oracle存储过程基本语法
- oracle 存储过程基本语法
- oracle 存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- oracle 存储过程基本语法
- oracle存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- Oracle存储过程基本语法
- DNS域名解析系统简介
- linq 嵌套if查询 判断输入是否为空,并查询
- 命令模式下的java带包及类的的编译及执行(error出现找不到符号)
- 3G名家大讲堂:从Android和Windows Phone 7观移动应用界面开发
- Oracle Scheduler(一)——Jobs(基本使用)!
- oracle 存储过程基本语法
- SystemWideSaltSource
- C#对word、excel读取
- SMS
- High Load And Linux Server. Part 1. Router And NAT-server
- plsql 中文乱码 字符集解决方案
- jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关
- android adb am命令
- 网络技术日志