2個sql 函數dbms_lob.substr 和 wm_concat


轉自:

http://blog.csdn.net/wenzhongyan/article/details/50315473

http://blog.csdn.net/ojerryzuo/article/details/53927057

 

1、通過dbms_lob.substr()轉換clob字段為varchar2類型

在查詢dba_stat_extensions視圖的時候,其中extension字段是clob類型,直接通過select語句無法顯示,如下:

需要通過dbms_lob.substr()轉換

SELECT owner ,a.table_name,trim(dbms_lob.substr(extension,4000)) as extension  FROM dba_stat_extensions a 轉換后顯示如下:

 

 

2.

首先讓我們來看看這個神奇的函數wm_concat(列名),該函數可以把列值以","號分隔起來,並顯示成一行,接下來上例子,看看這個神奇的函數如何應用
准備測試數據
SQL> create table test(id number,name varchar2(20));
SQL> insert into test values(1,'a');
SQL> insert into test values(1,'b');
SQL> insert into test values(1,'c');
SQL> insert into test values(2,'d');
SQL> insert into test values(2,'e');
SQL> commit;
效果1 : 行轉列
SQL> select wm_concat(name) from test;
WM_CONCAT(NAME)
-------------------------------------------------------------------------
a,b,c,d,e
效果2: 把結果里的逗號替換成"|"
SQL> select replace(wm_concat(name),',','|') from test;
REPLACE(WM_CONCAT(NAME),',','|')
-----------------------------------------------------------------------
a|b|c|d|e
效果3:按ID分組合並name
SQL> select id,wm_concat(name) name from test group by id;
ID NAME
---------- ------------------------------
1 a,b,c
2 d,e
懶人擴展用法:
案例:我要寫一個視圖,類似"create or replace view as select 字段1,...字段50 from tablename" ,基表有50多個字段,要是靠手工寫太麻煩了,有沒有什么簡便的方法? 當然有了,看我如果應用wm_concat來讓這個需求變簡單
SQL> select 'create or replace view as select '|| wm_concat(column_name) || ' from dept'from user_tab_columns where table_name='DEPT';
'CREATEORREPLACEVIEWASSELECT'||WM_CONCAT(COLUMN_NAME)||'FROMDEPT'
--------------------------------------------------------------------------------
create or replace view as select DEPTNO,DNAME,LOC from dept
 
 
issue 1:ORA-0103


######1
SELECT TRIM(DBMS_LOB.SUBSTR(WM_CONCAT(ABC))) DATAVAL
*
ERROR at line 63:
ORA-01031: insufficient privileges


###########2
select wm_concat('aaa') from dual;

*
ERROR at line 1:
ORA-01031: insufficient privileges


###### fix  3:

grant execute on wm_concat to Dbmonopr;

 

 #########re-create function 

           scp owmctab.plb owmaggrs.plb owmaggrb.plb admin@25.16.1.1:/tmp/dba

  1. SQL>@$ORACLE_HOME\RDBMS\ADMIN\owmctab.plb;  
  2. SQL>@$ORACLE_HOME\RDBMS\ADMIN\owmaggrs.plb  
  3. SQL>@$ORACLE_HOME\RDBMS\ADMIN\owmaggrb.plb 

####

WMSYS Packages/Functions missing on 12c (文檔 ID 2368194.1)

There are WMSYS functionalities that are gone in 12c, while available in 11g.

Please find the list of missing ones below, they all belong to WMSYS Owner

select * from SYS.DBA_TAB_PRIVS WHERE PRIVILEGE = 'EXECUTE' and OWNER = 'WMSYS';
WM_CONCAT_IMPL
WM_CONCAT
WM$GETDBVERSIONSTR
WM$DISALLOWQNDML
WM$CONVERTDBVERSION
OWM_9IP_PKG

Is there any change, new function or substitute for using this on Oracle 12c with WMSYS?
 

SOLUTION

All of those packages/functions are meant for internal use only and should not be used by customers. This is expected behavior for 12g. Some of them were dropped, others were moved into a package. 

WM_CONCAT : Use listagg.
WM$GETDBVERSIONSTR : Use the v$version view or the dbms_utility.db_version procedure.
WM$DISALLOWQNDML : All this does is unconditionally raise an error. Create a procedure that raises a similar error.
WM$CONVERTDBVERSION : No alternative. Would need to write their own procedure to do the version substitution.
OWM_9IP_PKG : They should not be directly altering the session context used by Workspace Manager.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM