PL/SQL 游标选择唯一记录并在平面文件中打印
我在游标中有一组值。例如:
CURSOR c_stock_option IS
SELECT empid, '1' AS ISenrolled
FROM employee emp
UNION ALL
SELECT empid, '2' AS ISenrolled
FROM employee emp;
现在我想检查 empid
是否同时出现在第一个选择中(其中 IEnrolled =1
)和第二个选择(其中 IEnrolled =2
)。我只想从第一个 select where enroll=1
获取值并拒绝 where 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;
在查询的第一部分中,它选择所有已购买股票的员工(这将仅给出已购买股票的员工集合,查询的其他部分给出公司中的所有在职员工,因此位于选择的第一部分中的员工将始终位于选择的第二部分中,但是位于选择的第二部分中的员工不一定位于第一部分中,当员工出现在两个部分中时,我需要做什么。只需选择 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;
I have set of values in a cursor. For example:
CURSOR c_stock_option IS
SELECT empid, '1' AS ISenrolled
FROM employee emp
UNION ALL
SELECT empid, '2' AS ISenrolled
FROM employee emp;
Now I want to check if the empid
appears both in first select (where ISenrolled
) and second select (
=1where ISenrolled
). I want to only grab the value from first select
=2where enroll=1
and reject the one where enroll=2
. I want to only print records that qualifies this criteria.
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;
In the first part of query it is selecting all the employees that have purchased the stocks (that will give only the set of employees who have purchased the stocks, other part of the query gives all the active employees in the company, so the employee who is in the first part of the select will always be in second part of the select, but the employee who is in second part of select is not necessarily in the 1st part. In the scenario when employee appears in both parts what I need to do is just select the employee who has isenrolled=1).
Below is the SQL to differentiate
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种可行的方法,它只会返回 1=>IsEnrolled(如果存在),否则会返回 2
IsEnrolled
。“数据”是模仿你的两个选择语句。
这只是完成任务的一种方法,
onlyOneIsEnrolled
收集所有 1,然后notInOneIsEnrolled
获取所有不在上面的emps
,查询的最后部分将它们放在一起。有很多方法可以完成此任务,这利用 CTE,但根据您的需要,您可以无需使用
with
子句即可完成此操作。Here is one way that should work, it will only return the 1=>IsEnrolled if it exists, otherwise it'll return the 2
IsEnrolled
.The "data" is to mimic your two select statements.
This is just one way to accomplish the task, the
onlyOneIsEnrolled
gathers all the 1's, thennotInOneIsEnrolled
gets all theemps
not in the above, the final part of the query puts them together.There are many ways to accomplish this task, this utilizes CTE's but depending on your need you may be able to do this without utilizing the
with
clause.为此,您不需要复杂的 PL/SQL,只需要 LEFT OUTER JOIN。这将返回所有 EMPLOYEE 记录,无论它是否与 HREMPUSF 记录匹配。
如果第一个参数不为 null,则 NVL2() 返回第二个值;如果为 null,则返回第三个参数。
You don't need complicated PL/SQL for this, you just need a LEFT OUTER JOIN. This will return all the EMPLOYEE records, regardless of whether it matches an HREMPUSF record.
The NVL2() returns the second value if the first argument is not null and the third argument if it is null.