gpt4 book ai didi

mysql - 使用 2 个游标,一个为另一个获取参数

转载 作者:行者123 更新时间:2023-11-30 01:11:32 25 4
gpt4 key购买 nike

我正在做一项练习,要求我使用 2 个显式光标。第一个游标用于获取第二个游标的参数。最终目标是根据注册找到每辆车的最近“租赁日期”。例如,注册号 345JKL 于 01/06/2010、07/09/2011 和 08/09/2013 租用。我希望它只返回最近的日期,即 08/09/2013,并且我希望它为表中的每个注册提供最近的日期。

我知道有更好的方法可以做到这一点,例如 MAX、子查询等(这两种方式我都不允许使用),但作为“横向思维练习”,我需要在没有内置函数的情况下做到这一点,子查询和其他让生活变得轻松的事情。

我对这个有点困惑。

这是我到目前为止所拥有的,但对我毫无帮助:

    declare
v_maxdate DATE;
v_reg VARCHAR2(20);

cursor reg_cur IS
SELECT * FROM i_car;
v_car reg_cur%ROWTYPE;

cursor c_reg (reg i_booking.registration%TYPE) IS
SELECT date_reserved from i_booking
WHERE registration = reg;
v_date c_reg%ROWTYPE;

begin
FOR v_date IN c_reg (v_car.registration) LOOP
v_maxdate := '01/JAN/90';
If v_date > v_maxdate THEN
v_maxdate := v_date;
end if;
end loop;
end;

它向我抛出了这个错误:

If v_date > v_maxdate THEN
*
ERROR at line 17:
ORA-06550: line 17, column 11:
PLS-00306: wrong number or types of arguments in call to '>'
ORA-06550: line 17, column 1:
PL/SQL: Statement ignored

我想与其继续用头撞 table ,不如寻求指导。

感谢您的帮助。

最佳答案

v_date 变量是一条记录,因此您必须使用点来实际访问其某些字段 - 在您的情况下为 date_reserved:

begin
v_maxdate := '01/JAN/90';
FOR v_date IN c_reg (v_car.registration) LOOP
If v_date.date_reserved > v_maxdate THEN
v_maxdate := v_date;
end if;
end loop;
end;

我还将 v_maxdate 的初始化移到循环之外。

关于mysql - 使用 2 个游标,一个为另一个获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19439156/

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