Oracle应用笔记

1、数字型的字符串在order by排序时会出现错乱,如对1、2、3……11,12进行排序,查询结果为1、10、11、12、2、3……9,解决的办法是,可以使用函数to_number()。

2、计算字符串[abca]中某个字符[a]出现的次数,可以使用regexp_count([abca],[a]),如果字符是[.],要加上转义,regexp_count(er.code,'\.')。

3、计算字符串[abca]中某个字符[c]出现的位置,可使用函数instr([abca],[c],[startIdx],[num]),startIdx是指定从第几个位置开始找,num指定查找第几次出现的,如果找不到,返回0。

4、截取字符串,使用函数substr([string],[startIdx],[length]),最后一个参数不填表示截取到最后。

5、decode([条件],[结果1],[返回1],[无结果返回]),结果和返回可多个。

6、树形查询,exp:
select decode(m.lev,1,'一级菜单',2,'二级菜单',3,'功能菜单') 层次,m.name 功能菜单,SYS_CONNECT_BY_PATH(m.name, '>') 路径 
from menu m where m.lev!=4 and m.enabled = 'USERED' 
start with m.pk_id=10 connect by prior m.pk_id=m.fk_parent

可使用level关键字获得数据的层级

7、执行not in null,会产生预料之外情况,查询不到数据,详细原因可参考:
http://www.cnblogs.com/killkill/archive/2010/09/04/1817266.html

8、with as :其实就是把一大堆重复用到的SQL语句放在with as 里面,取一个别名,后面的查询就可以用它。这样对于大批量的SQL语句起到一个优化的作用,而且清楚明了。

9、列转行函数 listagg()
结合with as举个例子:
with temp as(
  select 'China' nation ,'Guangzhou' city from dual union all
  select 'China' nation ,'Shanghai' city from dual union all
  select 'China' nation ,'Beijing' city from dual union all
  select 'USA' nation ,'New York' city from dual union all
  select 'USA' nation ,'Bostom' city from dual union all
  select 'Japan' nation ,'Tokyo' city from dual
)
select nation,listagg(city,',') within group(order by city)
from temp
group by nation
查询结果:
China Beijing,Guangzhou,Shanghai
Japan Tokyo
USA Bostom,New York

10、count加条件计算分组内的数据
count(1) 计算+1
count(null) 不计算
--以下例子源自其他博客:http://blog.csdn.net/yin_jw/article/details/38893607
--学生表
create table t_student
(
  id   number,
  name varchar2(100),
  sex  number
)
;
-- Add comments to the columns
comment on column t_student.id
  is 'id';
comment on column t_student.name
  is '姓名';
comment on column t_student.sex
  is '性别0:男 1:女';
 
--查询男、女生的数量
--count(1),算在总数内。
--count(null),不算在总数内。
select count(decode(sex, 0, 1, null)) boyNum,
       count(decode(sex, 1, 1, null)) girlNum
  from t_student

11、declare程序实例
declare
  stuid number(30) :=135181;
  oldId number(30) :=0;
  maxId number(30) :=0;
  canId number(30) :=0;
  flag number(30) :=1;
begin
     select max(et.id) into maxId from edu_thesis_info et
     where length(et.id)!=7 and et.enabled_flag=1
     and et.student_id=stuid
     and et.elective_id<(select t.elective_id from edu_thesis_info t where length(t.id)=7 and t.enabled_flag=1
     and t.student_id=stuid);
    if maxId is null then
       select min(et.id) into maxId from edu_thesis_info et
       where length(et.id)!=7 and et.enabled_flag=1
       and et.student_id=stuid
       and et.elective_id>(select t.elective_id from edu_thesis_info t where length(t.id)=7 and t.enabled_flag=1
       and t.student_id=stuid);
    end if;
    
    for i in REVERSE (maxId-1000)..maxId loop
        select count(1) into flag from edu_thesis_info et where et.id=i;
        if flag=0 then
           canId:=i;
           exit;
        end if;
    end loop;
    select et.id into oldId from edu_thesis_info et where et.student_id=stuid and length(et.id)=7 and et.enabled_flag=1;
    dbms_output.put_line('学生论文的最大id');
    dbms_output.put_line(maxId);
    dbms_output.put_line('可用id');
    dbms_output.put_line(canId);
    dbms_output.put_line('原论文id');
    dbms_output.put_line(oldId);
end;

12、导出所有的表信息:
select u.table_name 表名,utc.comments 表注释, u.column_name 字段名,a.DATA_TYPE 字段类型,u.comments 注释
from user_col_comments u,all_tab_columns a,user_tab_comments utc
where u.table_name=a.TABLE_NAME and a.OWNER='XMUEDU' and u.column_name=a.COLUMN_NAME and utc.table_name=a.TABLE_NAME
--and a.TABLE_NAME= 'BASIC_CODE'
order by u.table_name,a.DATA_TYPE,u.column_name;

13、获得根据特定属性排序的序号
ROW_NUMBER() OVER(order by es.examno asc)

14、VARCHAR2(20)和CHAR(20)区别:字符串长度小于20时,VARCHAR2(20)字段的长度为实际字符串的长度,CHAR(20)长度固定为20,不足20补空格。

15、
-- 获取序列下一个值
create or replace function get_seq_next (seq_name in varchar2) return number
is
  seq_val number ;
begin
  execute immediate 'select '|| seq_name|| '.nextval from dual' into seq_val ;
  return seq_val ;
end get_seq_next;

-- 获取序列当前值(无需先执行nextval也可使用)
create or replace function get_seq_curr (seq_name in varchar2) return number
is
  seq_val number ;
begin
  execute immediate 'select '|| seq_name|| '.currval from dual' into seq_val ;
  return seq_val ;
end get_seq_curr;

猜你喜欢

转载自zzzwp.iteye.com/blog/2265891