Orale 基础总结知识点

---Oracle 服务名登陆
SQL> connect sys/[email protected]/ORCL as sysdba
已连接。
SQL> shutdown immediate;
----本月首日
Select to_char(last_day(add_months(sysdate,-1))+1,'YYYY-MM-DD')||' 00:00:01' From dual


C:/Users/Administrator/AppData/Local/Microsoft/Outlook/Outlook.pst
邮箱位置
----删除数据文件
alter database datafile '/home/oracle/dwbx_wmstat.dbf' offline drop
--数据库链接
Select  A.Rowid ,A.* From 个人基本信息 A
CREATE DATABASE LINK "Server" CONNECT TO zlchs IDENTIFIED BY zlchs USING '179';


Select 姓名 From 个人信息@Server
--判断时间在 开始时间结束时间月份之内【CZB问题处理】
Select Case When Substr(To_char( to_date('2010-09-20','YYYY-MM-DD'),'YYYYMMDD'),1,6)>=Substr(To_char( to_date('2010-01-01','YYYY-MM-DD'),'YYYYMMDD'),1,6)
                    And Substr(To_char( to_date('2010-09-20','YYYY-MM-DD'),'YYYYMMDD'),1,6)<=Substr(To_char( to_date('2010-01-15','YYYY-MM-DD'),'YYYYMMDD'),1,6) Then
                    '1'
                    Else
                      '0'
          End         
From dual;
------------HB问题处理
select
           rownum As  序号, column_value As 过敏记录 From
  table(f_split_string(Replace('注射用青霉素钠'||Chr(10)||'氨苄西林'||Chr(10)||'注射用头孢拉定',Chr(10),'|'), '|'))
------------字符串 循环解析方法2
  strtemp := 指定科室_in;
      while strtemp is not null loop
        intpos := instr(strtemp, ',');
     
        if intpos = 0 then
          strtemp := '';
        else
          --得到工作性质
          lng科室id := substr(strtemp, 1, intpos - 1);
          strtemp   := substr(strtemp, intpos + 1);
       
          insert into 收费策略科室 (策略id, 科室id) values (策略_in, lng科室id);
        end if;
      end loop;
---------------------------本表序号重整 例如 1 2 3 4 删除1 后 234变为123
    --本表的ROWNUM更新序号(WSF 2010-06-30)START
    update 诊疗频率时间 w
    set w.方案序号 =
        (select rn
         from (select rownum rn, t.* from (select * from 诊疗频率时间 where 执行频率 = 执行频率_in) t) s
         where w.方案序号 = s.方案序号 and w.执行频率 = s.执行频率)
    where w.执行频率 = 执行频率_in;
    --本表的ROWNUM更新序号(王圣峰 2010-06-30)END
--------------------------
⑤:DBMS_LOB.Compare():比较二个大对象是否相等。返回数值0为相等,-1为不相等。
     DBMS_LOB.Compare(LOB数据,LOB数据);
     例子:
     Declare
        varC1 clob;
      varC2 clob;
      varC3 clob;
      ln number(4);
     Begin
      select resume into varC1 from tlob where no = 1;
      select resume into varC2 from tlob where no = 2;
      select resume into varC3 from tlob where no = 3;
      ln := DBMS_LOB.Compare(varC1,varC1);
      DBMS_output.put_line('比较的结果为: '||ln);
      ln := DBMS_LOB.Compare(varC2,varC3);
      DBMS_output.put_line('比较的结果为: '||ln);
     End;
--------------------------------------minus intersect
 ( Select 1 From dual
  Union All
  Select 2 From dual
  Union All
  Select 3 From dual
  Union All
  Select 4 From dual
  )
               minus
              --Intersect
(  Select 4 From dual
  Union All
  Select 3 From dual
  Union All
  Select 6 From dual
  Union All
  Select 7 From dual)
-------------
加一秒:
SELECT to_date('2007-05-29 10:47:12','yyyy-mm-dd HH24:MI:SS')+1/24/60/60 FROM DUAL;

减一秒:
SELECT to_date('2007-05-29 10:47:12','yyyy-mm-dd HH24:MI:SS')-1/24/60/60 FROM DUAL;
-------------解析多行数据
Select XMLQuery('for $i in /ROWSET/ROW return data($i/规则名)' Passing 选用规则 Returning Content).getStringval() as t
From 仪器质控均值
------------比较好的处理XML的方式
  Select Sys_Xmlagg(Xmlelement("ROW", Xmlforest(x.序号, x.Id As "规则ID", x.名称 As "规则名")))
  Into Xml_规则
  From (Select Rownum As 序号, j.Id, j.名称 From Table(f_Split_String(选用规则_In)) t, 质控基本规则 j Where t.Column_Value = j.Id) x;

  Update 仪器质控均值 Set 选用规则 = Xml_规则 Where 质控品id = 质控品id_In And 期间 = 期间_In;
--------------------------------------比较两个表
-------比较两个数据表 名称 V1.0
(
Select A.COLUMN_NAME||A. From user_tab_cols@BHServer A  Where A.table_name='个人信息'
Minus
Select  B.COLUMN_NAME From user_tab_cols B Where B.table_name='个人基本信息'
)
Union All
(
Select  B.COLUMN_NAME From user_tab_cols B Where B.table_name='个人基本信息'
Minus
Select A.COLUMN_NAME From user_tab_cols@BHServer  A  Where A.table_name='个人信息'
 )
