• <bdo id='GXpI9'></bdo><ul id='GXpI9'></ul>

    <i id='GXpI9'><tr id='GXpI9'><dt id='GXpI9'><q id='GXpI9'><span id='GXpI9'><b id='GXpI9'><form id='GXpI9'><ins id='GXpI9'></ins><ul id='GXpI9'></ul><sub id='GXpI9'></sub></form><legend id='GXpI9'></legend><bdo id='GXpI9'><pre id='GXpI9'><center id='GXpI9'></center></pre></bdo></b><th id='GXpI9'></th></span></q></dt></tr></i><div id='GXpI9'><tfoot id='GXpI9'></tfoot><dl id='GXpI9'><fieldset id='GXpI9'></fieldset></dl></div>

    <small id='GXpI9'></small><noframes id='GXpI9'>

      <legend id='GXpI9'><style id='GXpI9'><dir id='GXpI9'><q id='GXpI9'></q></dir></style></legend>

      1. <tfoot id='GXpI9'></tfoot>

        动态 Oracle Pivot_In_Clause

        时间:2023-09-19

              <tbody id='qIOQt'></tbody>

            <small id='qIOQt'></small><noframes id='qIOQt'>

                  <bdo id='qIOQt'></bdo><ul id='qIOQt'></ul>
                  <i id='qIOQt'><tr id='qIOQt'><dt id='qIOQt'><q id='qIOQt'><span id='qIOQt'><b id='qIOQt'><form id='qIOQt'><ins id='qIOQt'></ins><ul id='qIOQt'></ul><sub id='qIOQt'></sub></form><legend id='qIOQt'></legend><bdo id='qIOQt'><pre id='qIOQt'><center id='qIOQt'></center></pre></bdo></b><th id='qIOQt'></th></span></q></dt></tr></i><div id='qIOQt'><tfoot id='qIOQt'></tfoot><dl id='qIOQt'><fieldset id='qIOQt'></fieldset></dl></div>
                • <legend id='qIOQt'><style id='qIOQt'><dir id='qIOQt'><q id='qIOQt'></q></dir></style></legend>
                  <tfoot id='qIOQt'></tfoot>
                • 本文介绍了动态 Oracle Pivot_In_Clause的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有点卡住了.我想做一个用户角色关系数据透视表,到目前为止我的查询如下所示:

                  I'm kinda stuck. I want to do a user-role-relationship pivot table and my query so far looks like this:

                  WITH PIVOT_DATA AS (
                       SELECT *
                       FROM
                       (
                           SELECT USERNAME, GRANTED_ROLE
                           FROM DBA_USERS@DB_LINK U LEFT OUTER JOIN DBA_ROLE_PRIVS@DB_LINK R
                           ON U.USERNAME = R.GRANTEE
                        )
                  )
                  SELECT *
                  FROM PIVOT_DATA
                  PIVOT
                  (
                      COUNT(GRANTED_ROLE)
                      FOR GRANTED_ROLE
                      IN('CONNECT') -- Just an example
                  )
                  ORDER BY USERNAME ASC;
                  

                  它工作得非常好并且完成了工作,但我不想写任何我想在 pivot_in_clause 中搜索的角色,因为我们得到了很多这样的角色而我不不想每次都检查有没有变化.

                  It works really fine and does the job, but I don't want to write to write any role I want to search for in the pivot_in_clause, because we got like tons of them and I don't want to check every time if there are any changes.

                  那么有没有办法在pivot_in_clause中写一个SELECT?我自己试过:

                  So is there a way to write a SELECT in the pivot_in_clause? I tried it myself:

                  [...]
                  PIVOT
                  (
                      COUNT(GRANTED_ROLE)
                      FOR GRANTED_ROLE
                      IN( SELECT ROLE FROM DBA_ROLES@DB_LINK )
                  )
                  [...]
                  

                  但它总是给我一个 ORA-00936:在整个查询的第 1 行缺少表达式",我不知道为什么.pivot_in_clause 中不能有 SELECT 还是我做错了?

                  But it always gives me an ORA-00936: "missing expression" in line 1 of the whole query and I don't know why. Can't there be a SELECT in the pivot_in_clause or am I doing it wrong?

                  推荐答案

                  您可以在脚本中构建动态查询,看这个例子:

                  You can build dynamic query in your script, look at this example:

                  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
                  

                  这是结果(只有小片段,因为它很宽很长)

                  Here is the result (only small fragment, because it is very wide and long)

                  -----------------------------------------------------------------------------------------------------------------------------------
                      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      
                  

                  这篇关于动态 Oracle Pivot_In_Clause的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Oracle 中的递归 下一篇:如何在Oracle中检索A、B格式的两列数据

                  相关文章

                  <i id='S6Mx7'><tr id='S6Mx7'><dt id='S6Mx7'><q id='S6Mx7'><span id='S6Mx7'><b id='S6Mx7'><form id='S6Mx7'><ins id='S6Mx7'></ins><ul id='S6Mx7'></ul><sub id='S6Mx7'></sub></form><legend id='S6Mx7'></legend><bdo id='S6Mx7'><pre id='S6Mx7'><center id='S6Mx7'></center></pre></bdo></b><th id='S6Mx7'></th></span></q></dt></tr></i><div id='S6Mx7'><tfoot id='S6Mx7'></tfoot><dl id='S6Mx7'><fieldset id='S6Mx7'></fieldset></dl></div>
                • <small id='S6Mx7'></small><noframes id='S6Mx7'>

                  <legend id='S6Mx7'><style id='S6Mx7'><dir id='S6Mx7'><q id='S6Mx7'></q></dir></style></legend>

                    <tfoot id='S6Mx7'></tfoot>

                      • <bdo id='S6Mx7'></bdo><ul id='S6Mx7'></ul>