Oracle 方法

一、遞歸web

 select zxdept
   from (select d.id, d.zxdept, d.RANK, d.fatherId
           from web_dept d
          start with d.id = 18022 --獲取18022部門層級
                 and d.SHOWFLAG = 1
         connect by PRIOR fatherId = id) temp
  where temp.rank <= 4
  order by rank asc

結果:sql

1 分析及投後管理
2 財務部
3 財務部
4 管理中心數據庫

二、列拼接成行 wm_concat()服務器

 select replace(wm_concat(zxdept), ',', '/') as 部門名稱
   from (select zxdept
           from (select d.id, d.zxdept, d.RANK, d.fatherId
                   from web_dept d
                  start with d.id = 18022--獲取18022部門層級
                         and d.SHOWFLAG = 1--可用
                 connect by PRIOR fatherId = id) temp
          where temp.rank <= 4--四級之內
            and temp.rank >= 1--大於等於一級
          order by rank asc)

結果:管理中心/財務部/財務部/分析及投後管理oracle

三、獲取最新分組內全部一條記錄ide

select *
  from ( --以 orderno 分組,查找分組內最新一條工做流日誌
        select t.*,
                row_number() over(partition by orderno order by FORTIME desc) rn
          from (select *
                   from WORKFLOW_INSTANCE_LOG
                  where orderno in (select to_char(ID) as id
                                      from HR_DIMISSION
                                     where USERCODE = 'B143130'
                                       and (FLAGSAVE = 2 or FLAGSAVE = 9) -- 離職單
                                    union all
                                    select to_char(ID) as id
                                      from HR_ConFirm
                                     where USERCODE = 'B143130'
                                       and FLAGSAVE = 2 -- 轉正單
                                    union all
                                    select to_char(ID) as id
                                      from HR_Transposal
                                     where USERCODE = 'B143130'
                                       and FLAGSAVE = 2 -- 調崗單
                                    )) t)
 where rn = 1

 四、insert into select union all ORA-02287post

insert into OA_VOTE_QUESTIONNAIRE
  (ID, TYPE, FATHERID, SERIALNUMBER, ITEMTITLE, QUESTIONTYPE)
  select OA_VOTE_QuestionnaireID.Nextval,
         2,
         target.FATHERID,
         target.SERIALNUMBER,
         target.ITEMTITLE,
         target.QUESTIONTYPE
    from (select 0 FATHERID,
                 1 SERIALNUMBER,
                 '您離職的主要緣由是:' ITEMTITLE,
                 4 QUESTIONTYPE
            from dual --第一大題
          union all
          select (select max(temp.id)
                    from OA_VOTE_QUESTIONNAIRE temp
                   where temp.type = 2
                     and temp.fatherid = 0) FATHERID,
                 1 SERIALNUMBER,
                 '薪酬福利較低' ITEMTITLE,
                 4 QUESTIONTYPE
            from dual) target
View Code

 五、轉換成NVARCHAR2 類型:測試

Translate('ssdsdsd' USING NCHAR_CS);ui

六、insert when EXISTS then into:this

insert when EXISTS
  (select *
     from view_Rep_holiday
    where id = 'QJ151012_003'
      and holidaytype = 13) then into timecard
  (cardno, username, thisdate, entryexit, clientid)
  select '0', username, dateend, '1', '19'
    from view_Rep_holiday
   where id = 'QJ151012_003'
  union all
  select '0', username, datestart, '1', '19'
    from view_Rep_holiday
   where id = 'QJ151012_003';

 七、oracle ORA-00911: 無效字符

     語句放在plsql中執行沒問題 ,可是程序始終報錯,解決辦法:去掉sql結尾的分號(;)。

 八、得到時間差ROUND(TO_NUMBER(END_DATE - START_DATE)) --默認相減的結果以天爲單位,若是須要精確到小時、分、秒,則計算下便可;

     eg:ROUND(TO_NUMBER(END_DATE - START_DATE)*24*60)

 九、ORacle 遷移:

  (1)安裝 ORAcle server版本,安裝完成,登陸http://172.0.0.1:1158/em ,添加表空間(表空間和老數據庫相同,避免沒必要要的麻煩),添加用戶,併爲用戶賦予角色(conect,DBA,resource)和權限(這個根據需求,太           多就不一一列,建議先所有添加,而後移除);

     (2)導入數據庫中的隊列、再導入備份。

  導入備份若是有報錯則繼續下面步驟:

  (3)依次導入導入類型、導入trigger,導入view,導入fun和proc;

  ps:本人在實際中碰到字段若是是這個類型—>BINARY_FLOAT,系統會提示沒有導入成功(詳細能夠查看導入日誌),此時須要對先導入標結果真後根據下面方法插入數據。
    若是操做失敗,數據混亂,能夠把庫刪除重來,可是不須要卸載oracle,這樣會更快寫修復問題。

  第一步、建立dblink:    

