0

我没有运气搜索了互联网。我已经附加了我正在处理的练习集的链接,但是我正在创建一个存储过程,该存储过程需要在检查一个表中的外键是否与另一个表中的相应主键匹配后更新一行。到目前为止,我会附上我的代码(不多),但我真的迷路了。我知道如何创建外键约束,例如:

ALTER TABLE DRIVE
ADD CONSTRAINT TRUCK_NUM_FK FOREIGN KEY (TRUCK_NUM) REFERENCES TRUCK;

但我不知道如何从存储过程中执行这些操作以及更多操作。谢谢!

练习说明: http: //tinypic.com/r/2djxq4w/8 http://tinypic.com/r/sq61i1/8

CREATE OR REPLACE PROCEDURE TRUCK_RETURN (TR_NUM IN NUMBER,TR_MILE IN NUMBER) AS
BEGIN
IF
UPDATE
4

1 回答 1

0

这可能不准确,但希望它能让你接近:

CREATE OR REPLACE PROCEDURE TRUCK_RETURN (TR_NUM IN NUMBER,TR_MILE IN NUMBER) AS
BEGIN
IF TR_NUM IS NULL THEN
    Dbms_Output.PUT_LINE('Error: No truck number supplied.');
ELSE
    DECLARE TR NUMBER(10);
    SELECT TRUCK_NUM 
    INTO TR
    FROM TRUCK 
    WHERE TR_NUM = TRUCK_NUM;

    IF TR IS NOT NULL AND TR = TR_NUM THEN
       SELECT Truck_Mileage
           INTO TR
       FROM Truck
       WHERE Truck_Num = TR_NUM;

            IF TR_MILE >= TR THEN
                SELECT COUNT(*)
                INTO TR
                FROM DRIVE
                WHERE Truck_Num = TR_NUM
                      AND Drive_Status = ‘OUT’;

                IF TR = 1 THEN
                   UPDATE Drive
                   SET Drive_Status = ‘Received’
                   WHERE Truck_Num = TR_NUM
                   AND Drive_Status = ‘OUT’;
                ELSE 
                   Dbms_Output.PUT_LINE('Error: Truck has too many or not record of being out.’);
                END IF;
            ELSE
               Dbms_Output.PUT_LINE('Error: Truck mileage is less than previously recorded.’);
            END IF;
      ELSE 
         Dbms_Output.PUT_LINE('Error: Truck Number is incorrect.’);
      END IF;
END IF;
END TRUCK_RETURN;
于 2014-05-07T02:06:17.590 回答