Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Oracle Dinamis Pivot_In_Clause

Anda dapat membuat kueri dinamis di skrip Anda, lihat contoh ini:

variable rr refcursor

declare 
  bb varchar2(4000);
  cc varchar2( 30000 );
begin 
    WITH PIVOT_DATA AS (
         SELECT *
         FROM
         (
             SELECT USERNAME, GRANTED_ROLE
             FROM DBA_USERS U LEFT OUTER JOIN DBA_ROLE_PRIVS R
             ON U.USERNAME = R.GRANTEE
          )
    )
    select ''''|| listagg( granted_role, ''',''' ) 
            within group( order by granted_role ) || '''' as x 
    into bb
    from (
      select distinct granted_role from pivot_data
    )
    ;

    cc := q'[
    WITH PIVOT_DATA AS (
         SELECT *
         FROM
         (
             SELECT USERNAME, GRANTED_ROLE
             FROM DBA_USERS U LEFT OUTER JOIN DBA_ROLE_PRIVS R
             ON U.USERNAME = R.GRANTEE
          )
    )
    SELECT *
    FROM PIVOT_DATA
    PIVOT
    (
        COUNT(GRANTED_ROLE)
        FOR GRANTED_ROLE
        IN(]'  || bb || q'[) -- Just an example
    )
    ORDER BY USERNAME ASC]';

    open :rr for cc;
end;
/

SET PAGESIZE 200
SET LINESIZE 16000
print :rr

Inilah hasilnya (hanya potongan kecil, karena sangat lebar dan panjang)

-----------------------------------------------------------------------------------------------------------------------------------
    USERNAME                       'ADM_PARALLEL_EXECUTE_TASK' 'APEX_ADMINISTRATOR_ROLE' 'AQ_ADMINISTRATOR_ROLE' 'AQ_USER_ROLE'        
    ------------------------------ --------------------------- ------------------------- ----------------------- ----------------------
    ANONYMOUS                      0                           0                         0                       0          
    APEX_030200                    0                           0                         0                       0        
    APEX_PUBLIC_USER               0                           0                         0                       0    
    APPQOSSYS                      0                           0                         0                       0   
..............
    IX                             0                           0                         1                       1  
    OWBSYS                         0                           0                         1                       1      



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Cara mengirim email menggunakan Oracle 10 g Forms

  2. Nilai Urutan Oracle tidak dipesan

  3. Muat lembar data Excel ke database Oracle

  4. Batasan unik Oracle dan indeks unik

  5. kolom tidak diizinkan di sini kesalahan dalam pernyataan INSERT