如何在匿名块中使用函数?

时间:2015-07-20 20:56:04

标签: sql plsql oracle11g

问题

我想使用' calculate_bill_for_order' 创建的功能。我想在我的匿名块中使用它。当我运行匿名阻止时,应提示用户输入食品订单号' orderno' ,然后应计算食品订单总数。我创建的块不起作用,我不知道为什么。

匿名阻止

SET SERVEROUTPUT ON
SET VERIFY OFF
ACCEPT total_order_no PROMPT 'Enter order number to calculate total'
DECLARE
total_order_no varchar2(6) := &total_order_no;
total integer;

BEGIN
  SELECT CALCULATE_BILL_FOR_ORDER(ORDER_CODE) INTO total
  FROM customer_order
  WHERE order_code = total_order_no;

END;
/

功能
工作正常

CREATE OR REPLACE FUNCTION calculate_bill_for_order
    (order_code  IN  customer_order.orderno%TYPE) 
  RETURN number IS
    total_income number := 0;


  CURSOR food_order_cursor  IS
    SELECT dish.price, food_order.n_portions
  FROM food_order, dish
  WHERE food_order.orderno = order_code
  and food_order.dishid = dish.dishid;
  food_record food_order_cursor%ROWTYPE;


  CURSOR drink_order_cursor IS
  SELECT drink.price, drink_order.n_units
  FROM drink, drink_order
  WHERE drink_order.orderno = order_code AND drink_order.drinkid = drink.drinkid;
  drink_record drink_order_cursor%ROWTYPE; 


BEGIN
    OPEN food_order_cursor;
    LOOP
        FETCH food_order_cursor INTO food_record;
        EXIT WHEN food_order_cursor%NOTFOUND;
    total_income := total_income + (food_record.price * food_record.n_portions);
    END LOOP; 
    CLOSE food_order_cursor;


  OPEN drink_order_cursor;
    LOOP
        FETCH drink_order_cursor INTO drink_record;
        EXIT WHEN drink_order_cursor%NOTFOUND;
    total_income := total_income + (drink_record.price * drink_record.n_units);
    END LOOP; 
    CLOSE drink_order_cursor;
    RETURN total_income;
END;
/

更新

嘿谢谢你的回复。 这是我得到的错误:

SET SERVEROUTPUT ON
SET VERIFY OFF
ACCEPT total_order_no PROMPT 'Enter order number to calculate total'
DECLARE
total_order_no varchar2(6) := '&total_order_no';
total integer;
BEGIN
  SELECT CALCULATE_BILL_FOR_ORDER(ORDER_CODE) INTO total
  FROM customer_order
  WHERE order_code = total_order_no;

END;
/

Error report -
ORA-06550: line 7, column 9:
PL/SQL: ORA-00904: "ORDER_CODE": invalid identifier
ORA-06550: line 5, column 3:
PL/SQL: SQL Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
Elapsed: 00:00:00.031

**This is your code:**
enter code here
Error report -
ORA-06550: line 2, column 31:
PLS-00201: identifier 'O00001' must be declared
ORA-06550: line 2, column 16:
PL/SQL: Item ignored
ORA-06550: line 4, column 30:
PLS-00302: component 'ORDER_CODE' must be declared
ORA-06550: line 4, column 15:
PL/SQL: Item ignored
ORA-06550: line 13, column 22:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 13, column 22:
PL/SQL: ORA-00904: "TOTAL_ORDER_NO": invalid identifier
ORA-06550: line 11, column 3:
PL/SQL: SQL Statement ignored
ORA-06550: line 15, column 37:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 15, column 3:
PL/SQL: Statement ignored
ORA-06550: line 19, column 47:
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 19, column 7:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
Elapsed: 00:00:00.031

**This is your code that I slightly modified:**

SET SERVEROUTPUT ON
SET VERIFY OFF
ACCEPT total_order_no PROMPT 'Enter order number to calculate total'
DECLARE
total_order_no varchar2(6) := &total_order_no;
total number;
v_order_code  customer_order.orderno%type;
BEGIN
  SELECT ORDErno INTO v_order_code
  FROM customer_order
  WHERE orderno = total_order_no AND orderno = order_code;
  total := CALCULATE_BILL_FOR_ORDER(v_order_code);
  EXCEPTION
    WHEN OTHERS THEN
      dbms_output.put_line('No such order' || total_order_no || ' - ' || sqlcode);
END;
/

Orderno is an attribute in Customer_order table AND order_code is a substitution used in calculate_bill_for_order function

1 个答案:

答案 0 :(得分:0)

你应该发布你到达那里的错误,这将有助于我们了解正在发生的事情。 但是,试图找出你在寻找什么,在我看来,这不是执行该行动的正确方法。您可以先获取订单,然后将该订单注入该功能,最后您将获得总额。也许这有帮助:

variable defined: TEST_RUNNER=C:\GrowthEdition.QA\fitnesse\fitSharp\Runner.exe 

希望这有帮助!!。