Oracle PL/SQL光标选择唯一记录并在平面文件中打印

Oracle PL/SQL光标选择唯一记录并在平面文件中打印,oracle,plsql,Oracle,Plsql,我在光标中有一组值。例如: CURSOR c_stock_option IS SELECT empid, '1' AS ISenrolled FROM employee emp UNION ALL SELECT empid, '2' AS ISenrolled FROM employee emp; 现在,我想检查empid是否同时出现在first select(where ISenrolled)中 =1),然后第二次选择(,在其中被滚动 =2)。我只想从第一个选择中获取值,其中enroll=1

我在光标中有一组值。例如:

CURSOR c_stock_option IS
SELECT empid, '1' AS ISenrolled
FROM employee emp
UNION ALL
SELECT empid, '2' AS ISenrolled
FROM employee emp;
现在,我想检查
empid
是否同时出现在first select(
where ISenrolled)中
=1),然后第二次选择(
,在其中被滚动
=2
)。我只想从第一个选择中获取值,其中enroll=1
,并拒绝enroll=2中的值。我只想打印符合此条件的记录

FOR v_stock_option_record IN c_stock_option LOOP
    IF v_esppstock_recs  IN (v_stock_option_record.empid) THEN

    END IF;
    -- Participant file.
    v_member_string_1 := v_stock_option_record.empid || G_DELIMITER || --1. participant id
    v_stock_option_record.last_name || G_DELIMITER || --4. Last Name
    v_stock_option_record.first_name || G_DELIMITER || --5. First Name
END loop;
在查询的第一部分中,选择购买股票的所有员工(这将只给出购买股票的员工集,查询的其他部分给出公司中所有的活动员工,因此位于select第一部分的员工将始终位于select的第二部分,但位于select第二部分的员工不一定位于第一部分。在employee出现在这两个部分中,我需要做的只是选择isenrolled=1)的员工。 下面是要区分的SQL

SELECT
    empid,
    '1' AS ISenrolled
    FROM employee emp,
    hrempusf usf
    where emp.employee = usf.employee
          AND usf.field_key = 76 ---- 76 determines that employee has purchased stocks
UNION ALL
    SELECT
     empid,
    '2' AS ISenrolled
     FROM employee emp;

这里有一种可行的方法,它将只返回1=>IsEnrolled(如果存在),否则它将返回2
IsEnrolled

“数据”是模仿您的两个select语句

with data as(
select 1 empId, 1 ISEnrolled from dual
union
select 2 empId, 1 ISEnrolled from dual
union
select 3 empId, 1 ISEnrolled from dual
union
select 4 empId, 1 ISEnrolled from dual
union
select 5 empId, 1 ISEnrolled from dual /** these 5 are mimicing your first select */
union
select 1 empId, 2 ISEnrolled from dual /** the next are the 'all' */
union
select 2 empId, 2 ISEnrolled from dual
union
select 3 empId, 2 ISEnrolled from dual
union
select 4 empId, 2 ISEnrolled from dual
union
select 5 empId, 2 ISEnrolled from dual
union
select 6 empId, 2 ISEnrolled from dual
union
select 7 empId, 2 ISEnrolled from dual
union
select 8 empId, 2 ISEnrolled from dual
union
select 9 empId, 2 ISEnrolled from dual
union
select 10 empId, 2 ISEnrolled from dual)
,
onlyOneIsEnrolled as (
  select empId, isEnrolled
    from data
    where isEnrolled = 1
) ,
notInOneIsEnrolled as(
  select empId, isEnrolled
    from data d
    where not exists(select null
                           from onlyOneIsEnrolled ooie
                            where ooie.empid = d.empId
                            )
)
select EmpId, isEnrolled
  from onlyOneIsEnrolled
  union
select EmpId, isEnrolled
  from notInOneIsEnrolled
order by isEnrolled, EmpId
这只是完成任务的一种方法,
onlyoneisregisted
收集所有1,然后
notinoneisenlolled
获取所有
emp
不在上面,查询的最后一部分将它们放在一起


有很多方法可以完成此任务,这利用了,但根据您的需要,您可以不使用
with
子句来完成此任务。

您不需要复杂的PL/SQL,只需要一个左外部联接。这将返回所有员工记录,无论它是否与HREMPUSF记录匹配。

SELECT
     empid
     , nvl2(usf.field_key ,'1', '2') AS ISenrolled
  FROM employee emp
     left outer join hrempusf usf
          on ( usf.employee = emp.employee
              and usf.field_key = 76 )

如果第一个参数不为null,则NVL2()返回第二个值,如果为null,则返回第三个参数。

我格式化了您的代码(as),这表明您的部分代码实际上是SQL注释…请单击“编辑”以修复此问题(我已从第一个查询的第二行中删除了一个错误的“as”)@用户,您的编辑并没有真正修复代码…而且,选择本身也没有意义。从您的查询中,工会的两个部分都将始终有相同的员工。请提供您想要的详细信息,以获得更清楚的答案。我必须按光标的方式进行操作吗?例如,光标c_stock_选项是SELECT empid、name、ssn、isenrisenrolled=1游标c_espp_选项为Select empid、name、ssn、isenrolled from employee,其中isenrolled=2现在我想拒绝游标1 Select中的第二个游标中的所有记录,我该怎么做