简体   繁体   English

Oracle / PLSQL-使用列会生成where子句

[英]Oracle / PLSQL - Using column results in a where clause

I've been trying to do something very simple but still can't. 我一直在尝试做一些非常简单的事情,但仍然做不到。 I am trying to interate over a table and use each row of a column in a where clause to display a query. 我正在尝试遍历一个表,并在where子句中使用列的每一行来显示查询。

For example: 例如:

I want to retrieve all users from dba_users pass it to a where clause in a query to show for example account_status and profile for every user. 我想从dba_users中检索所有用户,并将其传递给查询中的where子句,以显示每个用户的例如account_status和配置文件。 But I want to do it in a way so I can turn the result into many html tables. 但我想以某种方式做到这一点,以便可以将结果转换成许多html表。

I've tried too many things really, so I will post something that doesn't work but which I think will show the problem I am having, 我确实尝试了太多事情,所以我会发布一些无效的内容,但我认为这会显示出我遇到的问题,

BEGIN
 FOR i IN (SELECT username from dba_users order by 1)
 LOOP
 EXECUTE IMMEDIATE 'select account_status from dba_users where username like ''||i.username||''';
 END LOOP;
 END;
 /

EDIT: Here's is another example of what I want to achieve: 编辑:这是我想要实现的另一个示例:

  1. Read 2 SQL_IDs from V$SQL 从V $ SQL读取2个SQL_ID

    SQL> select sql_id from v$sql where rownum < 3; SQL>从v $ sql选择sql_id,其中rownum <3;

    SQL_ID SQL_ID

    9avfy3fv2wq2x 9avfy3fv2wq2x
    0ywp98ffdz77f 0ywp98ffdz77f

  2. Use those returned IDs to gather some performance info and get the results in two result sets 使用这些返回的ID收集一些性能信息,并在两个结果集中获得结果

-- HTML Markup -HTML标记

set markup HTML ON HEAD " -  
" -  
BODY "" -  
TABLE "border='1' align='center' summary='Script output'" -  
SPOOL ON ENTMAP ON PREFORMAT OFF  

select sql_id, loads_total from dba_hist_sqlstat where sql_id like '9avfy3fv2wq2x';  
select sql_id, loads_total from dba_hist_sqlstat where sql_id like '0ywp98ffdz77f';  

And I get the following results 我得到以下结果

SQL_ID        LOADS_TOTAL  
------------- -----------  
9avfy3fv2wq2x          21  

SQL_ID        LOADS_TOTAL  
------------- -----------  
0ywp98ffdz77f          12  

Using the markup tag, this translates into: 使用标记标签,可以将其转换为:

Example

Thanks in advance for your time, 在此先感谢您的时间,

od od

Try this using a cursor: 使用游标尝试一下:

 DECLARE
    CURSOR USERS IS
      SELECT username FROM dba_users ;

 BEGIN

    FOR user in USER
    LOOP
       SELECT account_status FROM dba_users WHERE username = user;
    END LOOP;

 END; 

You can use some temporary table to store your data from each select and then you can process it further. 您可以使用一些临时表来存储每个选择中的数据,然后可以对其进行进一步处理。

The other option is using TABLE TYPE OF xxx that you populate in your loop. 另一种选择是使用您在循环中填充的TABLE TYPE OF xxx You can see how to use it later by the second part that displays results. 您可以在稍后显示结果的第二部分中看到如何使用它。

 DECLARE
    CURSOR USERS IS
      SELECT username FROM dba_users ;

    TYPE resultType IS TABLE OF NVARCHAR2(80) ;
    result resulttype := resulttype();

    indx NUMBER(10) :=0;
 BEGIN

  -- we insert data from some selects
  FOR user in USERs
    LOOP
        result.extend();
        indx := indx + 1 ;
       SELECT account_status into result(indx) FROM dba_users WHERE username = user.username;

    END LOOP;


  -- and now we will display content 
    FOR i IN result.FIRST .. result.LAST
   LOOP
       dbms_output.put_line(result(i));
  END LOOP;

 END; 

You are not far away with your code. 您与代码并不遥远。

declare
    v_account_status dba_users.account_status%type;
BEGIN
 FOR cur_users IN (SELECT username from dba_users order by 1)
 LOOP
   SELECT account_status INTO v_account_status -- you need to save somewhere 
                                               -- your value of account_status
          FROM dba_users 
          WHERE username LIKE cur_users.username; -- cur_users is the 
                                                  -- current row of dba_users
    -- do something with v_account_status, like
    -- htp.prn(cur_users.username || ' has account status ' || v_account_status);
 END LOOP;
END;

I believe you trying to acheive this kind of result. 我相信您正在尝试实现这种结果。 However this will work only when dynamic query that you are executing is fetching a single result. 但是,这仅在您正在执行的动态查询正在获取单个结果时才有效。 If your dynamic query is fetching multiple result you can use variable of collection type to store the result using a bulk collect and then loop through the list to display the output. 如果动态查询正在获取多个结果,则可以使用集合类型的变量通过批量收集来存储结果,然后循环浏览列表以显示输出。

    DECLARE
       v_sql         VARCHAR2 (4000);
       v_user_name   VARCHAR2 (400);
    BEGIN
       FOR i IN (SELECT   username
             FROM     dba_users
             WHERE    ROWNUM < 5
             ORDER BY 1) LOOP
          v_sql :=
            'select account_status from dba_users where username= '''
         || i.username
         || '''';

          EXECUTE IMMEDIATE v_sql
          INTO              v_user_name;

          DBMS_OUTPUT.put_line ('v_Sql:' || v_sql || ' v_user: ' || v_user_name);
       END LOOP;
    END;
    /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM