我自己记录的Oracle笔记

1.调试ORACLE 用 set serveroutput on ; DBMS_OUTPUT.PUT_LINE(val);
2.存储过程的参数中不能指定长度.且存储过程没有返回值.
3.    select  'select'    from  dual  ;    --选择出 select 这个值
    select "select" from dual;        --选择出列名为 select 这一列的值.
4.select * from all_errors where name='TEST1';        SELECT TEXT FROM ALL_ERRORS WHERE NAME='TESTYU';
5.怎样进行逻辑运算.
6.to_number('80000000','xxxxxxxx')
7.Oracle中所有的字符最好用大写.因为,所有的列名,表名,都是大写.
8. select val into myVal from yuy ;    --如果选出来的结果是空的,这样写会抛出一个错误!
    应该这样写.
    declare  :
    cou int;
    select count(*) into cou from yuy ;
    if(cou=0) then
        dbms_output.put_line('err');
        return;
    end if;

    select val into myVal from yuy ;
9. NULL 不能用 '' 来比较.如果要判断取出来的字符串是不是空,用  if (val is null) then ... end if ; 就可以了.
10. Oracle 中的位运算.
    Oracle 只提供 位与 的函数 bitand
    if rawtohex( utl_raw.bit_and(utl_raw.cast_from_binary_integer(15),utl_raw.cast_from_binary_integer(11))
                          ) = to_number(11,'xxxxxxxx')
                          then
    /*
    select count(*) into cou from yuy where val=val1 and rownum=1;
    if(cou=0 ) then
    dbms_output.put_line('oooo');
    return;
    end if;
   
    Val:='xxxxxxxx';
    */
    INSERT INTO YUY (ID,VAL) VALUES (10,'THIS');
    end if;
    ---------------------------------------------------------------------
    ops$tkyte@ORA8I.WORLD> select bitand(1,1)+0 from dual;
   
    BITAND(1,1)+0
    -------------
    1
   
    ops$tkyte@ORA8I.WORLD> c/,1/,2
    1* select bitand(1,2)+0 from dual
    ops$tkyte@ORA8I.WORLD> /
   
    BITAND(1,2)+0
    -------------
    0
     and given that bitor becomes:
   
    (x + y) - BitAND(x, y);
   
    and bitXor becomes:
   
    (x + y) - BitAND(x, y) * 2;
   
    (there are other ways to implement them as well I am sure)
   
   
    --------------------------------------------------------------------------------
    作者:biti_rainy    时间:03-04-18 10:10
    utl_raw 包
   
    SQL> desc sys.utl_raw
    FUNCTION BIT_AND RETURNS RAW
    Argument Name Type In/Out Default?
    ------------------------------ ----------------------- ------ --------
    R1 RAW IN
    R2 RAW IN
    FUNCTION BIT_COMPLEMENT RETURNS RAW
    Argument Name Type In/Out Default?
    ------------------------------ ----------------------- ------ --------
    R RAW IN
    FUNCTION BIT_OR RETURNS RAW
    Argument Name Type In/Out Default?
    ------------------------------ ----------------------- ------ --------
    R1 RAW IN
    R2 RAW IN
    FUNCTION BIT_XOR RETURNS RAW
    Argument Name Type In/Out Default?
    ------------------------------ ----------------------- ------ --------
    R1 RAW IN
    R2 RAW IN
11.    如何建立完整性约束(在表中的字段部分出现在另一个表中) 和 替换型触发器(在Befor Insert 中过滤掉不想要插入的数据,并记录在其它表中).
12. Oracle:PL/SQL 中如何使用Array
http://tech.163.com/school · 2005-07-01 10:36:35 · 来源: oracle论坛
  因为在PL/SQL 中并没有数组. 这是偶查资料找的范例和自己写的范例来解释如何在PL/SQL 中使用数组. 也许很多人已知道, 不过就是让不知道的朋友们了解一下吧。

---------------------- 单维数组------------------------

DECLARE
TYPE emp_ssn_array IS TABLE OF NUMBER
INDEX BY BINARY_INTEGER;

best_employees emp_ssn_array;
worst_employees emp_ssn_array;

BEGIN
best_employees(1) := '123456';
best_employees(2) := '888888';

worst_employees(1) := '222222';
worst_employees(2) := '666666';

FOR i IN 1..best_employees.count LOOP
DBMS_OUTPUT.PUT_LINE('i='|| i || ', best_employees= ' ||best_employees(i)
|| ', worst_employees= ' ||worst_employees(i));
END LOOP;

END;
 



---------------------- 多维数组------------------------

DECLARE

TYPE emp_type IS RECORD
( emp_id employee_table.emp_id%TYPE,
emp_name employee_table.emp_name%TYPE,
emp_gender employee_table.emp_gender%TYPE );

TYPE emp_type_array IS TABLE OF
emp_type INDEX BY BINARY_INTEGER;

emp_rec_array emp_type_array;
emp_rec emp_type;

BEGIN
emp_rec.emp_id := 300000000;
emp_rec.emp_name := 'Barbara';
emp_rec.emp_gender := 'Female';

emp_rec_array(1) := emp_rec;

emp_rec.emp_id := 300000008;
emp_rec.emp_name := 'Rick';
emp_rec.emp_gender := 'Male';

emp_rec_array(2) := emp_rec;

FOR i IN 1..emp_rec_array.count LOOP
DBMS_OUTPUT.PUT_LINE('i='||i
||', emp_id ='||emp_rec_array(i).emp_id
||', emp_name ='||emp_rec_array(i).emp_name
||', emp_gender = '||emp_rec_array(i).emp_gender);
END LOOP;

END;
-------------- Result --------------
i=1, emp_id =300000000, emp_name =Barbara, emp_gender = Female
i=2, emp_id =300000008, emp_name =Rick, emp_gender = Male
 



注:在PL/SQL 中是没有数组(Array) 概念的. 但是如果程序员想用Array 的话, 就得变通一下, 用TYPE 和Table of Record 来代替多维数组, 一样挺好用的。
emp_type 就好象一个table 中的一条record 一样, 里面有id, name,gender等。emp_type_array 象个table, 里面含有一条条这样的record (emp_type),就象多维数组一样。

posted @ 2009-03-09 00:58  NewSea  阅读(268)  评论(0)    收藏  举报