这是我的功能比较:
DROP FUNCTION IF EXISTS compra(customers.customerid%TYPE, prods integer ARRAY,orderlines.quantity%TYPE) CASCADE;
CREATE OR REPLACE FUNCTION compra(c_id customers.customerid%TYPE,prods integer ARRAY, quant integer ARRAY)
RETURNS void AS $$
DECLARE
t_price products.price%TYPE:= 0;
p_price products.price%TYPE;
tax_c CONSTANT numeric:=0.23;
ord_id orders.orderid%TYPE;
ord_date orders.orderdate%TYPE:= current_date;
counter INT:= array_length(prods, 1);
init INT:= 1;
BEGIN
SELECT MAX(ord.orderid) INTO ord_id
FROM orders AS ord;
ord_id:= ord_id + 1;
INSERT INTO orders(orderid,orderdate,customerid, netamount, tax, totalamount) values(ord_id, ord_date, c_id, 0, tax_c, 0);
WHILE init <= counter
LOOP
INSERT INTO orderlines(orderlineid, orderid, prod_id, quantity, orderdate)
VALUES(init,ord_id, prods[init], quant[init], ord_date);
INSERT INTO cust_hist(customerid, orderid, prod_id)
VALUES(c_id, ord_id, prods[init]);
SELECT price INTO p_price
FROM products
WHERE prod_id=prods[init];
t_price:=p_price*quant[init];
UPDATE orders SET
netamount=netamount + t_price,
totalamount =totalamount + t_price+(t_price * tax_c)
WHERE orderid = ord_id;
UPDATE inventory SET
quan_in_stock = quan_in_stock - quant[init],
sales = sales + quant[init]
WHERE prod_id =prods[init];
if (quant[init] > 0) then
PERFORM top_5();
end if;
init:=init+1;
END LOOP;
END;
$$ LANGUAGE plpgsql;
我的函数top_5:
DROP FUNCTION IF EXISTS top_5(customers.customerid%TYPE, products.prod_id%TYPE, orderlines.quantity%TYPE) CASCADE;
CREATE OR REPLACE FUNCTION top_5(c_id customers.customerid%TYPE, p_id products.prod_id%TYPE, quant orderlines.quantity%TYPE)
RETURNS void AS $$
DECLARE
ord_id orders.orderid%TYPE;
ord_date orders.orderdate%TYPE:= current_date;
ordln_id orderlines.orderlineid%TYPE:=1;
top_prod_id products.prod_id%TYPE;
top_prod CURSOR IS
SELECT inv.prod_id *
FROM inventory AS inv, products AS prod
WHERE inv.prod_id=prod.prod_id
ORDER BY inv.quan_in_stock desc, inv.sales
limit 5;
BEGIN
SELECT nova_orderid() INTO ord_id;
INSERT INTO orders(orderid, orderdate,customerid,netamount,tax,totalamount) VALUES(ord_id,ord_date,c_id,0,0.23,0);
PERFORM compra(c_id, p_id, quant, ord_id, ordln_id, ord_date);
OPEN top_prod;
LOOP
FETCH top_prod INTO top_prod_id;
IF (p_id = top_prod_id) THEN
UPDATE orders
SET netamount = netamount-(netamount*0.2)
WHERE ord_id = (SELECT MAX(ord_id) FROM orders);
UPDATE orders
SET totalamount = netamount+(netamount*tax)
WHERE ord_id = (SELECT MAX(ord_id) FROM orders);
ELSE EXIT;
END IF;
END LOOP;
CLOSE top_prod;
END;
$$ LANGUAGE plpgsql;
错误:
错误:函数top_5()不存在 第1行:SELECT top_5() ^ 提示:没有函数匹配给定的名称和参数类型。您可能需要添加显式类型转换。
我想在我买东西时对最库存的前5种产品打折