oracle常用实例练习(2)

/**
   *第二次熟悉pl/sql常用语句
   *目的:对基本的语句能够熟练操作。并保证在这些基础语法问题上的错误降低到最低。
   *version:1.0
   *Author:ITelite
   *date:2008-04-10
*/

/*
Create Table TESTTABLE(
       "RECORDNUMBER" number(4) Not Null,
       "CURRENTDATE" Date Not Null
       )
       Tablespace "USER1" --这里的表空间其实可以不去管,这样它就会在当前用户的表空间中创建一个表
      
       */
/*
这里实例一
Declare
maxrecords Constant Int:=100;
i Int:=1;
  Begin
        For i In 1..maxrecords Loop
            Insert Into hqf.testtable(recordnumber,currentdate)
            Values(i,Sysdate);
            dbms_output.put_line('现在出入的内容是:'||i||'   '||Sysdate);
            Commit;                                                       --这里切记要commit否则将不会将数据提交到表中
        End Loop;
        dbms_output.put_line('记录已经按照计划全部插入,请查看!');
  End;

*/

/*

实例证实数据的最大值
Declare
        str Varchar2(3000);                           --这里我证明了varchar2的最大长度不是2000(我看到书上为2000)
        i Int:=3000;
        Begin
             For i In 1..3000 Loop
             str:=str||'c';
             dbms_output.put_line('现在的长度为:!'||i);
             End Loop;
        End;
       */
      
/*
完整定义了一个记录类型的变量,简单应用。需要挖掘它深层使用价值。
 Declare
        Type myrecord Is Record(
             r_recordnumber Number(4),
             r_currentdate Date
             );
        v_myrecord myrecord;
        Begin
             Select * Into v_myrecord From testtable Where recordnumber=80;
             dbms_output.put_line('用记录类型的变量取出来的值为:'||
                                  v_myrecord.r_recordnumber||
                                  v_myrecord.r_currentdate);
        End;
        */

/*
Begin
     For j In 1..10.5 Loop        --有意思的pl/sql自定义循环变量(它按四舍五入来取值)
         dbms_output.put_line(j);
     End Loop;     
End ; 
      */

/*
简单的%Rowtype定义变量的实例    
Declare
        v_myrow testtable%Rowtype;
        Begin
             Select * Into v_myrow From testtable Where recordnumber=90;
             dbms_output.put_line('用rowtype查询的结果是:'||v_myrow.recordnumber||v_myrow.currentdate);
             --dbms_output.put_line('用rowtype查询的结果是:'||v_myrow.recordnumber||v_myrow.currentdate||myrow.Rowid);
             --上句操作说明了行类型的变量中不会把oracle表中的rowid的值带进来
        End;
*/

/*

--定义一维表变量的实例,这种变量看起来有些想编程语言中的一维数组
Declare
       Type mytbtype1 Is Table Of Varchar2(4) Index By Binary_Integer;
       Type mytbtype2 Is Table Of testtable.recordnumber%Type Index By Binary_Integer;
       tb1 mytbtype1;
       tb2 mytbtype2;
       Begin
            tb1(1):='大学';
            tb1(2):='大专';
            tb2(1):=90;
            tb2(2):=70;
            dbms_output.put_line(tb1(1)||tb2(1));
            dbms_output.put_line(tb1(2)||tb2(2));
       End;


*/

/*
--定义一个多维表变量,这就像一个二维数组
--当然这个二维的数组的下表就有些区别与我们在编程语言中熟悉的二维数组了
--可以理解为一维存储的列名,而另一维则是存储与一维列名相对应的数据
Declare
       --这里区别与一维表变量的定义
       Type multbtype Is Table Of testtable%Rowtype Index By Binary_Integer;
       multb multbtype;
       Begin
            Select * Into multb(12)
            From testtable
            Where recordnumber=88;
            dbms_output.put_line('multb(12).recordnumber='||
                                 multb(12).recordnumber||
                                 ' multb(12).currentedate'||
                                 multb(12).currentdate
                                 );
       End;

*/

/*

--这里体现了oracle中的‘数组’与其他编程语言中的数组的区别,
--明显的区别就是它这么做就有些类似与数据结构中的列表,但还不能完全称之为列表,
--呵呵,于是我认为它就是我们数据结构中的'半个列表'
Declare
       Type mytabletype Is Table Of Varchar2(9) Index By Binary_Integer;
       tb mytabletype;
       Begin
            tb(1):='成都市';
            tb(2):='太原市';
            tb(3):='北京市';
            dbms_output.put_line('记录总数:'||to_char(tb.Count));
            dbms_output.put_line('第一条记录为:'||tb.First||'其值为:'||tb(tb.First));
            dbms_output.put_line('最后条记录为:'||tb.Last||'其值为:'||tb(tb.Last));
            dbms_output.put_line('第二条的前一条记录为:'||tb.Prior(2)||'其值为:'||tb(tb.Prior(2)));
            dbms_output.put_line('第二条的后一条记录为:'||tb.Next(2)||'其值为:'||tb(tb.Next(2)));
            dbms_output.put_line('第二条记录为:'||tb(2));
            tb.Delete(2);
            dbms_output.put_line('删除第二条记录后的第二条记录为:'||tb(3));
            --exists存在的问题,不知到怎么使用
            --tb.Exists('太原市');
           
       End;

*/

