【发布时间】:2017-10-19 03:59:10
【问题描述】:
我有一个临时表,其中的数据是通过 sqlldr 加载的。临时表中的数据添加如下:
PHONE ADDRESS ORDER_ID
9971410000 D-166 GF 1
9910020000 H-12D 2
9873120000 K-19C 3
9811120000 J-200 4
9873120000 K-19C 5
9810320000 N-29B 6
9810390000 J-235 GF 7
9873500000 M-39 8
8447910000 J-62 GF 9
9873120000 K-19C 10
数据插入到'temp'表后,触发器调用一个过程插入到其他表中:
create or replace TRIGGER insert_customer
AFTER INSERT ON temp
FOR EACH ROW
BEGIN
insert_address(:new.phno, :new.addr, :new.ord_id);
END;
程序如下:
create or replace procedure INSERT_ADDRESS(
p_cust_phone customer.cust_phone%type,
p_address address.address%type,
p_ord_id customer_order.order_id%type
)
is
l_cust_id customer.cust_id%type;
l_address_id address.address_id%type;
begin
begin
select cust_id
into l_cust_id
from customer c
where c.cust_phone=p_cust_phone;
exception
when no_data_found then
insert into customer
values (cust_id_seq.nextval, 'No Name', p_cust_phone)
returning cust_id into l_cust_id;
end;
select address_id
into l_address_id
from address a
where a.address=p_address;
exception
when no_data_found then
insert into address
values (address_id_seq.nextval, UPPER(p_address), UPPER(p_area))
returning address_id into l_address_id;
insert into customer_address
values (l_cust_id, l_address_id);
insert into customer_order (order_id, cust_id)
values (p_ord_id,l_cust_id);
end;
CUSTOMER_ORDER 表中插入的数据不正确。下面列出了我在以下查询中得到的结果:
select * from customer_order;
ord_id cust_id
1 1
2 2
3 3
4 4
6 5
7 6
8 7
9 8
order_id (5,10) 丢失,因为它们与 ord_id=3 来自同一客户。同一客户的多个订单不会添加到 customer_order 表中。我无法理解我在哪里犯了错误?
【问题讨论】:
标签: stored-procedures oracle11g