a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 174|回复: 1

[考试辅导] Oracle数据库存储过程的若干问题

[复制链接]
发表于 2012-8-4 14:06:19 | 显示全部楼层 |阅读模式
  (中国软件网讯)1.在oracle中,数据表别名不能加as,如:
6 r7 r- m; A! [  select a.appname from appinfo a;-- 正确
( g& [. e8 t3 W! u' ~7 k& u  select a.appname from appinfo as a;-- 错误
( A2 l$ p2 f7 b, R" I  也许,是怕和oracle中的存储过程中的关键字as冲突的问题吧: s$ \5 A% c+ v6 E$ V' @
  2.在存储过程中,select某一字段时,后面必须紧跟into,如果select整个记录,利用游标的话就另当别论了。- O* b, I' Q4 e  e, p
  select af.keynode into kn from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 有into,正确编译( c5 H! `9 _+ a# q0 l
  select af.keynode from APPFOUNDATION af where af.appid=aid and af.foundationid=fid;-- 没有into,编译报错,提示:Compilation+ _. d3 F8 a  o2 A
  Error: PLS-00428: an INTO clause is expected in this Select statement5 q' w3 r& G: Z7 F; H# i% R
  3.在利用select...into...语法时,必须先确保数据库中有该条记录,否则会报出"no data found"异常。9 m: M7 _; o9 `3 K! f
  可以在该语法之前,先利用select count(*) from 查看数据库中是否存在该记录,如果存在,再利用select...into...# a. j, [9 I9 Z  f& t3 R- o2 K$ h1 J
  4.在存储过程中,别名不能和字段名称相同,否则虽然编译可以通过,但在运行阶段会报错* X- ^( g1 U0 T+ l
  select keynode into kn from APPFOUNDATION where appid=aid and foundationid=fid;-- 正确运行
1 v5 b6 b* g+ r% H: \- A0 E: F4 P  select af.keynode into kn from APPFOUNDATION af where af.appid=appid and af.foundationid=foundationid;-- 运行阶段报错,提示
  O3 ~" X* l' u+ n8 v  orA-01422:exact fetch returns more than requested number of rows' [8 P# T& R* y9 s+ _3 D2 ?" v1 w
  5.在存储过程中,关于出现null的问题
回复

使用道具 举报

 楼主| 发表于 2012-8-4 14:06:20 | 显示全部楼层

Oracle数据库存储过程的若干问题

  假设有一个表A,定义如下:
! }) A: Y' `) x3 ~# s- Q  如果在存储过程中,使用如下语句:
1 k7 G- T0 v0 A& I9 h5 v  select sum(vcount) into fcount from A where bid='xxxxxx';如果A表中不存在bid="xxxxxx"的记录,则fcount=null(即使fcount定义时设置了默认值,如:fcount number(8):=0依然无效,fcount还是会变成null),这样以后使用fcount时就可能有问题,所以在这里最好先判断一下:
6 d+ v8 b# Y$ S  以下是代码片段:; K0 h( c" p( b. G9 l3 W
  if fcount is null then
( T$ X. \2 v( f+ N: {; d  fcount:=0;+ q% ^6 u9 _6 q) s# t1 r
  end if;( S$ d1 @: a6 e# k# ]7 O3 y+ I
  这样就一切ok了。4 ^$ m" g2 @+ u) C$ e
  6.Hibernate调用oracle存储过程
2 y- Q8 ~# i3 q. a8 z* N, j  以下是代码片段:
2 J& m4 x* ^7 |9 k8 Q! R6 X  this.pnumberManager.getHibernateTemplate().execute(
! h, d+ }" H; j- J1 y4 B" U  new HibernateCallback() ...{
4 a8 d  B" d9 m# ?2 H  public Object doInHibernate(Session session)# ?" k( `: V' h3 i+ S9 N8 v# q
  throws HibernateException, SQLException ...{+ ^! I, B! h8 D& p$ R
  CallableStatement cs = session: c7 n5 n2 f7 ^- @
  .connection()* w; ^% [3 h2 C
  .prepareCall("{call modifyapppnumber_remain(?)}");
7 ]  p" a' u5 y% b; ?! A0 u, a5 @2 ]  cs.setString(1, foundationid);! v! Y, a& K2 f+ j) K
  cs.execute();
( c5 B; [% R' L6 O/ E" B2 _  return null;
. Q  S9 L& _! A( W  }" H+ R+ O- w6 Y) ~1 T1 O, n. d
  });
) f7 W! h6 ^# `$ ~5 H, E  以下是代码片段:8 s9 n: l, Z( m1 I1 E9 y
  create table A(
- J3 ~  c) U7 L# e( [; L" o: M  id varchar2(50) primary key not null,
  n& d8 D' r4 |, y' `, E7 v  vcount number(8) not null,
2 R) |  l4 T7 f& L, Q( y  bid varchar2(50) not null -- 外键
0 Y) Z2 g1 N4 [3 O  );
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-5-7 17:35 , Processed in 0.210597 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表