blogjava's web log

blogjava's web log
...

oracle练习(异常)

create    table  first
(
  no 
varchar2 ( 10 ),
  name 
varchar2 ( 10 )
)
alter   table   first modify (no  number ); -- 修改表
alter   table  first  add (id  number ) -- 添加列
create   table  first1 
(
  no 
number ,
  name 
varchar ( 20 )
);
alter   table  first  add   primary   key (no); -- 添加主键
select   *   from  first
select   *   from  first
delete  first  where  no = ' 11 '
-- 建序列
create  sequence firstsquence

select  firstsquence.nextvar  from  dual;
select  firstsquence.currval  from  dual;

insert   into  first  values ( ' 001 ' , ' wujunjun ' );
insert   into  first  values ( ' 002 ' , ' wujun ' );
insert   into  first  values ( ' 003 ' , ' lover ' );
insert   into  first  values ( ' 004 ' , ' blogjava ' );
select   *   from  first
drop   table  first

declare
vname first.name
% type;
begin
 
select  name  into  vname  from  first  where  no < ' 003 ' ;
 dbms_output.put_line(
' 成功 ' );
 exception 
  
when  too_many_rows  then
   dbms_output.put_line(
' 查询返回不止一行 ' );
   
when  others  then
    dbms_output.put_line(
' 发生了其他错误 ' );
  
end ;

-- 自己定义异常 
declare
 myexception exception;
 pragma exception_init(myexception,
- 1 );
 
begin
   
insert   into  first  values ( ' 001 ' , ' 11 ' );
   exception
   
when  myexception  then
    Dbms_Output.put_line(
' 错误 ' );
   
when  others  then
   dbms_output.put_line(
' 其他错误 ' );
 
end  ;
  
-- 抛出   
  declare  
 myexception exception;
  xm first.no
% type: = ' 001 ' ;
  
begin
  
  
if  (xm = ' 002 ' then
  raise myexception;
  
else
    
insert   into  first  values ( ' 005 ' , ' 222 ' );
   
end   if ;
   
   exception 
   
when  myexception  then
   dbms_output.put_line(
' myexception error! ' );
  
when  others  then
  raise_application_error(
' -20001 ' , ' error message ' );
  
end ;
  
-- %found用法
declare
 xm first.no
% type;
begin  
 
-- delete from first where name='001';
  select  name  into  xm  from  first  where  no = ' 002 ' ;
 
if  (sql % found = true)  then
  dbms_output.put_line(
' found ' );
  
else
     
insert   into  first  values ( ' 002 ' , 33 );
     dbms_output.put_line(
' not found ' );
 
end   if ;
end ;

-- 游标输出no 和name
declare
 xm first
% rowtype;
 
cursor  cc  is   select   *   from  first;
begin  
  
for  aa  in  cc
  loop
  
--   if(aa.name='002') then
   dbms_output.put_line(aa.no || aa.name);
  
--  end if;
   end  loop;
end ;

declare
TYPE ref_cursor 
IS  REF  CURSOR ;


-- 建包
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 ;
 
end ;
 
begin

end ;

create   or   replace  package pack11  is
 type ref_cursor1 
is  ref  cursor  ;
 
function  getCursor  return  ref_cursor1;
end ;

create   or   replace  package body pack11  is
 
 
function  getCursor  return  ref_cursor1  is
  cc ref_cursor1;
 
begin
  
open  cc  for   select   *   from  first;
  
return  cc;
 
end ;
end ;

posted on 2006-04-28 08:48 record java and net 阅读(363) 评论(0)  编辑  收藏 所属分类: Database


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论