create public database link Link_ltcs
                  connect to 用戶名 identified by 密碼
                  using '(DESCRIPTION =
                            (ADDRESS_LIST =
                              (ADDRESS = (PROTOCOL = TCP)(HOST = 服務器地址)(PORT = 1521))
                            )
                            (CONNECT_DATA =
                              (SERVICE_NAME = 數據庫實例名稱)
                            )
                          )';
View Code
eg:
create public database link Link_09
                  connect to ltg identified by longtugame123
                  using '(DESCRIPTION =
                            (ADDRESS_LIST =
                              (ADDRESS = (PROTOCOL = TCP)(HOST = 172.16.200.9)(PORT = 1521))
                            )
                            (CONNECT_DATA =
                              (SERVICE_NAME = OADB)
                            )
                          )';

 

 

 第二步、導數據:

 insert into 本地表 select * from 遠端數據庫表@link_ltcs.regress.rdbms.dev.us.oracle.com 

十、類型轉換致使查處數據被截斷

  PAYEEACCOUNT NVARCHAR2(100) 

  原始值是 :測試人

  通過 cast(PAYEEACCOUNT as NVARCHAR2(100) )的結果爲:測

十一、oracle表空間使用狀況查詢(轉載的)

select a.tablespace_name, total, free, total-free as used, substr(free/total * 100, 1, 5) as "FREE%", substr((total - free)/total * 100, 1, 5) as "USED%" from 
(select tablespace_name, sum(bytes)/1024/1024 as total from dba_data_files group by tablespace_name) a, 
(select tablespace_name, sum(bytes)/1024/1024 as free from dba_free_space group by tablespace_name) b
where a.tablespace_name = b.tablespace_name
order by a.tablespace_name;

 十二、oracle10g 按期導出數據

 一、

/*設置導出路徑並賦予權限*/
create or replace directory exp as 'D:\OA\DataBaseBack';
grant write on directory exp to public;
grant read on directory exp to public;

二、建立 文件名.bat

@echo off 
Set OrclSid=數據庫
Set ExpUser=登陸名
Set ExpPass=密碼
Set SysDate=%date:~0,4%-%date:~5,2%-%date:~8,2%.%time:~0,2%%time:~3,2%

@echo * * * * * * * * * * * * * * * * * * * * 
@echo * * * * * * * * * * * * * * * * * * * * 
@echo * * * * * ORACLE 數據庫導出 * * * * * * 
@echo * * * * * * * * * * * * * * * * * * * * 
@echo * * * * * * * * * * * * * * * * * * * *

@echo 服務名 = %OrclSid% 
@echo 用戶名 = %ExpUser% 
@echo 密 碼 = %ExpPass% 
@echo 時 間 = %SysDate%

expdp %ExpUser%/%ExpPass%@%OrclSid% directory=exp dumpfile=%ExpUser%~%OrclSid%_%SysDate%.dmp logfile=%ExpUser%~%OrclSid%_%SysDate%.log

/*倒入*/
IMPDP USERID='用戶/密碼@sid' schemas=倒入用戶 directory=exp table_exists_action=replace dumpfile=2017-07-24.DMP

三、添加系統系統計劃;

 1三、oracle 中使用order等關鍵字做爲列操做:

insert into Api_beisen_postInformation(OId,OIdTalentCriterion,Name,OIdResourceSet,StartDate,StopDate,\"Order\",OIdJobLevel,EstablishDate,Description,Score,JobPostKey,JobPostSecret,JobRequirements,Code,OIdJobGradeLow,OIdJobGradeHigh,OIdJobSequence,OIdProfessionalLine) values(:OId,:OIdTalentCriterion,:Name,:OIdResourceSet,:StartDate,:StopDate,:\"ORDER\",:OIdJobLevel,:EstablishDate,:Description,:Score,:JobPostKey,:JobPostSecret,:JobRequirements,:Code,:OIdJobGradeLow,:OIdJobGradeHigh,:OIdJobSequence,:OIdProfessionalLine)

注意:參數必須大寫::\"ORDER\"

1四、Function中拼接sql寫邏輯:

CREATE OR REPLACE FUNCTION FN_DEPTstaffcount (dept_ number , dq number) return number is stffs int;
 sql_str VARCHAR2(4000);
begin
  sql_str := 'select count(1) from web_admin wa join (select temp.id from web_dept temp where temp.showflag=1 start with temp.id='||dept_||' connect by prior temp.id=temp.fatherid) temp1
    on wa.zxdept=temp1.id and wa.isvirtual=0 join web_admin_work temp2 on wa.usercode=temp2.usercode where wa.isvirtual=0 and temp2.rank>0';
    if dq>0 then
       sql_str:=sql_str||' and exists(select 1 From web_admin_work waw where wa.usercode=waw.usercode and waw.adress=to_char('||dq||'))';
      end if;
     execute immediate sql_str into stffs ;
  return stffs;
  end;
相關文章
相關標籤/搜索