标签:
/*新建一张account表*/
CREATE TABLE account( account_id VARCHAR(20), account_name VARCHAR(50) )
/*新建结果表*/
CREATE TABLE orders( account_id VARCHAR(20) NULL, ordered_on DATE DEFAULT SYSDATE, sales INT NULL)
/*自定义汇总函数*/
CREATE OR REPLACE FUNCTION total_sales(id_in IN account.account_id%TYPE) RETURN NUMBER IS DECLARE CURSOR tot_cur IS SELECT SUM(sales) total FROM orders WHERE account_id = id_in AND TO_CHAR(ordered_on,‘YYYY‘) = TO_CHAR(SYSDATE,‘YYYY‘); tot_rec tot_cur%ROWTYPE; BEGIN OPEN tot_cur; FETCH tot_cur INTO tot_rec; RETURN tot_rec.total; END;
/*向结果表中插入两条数据*/
INSERT INTO orders(account_id,ordered_on,sales) values(‘1041301063‘,TO_DATE(‘20150809‘,‘yyyymmdd‘),25); INSERT INTO orders(account_id,ordered_on,sales) values(‘1041301063‘,TO_DATE(‘20150810‘,‘yyyymmdd‘),12);
标签:
原文地址:http://my.oschina.net/u/1866459/blog/507946