从名称为另一张表的值的一个表列中获取数据

时间:2019-06-12 09:01:16

标签: sql oracle security

我需要从主表中获取列,并且我需要获取的列列表将位于不同用户的元数据表中。这将是动态的,因为每个用户可能没有相同数量的列被获取。

元数据表

User    short_desc
rk      sector
rk      Industry
pa      sector
pa      Industry
pa      Subindustry

主数据表

ID          sector              Industry                  Subindustry
594918104   Technology          Information Technology    CyberSecurity
G0464B107   Financial Services  Financials                Banks

当从元表中选择用户rk时,我只想获取部门和行业 当选择用户pa时,我也只希望获取行业,行业和子行业字段。

从另一个解决方案中进行了尝试,但这不是动态的。

SELECT t2.user_id,
       t1.attr_name,
       CASE
          WHEN t1.attr_path = 'str1' THEN t2.str1
          WHEN t1.attr_path = 'str2' THEN t2.str2
       END
          col_sel
  FROM metadata t1 INNER JOIN class t2 ON t1.org_id = t2.org_id
 WHERE t1.org_id = 1

当从元表中选择用户rk时,我只想获取部门和行业 当选择用户pa时,我也只希望获取行业,行业和子行业字段。

2 个答案:

答案 0 :(得分:2)

选择所有列,但在用户没有元数据权限时返回这些列中的* upload completely sent off: 106 out of 106 bytes < HTTP/1.1 200 OK < Cache-Control: no-cache < Pragma: no-cache < Content-Type: application/json; charset=utf-8 < Expires: -1 < Server: Microsoft-IIS/10.0 * cookie 'ASP.NET_SessionId' dropped, domain 'cswsql' must not set cookies for 'cswsql' < Set-Cookie: ASP.NET_SessionId=khdalre2kjro4usbaq4co33c; path=/; HttpOnly < X-AspNet-Version: 4.0.30319 * cookie '.eti_ASPXAUTH' dropped, domain 'cswsql' must not set cookies for 'cswsql' < Set-Cookie: .eti_ASPXAUTH=75524EDA1707527A449A395913EDB222D37F40171D44037CA430958BD2F24BF90C2975461F57F58EC6A6E0450ACD98AE64092B13CCDAA73E4CE7207AA2CE834688CBD4113C82AFAB4F513FC2DEFFCEF34496FF47896BCF8BBB3424FD6F6F8F1593B7869E2647AEB82F9D6CD89B2E939E38036D4A94635072996E88528225E793; path=/; HttpOnly * cookie 'eti_sessionInfo' dropped, domain 'cswsql' must not set cookies for 'cswsql' < Set-Cookie: eti_sessionInfo=YwBzAHcAcwBxAGwAXABjAG8AbQBwAHUAcwCnAFMAaQBzAHQAZQBtAGEAQwBTAFcApwBDAFMAVwCnAEUAeAAgADIAMAAxADkApwAxAKcAUABUAC0AUABUAA==; path=/ < X-Powered-By: ASP.NET < Date: Thu, 13 Jun 2019 14:09:21 GMT < Content-Length: 56350 < * Connection #0 to host cswsql left intact 值:

Oracle设置

NULL

查询

CREATE TABLE metadata ( org_id, Username, short_desc ) AS
SELECT 1, 'aa', 'Industry'    FROM DUAL UNION ALL
SELECT 1, 'rk', 'sector'      FROM DUAL UNION ALL
SELECT 1, 'rk', 'Industry'    FROM DUAL UNION ALL
SELECT 1, 'pa', 'sector'      FROM DUAL UNION ALL
SELECT 1, 'pa', 'Industry'    FROM DUAL UNION ALL
SELECT 1, 'pa', 'Subindustry' FROM DUAL;

CREATE TABLE master ( org_id, ID, sector, Industry, Subindustry ) AS
SELECT 1, '594918104', 'Technology',         'Information Technology', 'CyberSecurity' FROM DUAL UNION ALL
SELECT 1, 'G0464B107', 'Financial Services', 'Financials',             'Banks'         FROM DUAL

输出

USERNAME | ID        | SECTOR             | INDUSTRY               | SUBINDUSTRY  
:------- | :-------- | :----------------- | :--------------------- | :------------
rk       | G0464B107 | Financial Services | Financials             | null         
rk       | 594918104 | Technology         | Information Technology | null         
aa       | G0464B107 | null               | Financials             | null         
aa       | 594918104 | null               | Information Technology | null         
pa       | G0464B107 | Financial Services | Financials             | Banks        
pa       | 594918104 | Technology         | Information Technology | CyberSecurity

db <>提琴here

答案 1 :(得分:1)

一种选择是使用返回引用游标的函数。

测试用例:

SQL> select * from metadata;

C_ SHORT_DESC
-- --------------------
rk sector
rk industry
pa industry
pa sector
pa subindustry

SQL> select * from master_data;

ID         SECTOR          INDUSTRY             SUBINDUSTRY
---------- --------------- -------------------- ---------------
5949       technology      information tech     cyber security
g046       financial       financials           banks

功能:

SQL> create or replace function f_meta (par_user in varchar2)
  2    return sys_refcursor
  3  is
  4    l_str varchar2(200);
  5    rc    sys_refcursor;
  6  begin
  7    select listagg(short_desc, ', ') within group (order by null)
  8      into l_str
  9      from metadata
 10      where c_user = par_user;
 11
 12    l_str := 'select ' || l_str ||' from master_data';
 13
 14    open rc for l_str;
 15    return rc;
 16  end;
 17  /

Function created.

测试:

SQL> select f_meta('rk') from dual;

F_META('RK')
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

INDUSTRY             SECTOR
-------------------- ---------------
information tech     technology
financials           financial


SQL> select f_meta('pa') from dual;

F_META('PA')
--------------------
CURSOR STATEMENT : 1

CURSOR STATEMENT : 1

INDUSTRY             SECTOR          SUBINDUSTRY
-------------------- --------------- ---------------
information tech     technology      cyber security
financials           financial       banks


SQL>