2011-04-08 3 views
0

시험에 대한 질문이 있지만 트리거 오류가 발생했습니다. 도와주세요.PL/SQL 트리거에서 오류가 발생합니까?

질문

고객 (CUST_ID, CUST_NAME, 주소) rent_info (CUST_ID, date_out, date_due_in, date_returned, 미세) rented_video (CUST_ID, no_of_videos)

Issuedate가해야 현재 날짜와 인해 날짜는 발급일로부터 7 일이어야합니다. 고객이 비디오를 반환하면 rent_info 테이블에 cust_id, date_out, date_due_in이 포함되어야합니다. 트리거는 rent_info 테이블에 데이터를 삽입하는 데 사용됩니다. 데이터를 삽입하기 전에 다음 유효성 검사를 수행해야합니다.

고객은 같은 날짜에 3 개 이상의 동영상을 찍을 수 없습니다. 비디오가 반환되면 재생 날짜가 업데이트됩니다. 그렇다면 벌금이 계산됩니다.

벌금 지연 RS의 제 사흘

  1. 하여 계산 일

  2. 당 지연 값 (Rs) (20)의 때라도 사흘에 대해 하루

  3. 당 10 6 일간의 벌금은 Rs 30 일당

업데이트 작업을 수행하기위한 프로 시저/트리거를 작성하십시오.

나는 이것을 이렇게 풀었다.

만들어진 세 개의 테이블 : UPD에 대한 책

create or replace procedure return_proc(c_id in int,d_ret in date) is 

val number(3) :=0; 

begin 

    update rented_video set no_vid=no_vid-1 where cust_id=c_id; 

    update rent_info set date_returned=d_ret where cust_id=c_id; 

    --insert into rent_info values(c_id,d_out,d_out+7,NULL,0); 

    update rented_video set no_vid=no_vid-1 where cust_id=c_id; 

    --val := select count(date_out) from rent_info where (date_out='12-jan-2010'); 

    --dbms_output.put_line('Values is '||val); 

end; 

/

트리거를 반환하는 책

create or replace procedure take_proc(c_id in int,d_out in date) is 

val number(3) :=0; 

begin 

    insert into rent_info values(c_id,d_out,d_out+7,NULL,0); 

    update rented_video set no_vid=no_vid+1 where cust_id=c_id; 

    --val := select count(date_out) from rent_info where (date_out='12-jan-2010'); 

    --dbms_output.put_line('Values is '||val); 

end; 

/

절차을 촬영

create table customer(

cust_id number(4), 

cust_name varchar2(8), 

address varchar2(8) 

); 



create table rent_info(

cust_id number(4), 

date_out date, 

date_due_in date, 

date_returned date, 

fine number(10) 

); 



create table rented_video(

cust_id number(4), 

no_vid number(4) 

); 

절차 책은

create or replace trigger ret_trig 

before update on rent_info 

for each row 

declare 

tfine number(7) := 0; 

temp number(7) := 0; 

rdate date; 

dudate date; 

cid number(4); 

begin 

    --select date_returned into rdate from rent_info; 

    --select date_due_in into dudate from rent_info; 

    --select cust_id into cid from rent_info; 

    --if (rdate- dudate) <=3 then 

     --temp := rdate- dudate; 

     --tfine := tfine+ temp * 10; 

    --end if; 

    if (:new.date_returned-:old.date_due_in) <=3 then 

     temp := :new.date_returned-:old.date_due_in; 

     tfine := tfine+ temp * 10; 

     dbms_output.put_line('Fine Values is '|| tfine); 

    elsif (:new.date_returned-:old.date_due_in) <=6 then 

     temp := :new.date_returned-:old.date_due_in; 

     tfine := tfine+ 3 * 10; 

     tfine := tfine+ 20*(temp-3); 

     dbms_output.put_line('Fine Values is '|| tfine); 

    else 

     temp := :new.date_returned-:old.date_due_in; 

     tfine := tfine+ 3 * 10; 

     tfine := tfine+ 3 * 20; 

     tfine := tfine+ 30*(temp-6); 

     dbms_output.put_line('Fine Values is '|| tfine); 

    end if; 



    --update rent_info set fine=fine+tfine where cust_id=:old.cust_id; 

end; 

/

를 반환 될 때 ating 미세 나는 제대로 벌금을 계산하지만 couldnt한다 (주석되고 그 갱신을 수행하는 트리거의 마지막 라인)이 rent_info 테이블을 업데이트 할 수 있습니다.

나는 트리거 생성에 논리적 실수를 저지른 것으로 생각합니다. 문제점을 올바르게 해결하는 방법을 알려주십시오.

샘플 값은 당신이 단순히 과제를 수행하여 컬럼의 값을 변경, 행 레벨 트리거 내부

insert into customer values(1,'john','abc h'); 

insert into customer values(2,'joseph','cde h'); 

insert into rented_video values(1,0); 

insert into rented_video values(2,0); 

exec take_proc(1,'12-jan-2010'); 

exec take_proc(2,'13-jan-2010'); 

exec return_proc(1,'16-jan-2010'); 
exec return_proc(2,'29-jan-2010'); 

답변

0

왜 방아쇠를 사용하고 있습니까? 이 절차를 개발하여 매우 명확한 API를 구축하지만 코드를 트리거로 사용합니다. 왜이 논리를 return_proc에 통합하여 더 명확하게 보이게할까요?

+0

이것은 시험 문제이며 질문은 "트리거가 rent_info 테이블에 데이터를 삽입하는 데 사용됩니다"라고 말합니다. – user567879

+0

트리거가 무엇인지 배우는 것이 좋겠지 만 실제로는 거의 사용하지 않아야합니다. –

3

를 삽입 : NEW - 당신은 UPDATE 문을 실행하지 않습니다. 예 ::

:NEW.fine := :OLD.fine + tfine;