PL / SQL需要一些方向

时间:2015-11-13 02:51:00

标签: oracle plsql sqlplus

这是一个问题:您想确定在您需要花费的金额上可以购买多少特定商品。显然,您需要确定物品价格以及您需要花多少钱。屏幕上的结果应该告诉您可以购买多少件商品。

这是我创建的表格:

SQL> select *
 2  from product2;

 PIDNO PNAME                PCOST
---------- --------------- ----------
  1111 Pencil                   1
  2222 Soda                     5
  3333 Cards                    2

这是代码:

       SET SERVEROUTPUT ON
     ACCEPT input_money PROMPT 'Enter how much money you have: '
  DECLARE
v_pidno product2.pidno%TYPE;
v_pname product2.pname%TYPE;
v_pcost product2.pcost%TYPE;
v_money number(3);
 BEGIN
 SELECT pidno, pname, pcost
INTO v_pidno, v_pname, v_pcost
FROM product2
WHERE pidno = v_pidno;
V_money :=v_money/v_pcost;
 dbms_output.put_line ('How much you can buy is: ' ||v_money);
END;
 /
  SET SERVEROUTPUT OFF

我也有这样编写的代码:

  SET SERVEROUTPUT ON
  ACCEPT input_pidno PROMPT 'Enter product ID: '
  DECLARE
  v_pidno product2.pidno%TYPE;
  v_pname product2.pname%TYPE;
 v_pcost product2.pcost%TYPE;
 v_money number(3);
 BEGIN
   SELECT pidno, pname, pcost
 INTO v_pidno, v_pname, v_pcost
 FROM product2
 WHERE pidno = &input_pidno;
 V_money :=v_money/v_pcost;
 dbms_output.put_line ('How much you can buy is: ' ||v_money); 
END;
/
SET SERVEROUTPUT

我希望能够输入我有多少钱和一个pidno,它告诉我我能买多少钱。我只是不知道如何重新说出这段代码。

1 个答案:

答案 0 :(得分:1)

您正在接受货币数量并将其置于PIDNO中。这可能不是你想要的。尝试类似:

SET SERVEROUTPUT ON

ACCEPT input_money PROMPT 'Enter how much money you have: '
ACCEPT input_pidno PROMPT 'Enter the product ID you want: '

DECLARE
  v_pidno         product2.pidno%TYPE;
  v_pname         product2.pname%TYPE;
  v_pcost         product2.pcost%TYPE;
  v_items_to_buy  number(3);
BEGIN
  SELECT pidno, pname, pcost
    INTO v_pidno, v_pname, v_pcost
    FROM product2
    WHERE pidno = &input_pidno;

  v_items_to_buy := &input_money / v_pcost;

  dbms_output.put_line ('You can buy ' || v_items_to_buy || ' ' || v_pname); 
END;
/
SET SERVEROUTPUT OFF

祝你好运。