gpt4 book ai didi

oracle - 从 PL/SQL 函数返回多个值

转载 作者:行者123 更新时间:2023-12-02 04:10:27 26 4
gpt4 key购买 nike

我需要尝试在飞机上找到一个空闲座位,然后预订该座位。

我需要返回一个指示成功与否的值以及一个座位号。通过环顾四周,我发现函数不能返回超过 1 个值,因此我考虑使用数组,但查看这些文档后发现,我对 PL/SQL 的了解还不够好,无法理解如何使用它们.

所以我在这里,被困了。

到目前为止我得到的代码如下所示:

CREATE OR REPLACE FUNCTION RESERVE_SEAT(P_NO VARCHAR2, ID NUMBER, RESERVE_TIME NUMBER, S_NO VARCHAR2)
RETURN INTEGER AS
RES INTEGER := 0;
COUNTS INTEGER := 0;
BEGIN
SELECT COUNT(*) INTO COUNTS FROM SEAT WHERE SEAT_NO=S_NO AND PLANE_NO=P_NO;
IF(COUNTS = 1) THEN
UPDATE SEAT
SET RESERVED = ID, BOOKING_TIME = RESERVE_TIME, BOOKED=ID
WHERE PLANE_NO=P_NO AND SEAT_NO=S_NO;
COMMIT;
ELSE IF(COUNTS = 0) THEN
RES := -1;
END IF;
END IF;
RETURN RES;
END RESERVE_SEAT;

上面的函数被下面的函数调用

CREATE OR REPLACE FUNCTION GET_FREE_SEAT(P_NO VARCHAR2, ID NUMBER, RESERVE_TIME NUMBER)
RETURN INTEGER AS
RESERVED_SEAT_NO VARCHAR2(100) := 'NULL';
RES INTEGER := 0; -- Assume Success Scenario from the Get-go
BEGIN
SELECT SEAT_NO INTO RESERVED_SEAT_NO
FROM SEAT
WHERE RESERVED IS NULL AND BOOKED IS NULL AND ROWNUM = 1
OR BOOKED IS NULL AND ((RESERVE_TIME - 5000) <= BOOKING_TIME) AND ROWNUM = 1;

IF(RESERVED_SEAT_NO != 'NULL') THEN
RES := RESERVE_SEAT(P_NO,ID,RESERVE_TIME,RESERVEd_SEAT_NO);
END IF;
RETURN RES;
END GET_FREE_SEAT;

现在不太确定该怎么做。

如何同时返回 RESRESERVED_SEAT_NO

最佳答案

你不可能返回 2 个变量。必须是一个。

您可以使用可以从函数返回的自定义记录类型或数组。

TYPE new_type is record(RES pls_integer, RESERVED_SEAT_NO pls_integer);

CREATE OR REPLACE FUNCTION GET_FREE_SEAT(P_NO VARCHAR2, ID NUMBER, RESERVE_TIME NUMBER)
RETURN new_type AS new_type_variable
BEGIN
SELECT SEAT_NO
INTO new_type_variable.RESERVED_SEAT_NO
FROM SEAT
WHERE
RESERVED IS NULL
AND BOOKED IS NULL
AND ROWNUM = 1
OR BOOKED IS NULL
AND (RESERVE_TIME - 5000) <= BOOKING_TIME
AND ROWNUM = 1;

IF(RESERVED_SEAT_NO != 'NULL') THEN
select RESERVE_SEAT(P_NO,ID,RESERVE_TIME,RESERVEd_SEAT_NO) into new_type_variable.res from dual;
END IF;
RETURN new_type_variable;
END GET_FREE_SEAT;

关于oracle - 从 PL/SQL 函数返回多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23447570/

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