gpt4 book ai didi

database - 将 oracle 数据库表与自定义类型的表连接起来

转载 作者:搜寻专家 更新时间:2023-10-30 22:29:36 24 4
gpt4 key购买 nike

我想加入一个表,比如 EMPLOYEE 和另一个自定义 oracle 类型 MATCHING_CRITERIA_LIST。MATCHING_CRITERIA_LIST 是自定义 oracle 类型 CRITERIA 的表。所有DDL如下:

CREATE OR REPLACE
type CRITERIA as object (
DOB DATETIME,
SALARY NUMBER
);

CREATE OR REPLACE TYPE
MATCHING_CRITERIA_LIST IS TABLE OF CRITERIA;

CREATE TABLE EMPLOYEE{
ID NUMBER PRIMARY KEY NOT NULL,
NAME VARCHAR(20 BYTE),
DOB DATETIME,
SALARY NUMBER
}

我实际上想要实现的是,

var allEmployeeList = new List<Employee>();
var filteredList = new List<Employee>();
var matchingCriteria = new List<MatchingCritera>{
new MatchingCritera(){DOB = <date1>, salary = <sal1>},
new MatchingCritera(){DOB = <date2>, salary = <sal2>},
new MatchingCritera(){DOB = <date3>, salary = <sal1>}
}
foreach(var emp in allEmployeeList)
{
foreach(var criteria in matchingCriteria)
{
if(emp.DOB == criteria.DOB && emp.salary = criteria.salary)
{
filteredList.Add(emp);
}
}
}

我希望在 SP 中使用相同的逻辑。我目前正在执行以下操作,效果很好。

CREATE OR REPLACE
type IDTYPE as object (
id NUMBER
);
CREATE OR REPLACE
type IDTABLETYPE IS TABLE OF IDTYPE;

CREATE OR REPLACE PROCEDURE GET_FILTERED_EMPLOYEE (
IN_CRITERIA_LIST IN MATCHING_CRITERIA_LIST,
CUR_OUT OUT sys_refcursor
)
IS

V_ID_TABLE IDTABLETYPE;
V_TEMP_ID_COLL EMPLOYEE_ID;

BEGIN

V_ID_TABLE := IDTABLETYPE();
V_TEMP_ID_COLL := EMPLOYEE_ID();

IF IN_CRITERIA_LIST.COUNT > 0 THEN
FOR i IN IN_CRITERIA_LIST.FIRST .. IN_CRITERIA_LIST.LAST
LOOP
SELECT EMP.ID BULK COLLECT INTO V_TEMP_ID_COLL FROM EMPLOYEE EMP WHERE
EMP.DOB = IN_CRITERIA_LIST(i).DOB
AND EMP.SALARY = IN_CRITERIA_LIST(i).SALARY
ORDER BY EMP.ID DESC;

IF (V_TEMP_ID_COLL.COUNT > 0) THEN
FOR j IN V_TEMP_ID_COLL.FIRST .. V_TEMP_ID_COLL.LAST
LOOP
V_ID_TABLE.extend();
V_ID_TABLE(V_ID_TABLE.count) := IDTYPE(TO_NUMBER(V_TEMP_ID_COLL(j)));
END LOOP;
END IF;
END LOOP;
END IF;

OPEN CUR_OUT FOR
SELECT * FROM EMPLOYEE EMP WHERE EMP.ID IN (SELECT * FROM TABLE(V_ID_TABLE));
END;

我想删除 IN_CRITERIA_LIST 上的 for 循环,因为它会影响性能,并执行如下操作:

SELECT * FROM EMPLOYEE EMP
INNER JOIN MATCHING_CRITERIA_LIST MCL ON
EMP.DOB = MCL.DOB
AND EMP.SALARY = MCL.SALARY
ORDER BY TD.TRANS_DASHBOARD_ID DESC;

有人可以指导我如何将我的自定义 UDT 表与 oracle 表连接起来吗?

最佳答案

您不需要 IDTYPE 对象或所有 PL/SQL 循环(甚至连接):

CREATE OR REPLACE PROCEDURE GET_FILTERED_EMPLOYEE (
IN_CRITERIA_LIST IN MATCHING_CRITERIA_LIST,
CUR_OUT OUT sys_refcursor
)
IS
BEGIN
OPEN CUR_OUT FOR
SELECT *
FROM EMPLOYEE
WHERE CRITERIA( dob, salary ) MEMBER OF IN_CRITERIA_LIST;
END;
/

关于database - 将 oracle 数据库表与自定义类型的表连接起来,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46419376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com