/*
Declare
       --主要熟悉pl/sql中的各种数据类型
        myinteger Integer:=80;
        mynumber Number(2):=90;
        myint Int:=98;
        myfloat Float:=90.9;
        Begin
             If myinteger>mynumber Then
                dbms_output.put_line('myinteger大于mynumber!');
             Else
                dbms_output.put_line('mynumber大于或等于myinteger!');
                     
                     
                      If myint>mynumber Then
                         dbms_output.put_line('myint大于mynumber!');
                              If myfloat<myint Then
                                 dbms_output.put_line('myfloat小于myint!');
                              End If;
                      Else
                          dbms_output.put_line('mynumber大于myint!');
                      End If;
                     
                     
             End If;
            
        End;

       
*/

/*
--这种写法是为了简化多个选择用if来做判断,这种case区别与编程语言中的case语句。
--不过在写多种情况的判断时还是蛮不错的。
Declare
        v_test Int:=60;       
        Begin
             Case
             When v_test=90 Then
             dbms_output.put_line('v_test的值为:90');
             When v_test=80 Then
             dbms_output.put_line('v_test的值为:80');
             Else
             dbms_output.put_line('v_test的值我不知道!');
             End Case;
        End;
       
       
*/
/* 

--真正常用的case语句,不过它的灵活性远远的不如vb中的好,但比c语言中的switch好多了
--它会自动跳出,不需要你再去break了。     
Declare
        v_test Int:=60;       
        Begin
             Case v_test
             When 90 Then
             dbms_output.put_line('v_test的值为:90!');
             When 80 Then
             dbms_output.put_line('v_test的值为:80!');
             Else
             dbms_output.put_line('v_test的值我不知道!');
             End Case;
        End;
 */ 

/*

--看看回滚的效果两个rollback回滚的位置不一样。
--可自行注释一下两个rollback来查看一下表内容,就会感觉到savepoint的魅力所在。
Insert Into testtable Values(105,Sysdate);
Savepoint testpoint;
Rollback To testpoint;
Rollback;
 */
 
/*
--这里我测试了下在oracle中没有continue和break语句,
--只有exit 和goto语句
--如果着实想用continue语句的话,我不介意你使用一下这个方法。^_^
 Declare
         i Int:=0;
         Begin
           While i<100 Loop
           <<labale>>
                 If i=50 Then
                    dbms_output.put_line('已经到了50');
                    i:=80;
                    Goto labale;
                    --Continue;
                    --break
                    --Exit;
                 Else
                    i:=i+1;
                 End If;
                 dbms_output.put_line('现在i的值为:'||i);
           End Loop;
         End;
 */

/*
--游标的功能展示,这里展示了%isopen,%found,%
Declare
       tempsal scott.emp.sal%Type;
       Cursor mycursor Is
              Select * From scott.emp e
              Where e.sal=tempsal;
       currentrecord mycursor%Rowtype;
       Begin
            tempsal:=3000;
            tempsal:=800;
            --打开游标
            If mycursor%Isopen Then
               dbms_output.put_line('该游标已经打开了,正在关闭!');
               Close mycursor;
               Open mycursor;
            Else
                 dbms_output.put_line('游标关闭,正在打开!');
               Open mycursor;
            End If;
            --读取数据
            Fetch mycursor Into currentrecord;
            If mycursor%Found Then
                dbms_output.put_line('游标已经取到数据,查询结果是:');
                dbms_output.put_line(to_char(currentrecord.ename));
            Else
                dbms_output.put_line('没有要查询的结果!');
            End If;
            --读取记录总条数
            Loop
                Fetch mycursor Into currentrecord;           
            Exit When mycursor%Notfound;
            End Loop;
            dbms_output.put_line('查结果总共有:'||mycursor%Rowcount);
            --关闭游标
            If mycursor%Isopen Then 
             dbms_output.put_line('正在关闭游标,程序正常结束!');                                                            
            Close mycursor;
            End If;
       End;

*/


/*
比较小的一个带参数的存储过程
Create Or Replace Procedure myprocedure(
       tempdeptno In scott.dept.deptno%Type,
       tempdname Out scott.dept.dname%Type,
       temploc In Out scott.dept.loc%Type) As
       loc1 scott.dept.loc%Type;
       dname1 scott.dept.dname%Type;
       Begin
            Select loc Into loc1
            From scott.dept t
            Where t.deptno=tempdeptno;
            dbms_output.put_line('地址为:'||loc1);
            Select dname Into dname1
            From scott.dept t
            Where t.deptno=tempdeptno;
            dbms_output.put_line('部门名称为:'||dname1);
            tempdname:=dname1;
            temploc:=loc1;
       End;

*/

/*

--这里集成了数据的操作,对上例及进行改进,减少了字段
--
Create Or Replace Procedure myprocedure2(
       tempdeptno In scott.dept.deptno%Type,
       tempdname Out scott.dept.dname%Type,
       temploc In Out scott.dept.loc%Type) As
      
       myrecord scott.dept%Rowtype;
       mysum number;
       zero Exception;
       Begin
      
            dbms_output.put_line('存储过程开始运行!');
           --看是否为空
            Select Count(*) Into mysum
            From scott.dept
            Where deptno=tempdeptno;
            If mysum=0 Then
              Raise zero;
            End If;
            --取值
            Select * Into myrecord
            From scott.dept t
            Where t.deptno=tempdeptno;
            dbms_output.put_line('地址为:'||myrecord.loc);
            dbms_output.put_line('部门名称为:'||myrecord.dname);
            dbms_output.put_line('存储过程正在运行!');
            tempdname:=myrecord.dname;
            temploc:=myrecord.loc;
           
            dbms_output.put_line('存储过程正常结束!');
           
            Exception
            When zero Then
            dbms_output.put_line('没有查询结果!');
            When Others Then
            dbms_output.put_line('程序出现了异常!'||Sqlcode||Sqlerrm);
           
       End;


*/


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值