存储过程基础

准备数据:

create table student(
sName varchar(20) primary key,
sAge int,
sEmail varchar(100),
sPhone varchar(20),
sAddress varchar(100)
)
begin 
  insert into student values('Jack',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jack1',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jack2',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jack3',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jack54',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jack6',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jack7',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jack21',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Rose',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('rose1',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('rose2',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('rose4',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Adi',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Aditt',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Niyes',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Jassic',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Carken',21,'dfdf@qq.com','2134343','Singapore');
insert into student values('Donview',21,'dfdf@qq.com','2134343','Singapore');
end;

我们查看姓名=Jack的信息,这里只有一条记录

declare 
       l_name student.sname%type;
------这句话的意思就是l_name和student.sname的类型是一样的
      %rowtype就是整个表都是一样的。不用一个字段一个字段的了 begin select sname into l_name from student where sname
='Jack'; dbms_output.put_line('find the name;'|| l_name); end;

 查看姓名包含rose的信息(多条记录的)

DECLARE
  cursor name_rose_cur is
  select sName from student where upper(sName) like upper('%rose%');
  l_name  student.sName%TYPE;
BEGIN
   open name_rose_cur;
   Loop
       fetch name_rose_cur into l_name;
       exit when name_rose_cur%NOTFOUND;
        
       DBMS_OUTPUT.put_line ('find the name: '||l_name);
   end loop;
    
   close name_rose_cur;
END;

或者:

DECLARE
 
  cursor name_rose_cur is
  select * from student where upper(sName) like upper('%rose%');
BEGIN
   for student_cur
    in name_rose_cur
   Loop
       DBMS_OUTPUT.put_line ('find the name: '||student_cur.sName);
   end loop;
END;

 

posted @ 2016-12-07 15:15  陆伟  阅读(227)  评论(0编辑  收藏  举报