简体   繁体   English

如何根据 select 中的其他列值分配多个变量 (PL/SQL)

[英]How do I assign multiple variables based on other columns values from select (PL/SQL)

Hi I'm fairly new to this, wondering how to assign values to multiple variables from a select statement so that i can use them in apex.嗨,我对此很陌生,想知道如何从 select 语句中为多个变量赋值,以便我可以在顶点中使用它们。

DECLARE
emails_hr varchar2(2000);
emails_ops varchar2(2000);
emails_finance varchar2(2000);
emails_sales varchar2(2000);
BEGIN

SELECT
  DEPARTMENT,
  LISTAGG(lower(EMAIL), '; ') WITHIN GROUP (ORDER BY DISPLAY_AREA) as EMAILS
FROM (
  select
    DEPARTMENT,
    EMAIL 
from CONTACT_TABLE
    where EMAIL is not NULL
    and OFFICE_ID = 100
 )
GROUP BY DEPARTMENT;

END;

The select query returns this: select 查询返回:

DEPARTMENT          EMAILS                  

human resources     abby@gmail.com                  
operations          bob@gmail.com; carol@gmail.com; dave@gmail.com  
finance             emma@gmail.com; fred@gmail.com          
sales               gary@gmail.com; harry@gmail.com; ian@gmail.com  

It's expecting an INTO clause so I tried putting the LISTAGG part in a case statement to select into the variables based on department, but I can't get it to a point where I don't just get a syntax error.它期待一个 INTO 子句,所以我尝试将 LISTAGG 部分放在 case 语句中到 select 到基于部门的变量中,但我无法做到不只是出现语法错误的地步。

EDIT: I should mention, not all offices have all 4 departments编辑:我应该提一下,并非所有办公室都有所有 4 个部门

My head hurts, thanks in advance for any help我的头很痛,在此先感谢您的帮助

As mentioned in the comments it is hard to work out exactly what you want to do but I think you might be after a cursor.....正如评论中提到的,很难准确地计算出你想做什么,但我认为你可能在 cursor 之后......

DECLARE
   emails_hr varchar2(2000);
   emails_ops varchar2(2000);
   emails_finance varchar2(2000);
   emails_sales varchar2(2000);

   CURSOR C_DEPARTMENTS IS       
   SELECT DEPARTMENT,
     LISTAGG(lower(EMAIL), '; ') WITHIN GROUP (ORDER BY DISPLAY_AREA) as EMAILS
   FROM (SELECT DEPARTMENT, EMAIL 
         FROM  CONTACT_TABLE
         WHERE EMAIL is not NULL
         AND   OFFICE_ID = 100
   )
  GROUP BY DEPARTMENT;

BEGIN
  FOR v_row in c_departments LOOP
    -- do whatever you want in here eg
    dbms_output.put_line('department = ' || v_row.department);
    dbms_output.put_line('emails = ' || v_row.emails);
  END LOOP;
END;

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

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