0

这个问题是在我大学的一次实践考试中提出的。问:表格具有以下列标题/列:

medicine_name,medicine_manufacture_date,medicine_expiry_date.

正如列头名称所描述的那样,它们中包含有关药物的相应数据。现在他们要求编写一个 pl/sql 块来列出所有已过期的药物名称(即当前系统日期大于药物的过期日期值)。我强烈认为必须将顺序连续整数列(如 1,2,3,...n)添加到表中,这将作为每个药物名称的主键。药物名称是唯一的,但要执行迭代他们应该拥有一个整数主键的操作。在不分配整数主键的情况下仍然可以获得结果吗?

我更改了表格并为每条记录分配了主键并编写了以下块...它有效,但我想在不分配主键的情况下这样做。所有可能的聪明方法都将受到崇拜!请建议更正我的代码中的任何类型......我的代码如下:

   declare
         a date;
         b date;
        diff number(10);
        medicine varchar(25);
   begin
              a:=sysdate;
                        for i in 1..5
               loop
                       select medicine_expiry_date into b from med_details where med_id=i ;
                            diff:=trunc(a-b);
                  if
                       diff>0  then
                            select medicine_name into medicine from med_details where med_id=i;
                                dbms_output.put_line(medicine);
                          end if;
             end loop;
  end;
  /
4

2 回答 2

1

尝试

BEGIN
  DBMS_OUTPUT.PUT_LINE('The following medicines have expired:');

  FOR aRow IN (SELECT MEDICINE_NAME
                 FROM MEDICINE
                 WHERE EXPIRY_DATE < SYSDATE)
  LOOP
    DBMS_OUTPUT.PUT_LINE(aRow.MEDICINE_NAME);
  END LOOP;
END;

如果需要让其他表引用该表并且没有其他实用的候选键可用,您可以在该表上放置一个人工主键,但不需要回答上述问题。

分享和享受。

于 2013-07-10T11:14:48.953 回答
0

它看起来很简单......我怀疑还有一些额外的细节。

/*
Q: A table has following columnheads/columns: 
medicine_name,
medicine_manufacture_date,
medicine_expiry_date.

Now they asked to write a pl/sql block to make list of all medicines' names which have expired
(i.e.current system date is greater than the expirydate values of the medicine).
*/

-- SQL:

SELECT medicine_name
FROM a_table
WHERE medicine_expiry_date < SYSDATE;

-- PL/SQL v.1:

DECLARE
    TYPE medicine_ntt IS TABLE OF a_table%ROWTYPE;
    l_medicine  medicine_ntt;
BEGIN
    SELECT  medicine_name
    ,       medicine_expiry_date
    BULK    COLLECT INTO l_medicine
    FROM    a_table;

    FOR indx IN 1..l_medicine.COUNT LOOP
        IF l_medicine(indx).medicine_expiry_date < SYSDATE THEN
            DBMS_OUTPUT.PUT_LINE(l_medicine(indx).medicine_name);
        END IF;
    END LOOP;
END;

-- PL/SQL v.2:

DECLARE
    CURSOR medicine_cur IS
        SELECT  medicine_name
        ,       medicine_expiry_date
        FROM    a_table;

    TYPE medicine_ntt IS TABLE OF medicine_cur%ROWTYPE;
    l_medicine         medicine_ntt;
    l_medicine_expired medicine_ntt := medicine_ntt();
BEGIN
    OPEN  medicine_cur;
    FETCH medicine_cur BULK COLLECT INTO l_medicine;
    CLOSE medicine_cur;

    FOR indx IN 1..l_medicine.COUNT LOOP
        IF l_medicine(indx).medicine_expiry_date < SYSDATE THEN
            l_medicine_expired.EXTEND;
            l_medicine_expired(l_medicine_expired.LAST) := l_medicine(indx);
        END IF;
    END LOOP;

    FOR indx IN 1..l_medicine_expired.COUNT LOOP
        DBMS_OUTPUT.PUT_LINE(l_medicine_expired(indx).medicine_name);
    END LOOP;
END;
于 2013-07-10T08:27:50.457 回答