plsql练习笔记

用于练习的表:

 1 create sequence test_id
 2 increment by 1
 3 start with 1
 4 maxvalue 99999
 5 minvalue 1
 6 cache 20
 7 cycle;
 8 create table test_table(
 9        testid int not null primary key,
10        testname nvarchar2(20),
11        testpwd nvarchar2(20),
12        testbirth date
13 );
14 insert into test_table values(test_id.nextval,'admin','123456',to_date('2012-5-6 12:31:25','yyyy-mm-dd HH:mi:ss'));
15 insert into test_table values(test_id.nextval,'cglin','123456',to_date('2012-5-6 12:31:25','yyyy-mm-dd HH:mi:ss'));
16 
17 select * from test_user.test_table;

pl_sql
◇◆列类型

 1 declare
 2        tname test_user.test_table.testname%type;
 3 begin
 4   select testname into tname from test_user.test_table where testid = 2;
 5   dbms_output.put_line('tname:'||tname);
 6   exception
 7     when too_many_rows then
 8       dbms_output.put_line('exception:出现异常!返回行数太多!');
 9     when no_data_found then
10       dbms_output.put_line('exception:出现异常!没有数据返回!');
11 end;
12 
13 
14 declare
15       tname test_user.test_table.testname%type;
16 begin
17   select testname into tname from test_user.test_table where testid = 34;
18   dbms_output.put_line('tname:'||tname);
19   exception
20     when others then
21       dbms_output.put_line('有异常出现了!');
22 end;

◇◆行类型

 1 declare
 2   trow test_user.test_table%rowtype;
 3 begin
 4   --select * into trow from test_user.test_table where testid = 2;
 5   select * into trow from test_user.test_table;
 6   dbms_output.put_line('row:id='||trow.testid||'  name='||trow.testname||'  pwd='||trow.testpwd||'  birth='||to_char(trow.testbirth,'yyyy-mm-dd HH:mi:ss'));
 7   exception 
 8  when too_many_rows then
 9     dbms_output.put_line('exception:出现异常!返回行数太多!');
10  when no_data_found then
11       dbms_output.put_line('exception:出现异常!没有数据返回!');
12 end;

◇◆if条件语句

 1 declare
 2       id int;
 3 begin
 4   id :=&亲输入数字;
 5   if id < 10 then
 6      dbms_output.put_line('您输入的数字小于10');
 7   elsif id = 10 then
 8      dbms_output.put_line('您输入的数字等于10');
 9   else
10      dbms_output.put_line('您输入的数字大于10');
11   end if;
12 end;

◇◆case语句

 1 declare
 2   id int;
 3 begin
 4   id :=&请输入数字;
 5   case id
 6     when 1 then
 7       dbms_output.put_line('您输入的是1');
 8     when 2 then
 9       dbms_output.put_line('您输入的是2');
10     else
11       dbms_output.put_line('您输入的不是1也不是2');
12   end case;
13   
14 end;

◇◆case条件语句

 1 declare
 2   id int;
 3 begin
 4   id :=&请输入数字;
 5   case
 6     when id = 1 then
 7       dbms_output.put_line('您输入的是1');
 8     when id = 2 then
 9       dbms_output.put_line('您输入的是2');
10     else
11       dbms_output.put_line('您输入的不是1也不是2');
12   end case;
13   
14 end;

◇◆loop循环

 1 declare
 2   id int;
 3 begin
 4   id :=&请输入数字;
 5   loop
 6     dbms_output.put_line(id);
 7     id := id + 1;
 8     exit when id > 10;
 9   end loop;
10 end;

◇◆while循环

1 declare
2   id int;
3 begin
4   id :=&请输入数字;
5   while id < 10 loop
6     dbms_output.put_line(id);
7     id := id + 1;
8   end loop;
9 end;

◇◆for循环

1 declare
2   id int;
3 begin
4   id :=&请输入数字;
5   for i in 1..id loop
6     dbms_output.put_line(i);
7   end loop;
8 end;

◇◆99乘法表

 1 declare
 2   i int;
 3 begin
 4   i := 9;
 5   for j in 1..i loop
 6     for a in 1..j loop
 7       dbms_output.put(j||'x'||a||'='||a*j||'  ');
 8     end loop;
 9     dbms_output.put_line('');
10   end loop;
11 end;

◇◆显示游标

 1 declare
 2   cursor test_cursor
 3   is
 4   select * from test_user.test_table;
 5   
 6   str_cursor test_user.test_table%rowtype;
 7 begin
 8   open test_cursor;
 9        
10        loop
11          fetch test_cursor into str_cursor;
12          exit when test_cursor%notfound;
13          dbms_output.put_line('testname:'||str_cursor.testname||'    testpwd:'||str_cursor.testpwd);
14        end loop;
15   close test_cursor;
16   
17 end;

◇◆隐示游标

1 declare
2   cursor test_cursor
3   is 
4   select * from test_user.test_table;
5 begin
6   for str_cursor in test_cursor loop
7     dbms_output.put_line('testname:'||str_cursor.testname);
8   end loop;
9 end;

◇◆参数游标

1 declare
2   cursor test_cursor(tid int)
3   is
4   select * from test_user.test_table where testid=tid;
5 begin
6   for str_cursor in test_cursor(&编号) loop
7     dbms_output.put_line('testname:'||str_cursor.testname);
8   end loop;
9 end;

 

 

 

posted @ 2013-04-06 20:54  果c子  阅读(150)  评论(0编辑  收藏  举报