2010-12-01 47 views
0

我在遊標中設置了一組值。例如: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無論是在第一選擇(where ISenrolled =1)和第二選擇(where ISenrolled =2)。我只想從第一個選擇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的員工)。 下面是區分

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

我對代碼進行了格式化([先前建議](http://meta.stackexchange.com/questions/71252/pl-sql-cursor-select-unique-records-from-cursor-and-print-in- flat-file-closed)),它揭示了你的部分代碼實際上是SQL評論......請點擊「編輯」來解決這個問題? (我已經在第一個查詢中從第二行刪除了一個錯誤的「AS」。) – Arjan 2010-12-01 15:14:16

+0

@user,你編輯並沒有真正修復代碼......另外,SELECT本身沒有意義。 – Arjan 2010-12-01 15:46:08

回答

0

的SQL下面是應該工作,如果它存在,它只會返回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 

這只是一個方式來完成任務,onlyOneIsEnrolled將收集所有11,則notInOneIsEnrolled得到所有的emps沒有在上面,查詢的最後部分提出在一起。

有很多方法可以完成這項任務,它利用了CTE's,但根據您的需要,您可以在不利用with條款的情況下做到這一點。

1

你不需要複雜的PL/SQL,你只需要一個LEFT OUTER JOIN。這將返回所有EMPLOYEE記錄,無論它是否與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) 

的NVL2()如果第一參數不爲null,第三個參數,如果它是零返回第二值。