由于open cr_count行出错,后续程序无效。当我运行没有这一行的代码时,它工作得很好。此外,我试图编写查询没有光标,如选择即使这样,程序没有工作。我没有写mysql错误打印为oracle sqlerrm,我认为mysql没有那个功能。
delimiter $$
create procedure proc_payment(
in p_transaction_guid varchar(200),
in p_card_type_id int,
in p_card_number varchar(100),
in p_billing_address varchar(500),
in p_flight_id int,
in p_flight_date date,
in p_cabin_class_id int,
in p_identity_number varchar(200)
)
begin
declare v_count int;
declare v_payment_id int;
declare v_passenger_id int;
declare v_booking_ref_id int;
declare cr_count cursor for
select count(1)
from tbl_passenger
where identity_number = p_identity_number;
declare cr_passenger cursor for
select passenger_id
from tbl_passenger
where identity_number = p_identity_number;
open cr_count;
fetch cr_count into v_count;
close cr_count;
if v_count = 0 then
insert into tbl_passenger (
firstname,
lastname,
gender,
birthdate,
identity_number
)
select
firstname,
lastname,
gender_id,
birthdate,
identity_number
from
v_booking_transaction
where
transaction_guid = p_transaction_guid
and identity_number = p_identity_number;
select last_insert_id() into v_passenger_id;
else
open cr_passenger;
fetch cr_passenger into v_passenger_id;
close cr_passenger;
end if;
insert into tbl_payment (
card_type_id,
card_number,
payment_date,
billing_address
) values (
p_card_type_id,
p_card_number,
current_date(),
p_billing_address
);
select last_insert_id() into v_payment_id;
insert into tbl_flight_booking (
flight_id,
flight_date,
cabin_class_id,
payment_id
) values (
p_flight_id,
p_flight_date,
p_cabin_class_id,
v_payment_id
);
select last_insert_id() v_booking_ref_id;
insert into tbl_flight_passenger (
flight_id,
booking_reference_id,
passenger_id
) values (
p_flight_id,
v_booking_ref_id,
v_passenger_id
);
end$$
答案 0 :(得分:0)
在这种情况下,也许可以避免使用游标。以下应该有效:
delimiter $$
create procedure proc_payment(
in p_transaction_guid varchar(200),
in p_card_type_id int,
in p_card_number varchar(100),
in p_billing_address varchar(500),
in p_flight_id int,
in p_flight_date date,
in p_cabin_class_id int,
in p_identity_number varchar(200)
)
begin
declare v_passenger_id,
v_payment_id,
v_booking_ref_id int;
select
passenger_id into v_passenger_id
from
tbl_passenger
where
identity_number = p_identity_number;
if v_passenger_id is null then
insert into tbl_passenger (
firstname,
lastname,
gender,
birthdate,
identity_number
)
select
firstname,
lastname,
gender_id,
birthdate,
identity_number
from
v_booking_transaction
where
transaction_guid = p_transaction_guid
and identity_number = p_identity_number;
set v_passenger_id := last_insert_id();
end if;
insert into tbl_payment (
card_type_id,
card_number,
payment_date,
billing_address
) values (
p_card_type_id,
p_card_number,
current_date(),
p_billing_address
);
set v_payment_id := last_insert_id();
insert into tbl_flight_booking (
flight_id,
flight_date,
cabin_class_id,
payment_id
) values (
p_flight_id,
p_flight_date,
p_cabin_class_id,
v_payment_id
);
set v_booking_ref_id := last_insert_id();
insert into tbl_flight_passenger (
flight_id,
booking_reference_id,
passenger_id
) values (
p_flight_id,
v_booking_ref_id,
v_passenger_id
);
end$$
delimiter ;