Мне нужно имитировать функциональность вложенного цикла for (или имитировать цикл foreach). Ниже приведен пример запроса (обратите внимание, это пример гораздо более сложного запроса - для простоты).
SELECT userName from employeeDetails where userName = 'Peter';
Достаточно просто. Однако мне нужно, чтобы значение userName было переменной, чтобы я мог вложить этот запрос в родительский запрос. Родительский запрос будет выглядеть так:
SELECT userName from allEmployees;
Таким образом, некоторый код sudo, использующий цикл foreach, например, будет выглядеть так:
foreach x as `SELECT userName from allEmployees`
do
SELECT userName from employeeDetails where userName = x;
done
PS - я запрашиваю базу данных Oracle 10.2.04
Обновление 1 - Извините за путаницу, но я думаю, что мой "простой" пример выше немного СЛИШКОМ прост. У меня есть весь мой запрос, указанный ниже - в этом запросе вы найдете мое имя ( CocoaNoob ) в 13 различных разделах запроса. Мне нужно заменить это жестко закодированное имя именем foreach в таблице.
SELECT SSN, EMP_NAME, HIRE_DT, DEPTID, FULL_PART_TIME,
(CASE WHEN AVAILABLE_VACATION < 0 THEN 0 ELSE AVAILABLE_VACATION END)
AVAILABLE_VACATION
FROM (SELECT 'XXX-XX-' || SUBSTR (A.EMPLID, 6) EMPLID,
A.EMPLID AS SSN,
(CASE
WHEN EMP_CAT NOT IN ('EFT-O', 'NEFT-O')
THEN
TO_CHAR (CURRENT_YR_SICK)
ELSE
'As Needed'
END)
CURRENT_YR_SICK,
(CASE
WHEN EMP_CAT NOT IN ('EFT-O', 'NEFT-O')
THEN
TO_CHAR (CARRY_OVER_SICK)
ELSE
'As Needed'
END)
CARRY_OVER_SICK,
(CASE
WHEN EMP_CAT NOT IN ('EFT-O', 'NEFT-O')
THEN
TO_CHAR ( (CURRENT_YR_SICK + CARRY_OVER_SICK))
ELSE
'As Needed'
END)
TOTAL_SICK,
(CASE
WHEN EMP_CAT NOT IN ('EFT-O', 'NEFT-O')
THEN
TO_CHAR (PERSONAL)
ELSE
'As Needed'
END)
PERSONAL,
CURRENT_YR_VACATION,
CARRY_OVER_VACATION,
(CURRENT_YR_VACATION + CARRY_OVER_VACATION) TOTAL_VACATION,
NVL (
(SELECT SUM (NO_DAYS)
FROM HRU_ENT_COFF_DETAILS
WHERE emplid = (SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')
AND comp_off_type = 'ECOFF'),
'0'
)
EARNED_COMP_OFF,
NVL (
(SELECT SUM (NO_DAYS)
FROM HRU_ENT_COFF_DETAILS
WHERE emplid = (SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')
AND comp_off_type = 'BLOOD'),
'0'
)
BLOOD_COMP_OFF,
NVL (
(SELECT SUM (NO_DAYS)
FROM HRU_ENT_COFF_DETAILS
WHERE emplid = (SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')
AND comp_off_type = 'HABITAT'),
'0'
)
HABITAT_COMP_OFF,
NVL (
(SELECT SUM (NO_DAYS)
FROM HRU_ENT_COFF_DETAILS
WHERE emplid = (SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')
AND comp_off_type = 'HCOFF'),
'0'
)
HOLIDAY_COMP_OFF,
(INITCAP (E.FIRST_NAME) || ' ' || INITCAP (E.LAST_NAME))
AS EMP_NAME,
TO_CHAR (HIRE_DT, 'mm/dd/yyyy') HIRE_DT,
TO_CHAR (REHIRE_DT, 'mm/dd/yyyy') REHIRE_DT,
JOBTITLE,
OFFICER_TITLE,
CLOCK_NBR,
D.DEPTID DEPTID,
FILE_NBR,
D.DESCR DESCR,
(CASE
WHEN FLSA_STATUS = 'E' AND FULL_PART_TIME = 'F'
THEN
'Exempt/Full-Time'
WHEN FLSA_STATUS = 'E' AND FULL_PART_TIME = 'P'
THEN
'Exempt/Part-Time'
WHEN FLSA_STATUS = 'N' AND FULL_PART_TIME = 'F'
THEN
'Non-Exempt/Full-Time'
WHEN FLSA_STATUS = 'N' AND FULL_PART_TIME = 'P'
THEN
'Non-Exempt/Part-Time'
ELSE
''
END)
AS EMP_STATE,
full_part_time,
COFF_ELIGIBLE,
EMP_CAT,
(CASE
WHEN EMP_CAT NOT IN ('EFT-O', 'NEFT-O')
THEN
TO_CHAR( ( (CURRENT_YR_SICK + CARRY_OVER_SICK)
- (NVL (
(SELECT SUM (no_days_used)
FROM HRU_ENT_USAGE_HIST
WHERE emplid =
(SELECT emplid
FROM employees
WHERE user_id =
'__CocoaNoob__')
AND TO_CHAR (
FROM_DATE_USED,
'yyyy'
) =
TO_CHAR (SYSDATE,
'yyyy')
AND entitlement_code IN
('SICK',
'FMS',
'PLS')),
0
))))
ELSE
'As Needed'
END)
AVAILABLE_SICK,
( (CURRENT_YR_VACATION + CARRY_OVER_VACATION)
- (NVL (
(SELECT SUM (no_days_used)
FROM HRU_ENT_USAGE_HIST
WHERE emplid =
(SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')
AND TO_CHAR (FROM_DATE_USED, 'yyyy') =
TO_CHAR (SYSDATE, 'yyyy')
AND entitlement_code IN
('VACATION', 'FMV', 'PLV')),
0
)))
AVAILABLE_VACATION,
(CASE
WHEN EMP_CAT NOT IN ('EFT-O', 'NEFT-O')
THEN
TO_CHAR( (PERSONAL
- (NVL (
(SELECT SUM (no_days_used)
FROM HRU_ENT_USAGE_HIST
WHERE emplid =
(SELECT emplid
FROM employees
WHERE user_id =
'__CocoaNoob__')
AND TO_CHAR (
FROM_DATE_USED,
'yyyy'
) =
TO_CHAR (SYSDATE,
'yyyy')
AND entitlement_code IN
('PERSONAL',
'FMP',
'PLP')),
0
))))
ELSE
'As Needed'
END)
AVAILABLE_PERSONAL,
(CASE
WHEN COFF_ELIGIBLE = 'YES'
THEN
TO_CHAR(NVL (
(SELECT (SUM (NO_DAYS)
- NVL (SUM (NO_DAYS_USED), 0))
FROM HRU_ENT_COFF_DETAILS
WHERE emplid =
(SELECT emplid
FROM employees
WHERE user_id =
'__CocoaNoob__')
AND comp_off_type = 'ECOFF'),
'0'
))
ELSE
'Not Eligible'
END)
AVAILABLE_ECOFF,
(TO_CHAR(NVL (
(SELECT (SUM (NO_DAYS)
- NVL (SUM (NO_DAYS_USED), 0))
FROM HRU_ENT_COFF_DETAILS
WHERE emplid =
(SELECT emplid
FROM employees
WHERE user_id =
'__CocoaNoob__')
AND comp_off_type = 'HCOFF'),
'0'
)))
AVAILABLE_HCOFF,
(TO_CHAR(NVL (
(SELECT (SUM (NO_DAYS)
- NVL (SUM (NO_DAYS_USED), 0))
FROM HRU_ENT_COFF_DETAILS
WHERE emplid =
(SELECT emplid
FROM employees
WHERE user_id =
'__CocoaNoob__')
AND comp_off_type = 'BLOOD'),
'0'
)))
AVAILABLE_BCOFF,
(TO_CHAR(NVL (
(SELECT (SUM (NO_DAYS)
- NVL (SUM (NO_DAYS_USED), 0))
FROM HRU_ENT_COFF_DETAILS
WHERE emplid =
(SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')
AND comp_off_type = 'HABITAT'),
'0'
)))
AVAILABLE_HACOFF
FROM HRU_ENTITLEMENTS_MASTER A,
EMPLOYEES E,
DEPTS D,
(SELECT EMPLID,
(CASE
WHEN FLSA_STATUS = 'E'
AND FULL_PART_TIME = 'F'
AND OFFICER_TITLE = 'NONE'
THEN
'EFT-NO'
WHEN FLSA_STATUS = 'E'
AND FULL_PART_TIME = 'F'
AND OFFICER_TITLE != 'NONE'
THEN
'EFT-O'
WHEN FLSA_STATUS = 'E'
AND FULL_PART_TIME = 'P'
AND OFFICER_TITLE = 'NONE'
THEN
'EPT-NO'
WHEN FLSA_STATUS = 'N'
AND FULL_PART_TIME = 'F'
AND OFFICER_TITLE != 'NONE'
THEN
'NEFT-O'
WHEN FLSA_STATUS = 'N'
AND FULL_PART_TIME = 'F'
AND OFFICER_TITLE = 'NONE'
THEN
'NEFT-NO'
WHEN FLSA_STATUS = 'N'
AND FULL_PART_TIME = 'P'
AND OFFICER_TITLE = 'NONE'
THEN
'NEPT-NO'
ELSE
''
END)
AS EMP_CAT,
(CASE
WHEN OFFICER_TITLE IN
('CHAIRMAN & CEO',
'VICE CHAIRMAN & CAO',
'PRESIDENT & COO',
'EVP, TREASURER & CFO',
'EXEC VP, SECRETARY & GEN COUNS',
'EXECUTIVE VICE PRESIDENT',
'SENIOR VP & CIO',
'SENIOR VICE PRESIDENT',
'DIRECTOR',
'FIRST VP/CONTROLLER',
'FIRST VP',
'VICE PRESIDENT')
THEN
'NO'
ELSE
'YES'
END)
COFF_ELIGIBLE
FROM EMPLOYEES
WHERE EMPLID = (SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')) B
WHERE A.EMPLID = (SELECT emplid
FROM employees
WHERE user_id = '__CocoaNoob__')
AND A.YEAR = '2012'
AND A.EMPLID = E.EMPLID
AND D.DEPTID = E.DEPTID
AND E.EMPLID = B.EMPLID);