LOOP/FOR oracle 脚本语句

LOOP/FOR statement on oracle script

我正在 运行 编写一个 ansible playbook,它 运行 是一个 Oracle 数据库的 sqlplus 脚本。
基本上,该脚本会创建一个包含一些服务器信息的 CSV 文件。查询几乎是自动生成的,因此很难对其进行更改。

set markup csv on
spool 'playbook-dir/files/servers.csv'

SELECT *
FROM   (SELECT DISTINCT server.primary_name                  SERVER_NAME,
                        server.arpa_domain                   ARPA_DOMAIN,
                        server.impact                        IMPACT,
                        instance_definition.category         SOLUTION_CATEGORY,
                        instance_definition.instance_name    SOLUTION_NAME,
                        instance_on_server.ins_instance_name INSTANCE_NAME
        FROM   server_db.instance_definition, server_db.instance_on_server, server_db.business, server_db.server_customer, server_db.server
        WHERE  ( server_db.instance_definition.app_id(+) = server_db.instance_on_server.app_id )
               AND ( server_db.server.system_id = server_db.instance_on_server.system_id(+) )
               AND ( ( instance_definition.instance_name LIKE '%windows%' )
                      OR ( instance_definition.instance_name LIKE '%centos%' ) )
               AND ( instance_on_server.status LIKE 'in production' )
               AND ( server_db.business.business_id(+) = server_db.server_customer.business_id )
               AND ( server_db.server_customer.system_id(+) = server_db.server.system_id )
               AND (( instance_definition.category LIKE 'os' ))
               AND (( server.primary_name||'.'||server.arpa_domain LIKE '%' ))
               AND business.secure_access_r <> 1)
WHERE  ROWNUM <= 600000 + 1
ORDER  BY server_name;  

spool off

问题是这个查询带来了所有 5000 个服务器,而我只需要 200 个。
我想添加只包含我需要的服务器的 LOOP/FOR 语句,但我认为我做错了什么。
这是带有循环的查询:

declare
  type table_varchar  is table of varchar2(10);

  var_table_varchar  table_varchar;
begin
  var_table_varchar  := table_varchar('server1', 'server2', 'server3', 'server4');

  for elem in 1 .. var_table_varchar.count loop
    SELECT *
    FROM   (SELECT DISTINCT server.primary_name                  SERVER_NAME,
                            server.arpa_domain                   ARPA_DOMAIN,
                            server.impact                        IMPACT,
                            instance_definition.category         SOLUTION_CATEGORY,
                            instance_definition.instance_name    SOLUTION_NAME,
                            instance_on_server.ins_instance_name INSTANCE_NAME
            FROM   server_db.instance_definition, server_db.instance_on_server, server_db.business, server_db.server_customer, server_db.server
            WHERE  ( server_db.instance_definition.app_id(+) = server_db.instance_on_server.app_id )
                   AND ( server_db.server.system_id = server_db.instance_on_server.system_id(+) )
                   AND ( ( instance_definition.instance_name LIKE '%windows%' )
                          OR ( instance_definition.instance_name LIKE '%centos%' ) )
                   AND ( instance_on_server.status LIKE 'in production' )
                   AND ( server_db.business.business_id(+) = server_db.server_customer.business_id )
                   AND ( server_db.server_customer.system_id(+) = server_db.server.system_id )
                   AND (( instance_definition.category LIKE 'os' ))
                   AND (( server.primary_name||'.'||server.arpa_domain LIKE '%' ))
                   AND (( server.primary_name LIKE '%var_assoc_varchar(elem)%' ))
                   AND business.secure_access_r <> 1)
    WHERE  ROWNUM <= 600000 + 1
    ORDER  BY server_name; 
  end loop;
end;

当我 运行 它时,我得到这个错误:

Error report -
ORA-06550: line 9, column 5:
PLS-00428: an INTO clause is expected in this SELECT statement
06550. 00000 - "line %s, column %s:\n%s"
*Cause: Usually a PL/SQL compilation error.
*Action:

遗憾的是,我不是 DBA,到目前为止,我的知识让我如此。所以建议将不胜感激

你不能在 PL/SQL.

中使用没有 into 子句的普通 select sql

您对 PL/SQL 中的查询有何期望?

您的第一个查询已执行并填充了 csv,因为它是单个查询而不是匿名块。

您可以简单地将条件添加到第一个查询的 where 子句中,如下所示:

server.primary_name in ('server1', 'server2', 'server3', 'server4')