我的带有光标的购物车程序抛出错误
My shopping cart procedure with cursor is throwing errors
我有一个小组项目,我们正在其中创建一个购物车应用程序。我得到了一些反馈并修复了它,把它变成了一个带有光标的过程。但是,我们仍然遇到我不知道如何修复的错误,例如尝试将空值放入不能为空的 table 列中。我不知道为什么它认为我试图输入一个空值。我们的项目明天到期,请帮忙!
起初我尝试将其作为触发器,但它不起作用,所以建议将其转换为带有游标的过程,所以我就这样做了。
create or replace PROCEDURE CANCELCART (arg_cart_id IN number)
IS
ws_prod_id number;
ws_item_quantity_in_cart number;
ws_cart_id number;
ws_cart_status char;
cursor cancel IS
select item_product_id, item_quantity_in_cart
FROM sc_items i
WHERE item_cart_id = arg_cart_id
group by item_product_id;
alreadycancelled exception;
BEGIN
select max(cart_id) into ws_cart_id
from sc_cart
where arg_cart_id = cart_id;
/*cart check*/
select max(cart_status) into ws_cart_status
from sc_cart
where arg_cart_id = cart_id;
if ws_cart_id is null
then raise alreadycancelled;
end if;
open cancel;
LOOP
fetch cancel into ws_prod_id, ws_item_quantity_in_cart;
UPDATE sc_product p SET prod_quan_avail = prod_quan_avail + ws_quantity_in_cart,
prod_quan_sold = prod_quan_sold - ws_quantity_in_cart
WHERE prod_id = ws_prod_id;
DELETE FROM sc_items i WHERE i.item_cart_id = arg_cart_id;
END LOOP;
close cancel;
DELETE FROM sc_cart c WHERE c.cart_id = arg_cart_id;
EXCEPTION
when alreadycancelled
then raise_application_error(-20400, 'Cart does not exist');
END;
我需要此代码来取消购物车并且return所有商品数量恢复到原始库存。
除了用数据类型定义 ws_quantity_in_cart
变量外,您的代码块没有太多遗漏的部分,并为已包含的其他类型的异常添加更多处理 (ORA-20400
)
CREATE OR REPLACE PROCEDURE CANCELCART( arg_cart_id sc_cart.cart_id%type ) IS
ws_prod_id sc_product.prod_id%type;
ws_item_quantity_in_cart sc_product.prod_quan_avail%type;
ws_cart_id sc_cart.cart_id%type;
ws_cart_status sc_cart.cart_status%type;
ws_quantity_in_cart sc_product.prod_quan_sold%type;
cursor cancel IS
select item_product_id, item_quantity_in_cart
from sc_items i
where item_cart_id = arg_cart_id
group by item_product_id;
alreadycancelled exception;
BEGIN
select max(cart_id)
into ws_cart_id
from sc_cart
where arg_cart_id = cart_id;
/*cart check*/
select max(cart_status)
into ws_cart_status
from sc_cart
where arg_cart_id = cart_id;
if ws_cart_id is null
then raise alreadycancelled;
end if;
open cancel;
loop
fetch cancel into ws_prod_id, ws_item_quantity_in_cart;
update sc_product p
set prod_quan_avail = prod_quan_avail + ws_quantity_in_cart,
prod_quan_sold = prod_quan_sold - ws_quantity_in_cart
where prod_id = ws_prod_id;
delete sc_items i where i.item_cart_id = arg_cart_id;
end loop;
close cancel;
delete sc_cart c where c.cart_id = arg_cart_id;
exception when alreadycancelled then raise_application_error(-20400, 'Cart does not exist');
when others then raise_application_error(SQLCODE,SQLERRM);
END;
优先使用<table_name>.<column_name>.%type
使用列的原始数据类型定义变量的数据类型,不使用char
类型的定长字符串类型变量,改用varchar2
.
我有一个小组项目,我们正在其中创建一个购物车应用程序。我得到了一些反馈并修复了它,把它变成了一个带有光标的过程。但是,我们仍然遇到我不知道如何修复的错误,例如尝试将空值放入不能为空的 table 列中。我不知道为什么它认为我试图输入一个空值。我们的项目明天到期,请帮忙!
起初我尝试将其作为触发器,但它不起作用,所以建议将其转换为带有游标的过程,所以我就这样做了。
create or replace PROCEDURE CANCELCART (arg_cart_id IN number)
IS
ws_prod_id number;
ws_item_quantity_in_cart number;
ws_cart_id number;
ws_cart_status char;
cursor cancel IS
select item_product_id, item_quantity_in_cart
FROM sc_items i
WHERE item_cart_id = arg_cart_id
group by item_product_id;
alreadycancelled exception;
BEGIN
select max(cart_id) into ws_cart_id
from sc_cart
where arg_cart_id = cart_id;
/*cart check*/
select max(cart_status) into ws_cart_status
from sc_cart
where arg_cart_id = cart_id;
if ws_cart_id is null
then raise alreadycancelled;
end if;
open cancel;
LOOP
fetch cancel into ws_prod_id, ws_item_quantity_in_cart;
UPDATE sc_product p SET prod_quan_avail = prod_quan_avail + ws_quantity_in_cart,
prod_quan_sold = prod_quan_sold - ws_quantity_in_cart
WHERE prod_id = ws_prod_id;
DELETE FROM sc_items i WHERE i.item_cart_id = arg_cart_id;
END LOOP;
close cancel;
DELETE FROM sc_cart c WHERE c.cart_id = arg_cart_id;
EXCEPTION
when alreadycancelled
then raise_application_error(-20400, 'Cart does not exist');
END;
我需要此代码来取消购物车并且return所有商品数量恢复到原始库存。
除了用数据类型定义 ws_quantity_in_cart
变量外,您的代码块没有太多遗漏的部分,并为已包含的其他类型的异常添加更多处理 (ORA-20400
)
CREATE OR REPLACE PROCEDURE CANCELCART( arg_cart_id sc_cart.cart_id%type ) IS
ws_prod_id sc_product.prod_id%type;
ws_item_quantity_in_cart sc_product.prod_quan_avail%type;
ws_cart_id sc_cart.cart_id%type;
ws_cart_status sc_cart.cart_status%type;
ws_quantity_in_cart sc_product.prod_quan_sold%type;
cursor cancel IS
select item_product_id, item_quantity_in_cart
from sc_items i
where item_cart_id = arg_cart_id
group by item_product_id;
alreadycancelled exception;
BEGIN
select max(cart_id)
into ws_cart_id
from sc_cart
where arg_cart_id = cart_id;
/*cart check*/
select max(cart_status)
into ws_cart_status
from sc_cart
where arg_cart_id = cart_id;
if ws_cart_id is null
then raise alreadycancelled;
end if;
open cancel;
loop
fetch cancel into ws_prod_id, ws_item_quantity_in_cart;
update sc_product p
set prod_quan_avail = prod_quan_avail + ws_quantity_in_cart,
prod_quan_sold = prod_quan_sold - ws_quantity_in_cart
where prod_id = ws_prod_id;
delete sc_items i where i.item_cart_id = arg_cart_id;
end loop;
close cancel;
delete sc_cart c where c.cart_id = arg_cart_id;
exception when alreadycancelled then raise_application_error(-20400, 'Cart does not exist');
when others then raise_application_error(SQLCODE,SQLERRM);
END;
优先使用<table_name>.<column_name>.%type
使用列的原始数据类型定义变量的数据类型,不使用char
类型的定长字符串类型变量,改用varchar2
.