-------比较两个数据表 名称 类型 长度 V2.0
(
Select A.COLUMN_NAME||'【'||A.DATA_TYPE||'】'||'【'||A.DATA_LENGTH||'】' From user_tab_cols@BHServer A  Where A.table_name='个人信息'
Minus
Select  B.COLUMN_NAME||'【'||B.DATA_TYPE||'】'||'【'||B.DATA_LENGTH||'】' From user_tab_cols B Where B.table_name='个人基本信息'
)
Union All
(
Select  B.COLUMN_NAME||'【'||B.DATA_TYPE||'】'||'【'||B.DATA_LENGTH||'】' From user_tab_cols B Where B.table_name='个人基本信息'
Minus
Select A.COLUMN_NAME||'【'||A.DATA_TYPE||'】'||'【'||A.DATA_LENGTH||'】' From user_tab_cols@BHServer A  Where A.table_name='个人信息'
 )
-------数据分批插入
declare
 n_count integer := 0;
 cursor cur_1 is select * from 源表;
begin
 for icur_1 in cur_1 loop
 insert into 目标表 values (icur_1.COL1,icur_1.COL2, ……icur_1.COLN);
    n_count := n_count + 1;
    if n_count >= 5000 then
      commit;
      n_count := 0;
    end if;
  end loop;
commit;
exception
when others then
rollback;
end;
/
-----------------时间参数
alter system set nls_date_format="YYYY-MM-DD HH24:MI:SS" scope=spfile;

SQL> show parameter nls_date_format;
 
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
nls_date_format                      string      yyyy-mm-dd hh24:mi:ss
 
------------时间相差 [年月]
select floor(ceil(months_between(sysdate,to_date('2009-12-01','yyyy-mm-dd')))/12)  from dual;                --年

select mod(ceil(months_between(sysdate,to_date('2009-12-27','yyyy-mm-dd'))),12)  from dual;                 --月
------------查询某个节点的个数[在XML里面]
select
xmlquery('for
                    $i  in /ROWSET
                         return count(data($i/ROW))' passing 选用规则 returning Content).getstringval() as 个数
from 仪器质控均值
-------------查询语句XML
select extractvalue(内容信息, '/DOC/sdElement[@CName="舒张压"]') 结果值, 创建时间
from 个人文档记录
where existsnode(内容信息, '/DOC/sdElement[@CName="舒张压"]') = 1 and rownum <= 1
order by 创建时间 desc
---
XML格式
<DOC><sdElement id="f5380dec-3892-45e8-8bc8-e8b37b4b693f" ElementID="f13349a3-78a7-4587-9733-c4a08be4b137" CName="姓名" FieldType="1" BizType="1" Value="">商乐欢</sdElement>
</DOC>
----------------------深化解析本表的所有记录XML   [若是有舒张压 则进行解析,把element的 数值拔出来 ]为空的不处理
Select  M.舒张压,M.创建时间 From
(
Select
       Xmlquery('for $i in /DOC
          return
                         data($i/sdElement[@CName="舒张压"])'  Passing 内容信息 Returning Content).getstringval()  舒张压,A.创建时间
From 个人文档记录 A
Where existsnode(A.内容信息, '/DOC/sdElement[@CName="舒张压"]') = 1
) M
Where Trim(M.舒张压) Is Not Null
-------------数据库最大编码
Select  M.居委会编码 From
(
select b.编码 || lpad(nvl(to_number(substr(trim(to_char(max(a.编码) + 1, '000000')), 4, 3)), 1), 3, '0') as 居委会编码
from 居委会 a, 居委会 b
where (a.性质 is null or a.性质 = 2) and b.id = '177CDFD0-B574-4018-9422-6872587DD4DF' and b.id = a.上级id(+)
group by a.编码, b.编码
order by b.编码 || lpad(nvl(to_number(substr(trim(to_char(max(a.编码) + 1, '000000')), 4, 3)), 1), 3, '0') desc
)
M
Where   rownum<=1
----------新特性行列转换
select 分类, 值
from (
       select decode(z.标记, 0, '在控', 1, '失控', 2, '警告') as 标记,
                  b.规则,
                  nvl(b.提示, '') as 提示,
                  b.原因,
                  b.措施,
                  NVL2(b.结论,b.结论,'') as 结论
       from 仪器质控报告 b, 仪器质控记录 z
       where b.记录id = z.记录id
         ) unpivot(值 for 分类 in(标记, 规则, 提示, 原因, 措施, 结论)
       )
------------截取字符串的方法1
  strtemp := 指定科室_in;
    while strtemp is not null loop
      intpos := instr(strtemp, ',');
   
      if intpos = 0 then
        strtemp := '';
      else
        --得到工作性质
        lng科室id := substr(strtemp, 1, intpos - 1);
        strtemp   := substr(strtemp, intpos + 1);
     
        insert into 收费策略科室 (策略id, 科室id) values (策略_in, lng科室id);
------------截取字符串的方法2
    select count(column_value) into n_count from table(f_split_string(结算场合str_in, '|'));
    for n_index in 1 .. n_count loop
      select a.column_value
      into v_结算场合
      from (select rownum no, column_value from table(f_split_string(结算场合str_in, '|'))) a
      where a.no = n_index;
      insert into 结算方式应用 values (v_结算场合, 名称_in);
    end loop;
-------------------sum(case when)
*更改表空间*/
alter table table_name move tablespace tablespace_name

/*重建表索引*/
alter index index_name rebuild
-----------目前动态行转列有三种方法:
1.自定义func
2.sys_connect_by_path
3.wm_sys.wm_concat
 select id,
        ltrim(max(sys_connect_by_path(yf, ',')), ',') a
   from (select id,
                yf,
                yf d,
                min(yf) over(partition by id) d_min,
                (row_number() over(order by id, yf)) +
                (dense_rank() over(order by id)) numid
           from tab)
  start with d = d_min
 connect by numid = prior numid + 1
        and id = PRIOR id
  group by id;

你可能感兴趣的:(Date,String,user,table,insert,returning)