我正在使用Sql Developer和Oracle。
我需要在数据库中实施一个系统,这样一旦客户的供应中断,负责的人或企业 该帐单将无法在其他服务地址创建新帐户,直到 他们欠款已经清理的时间。这需要使用触发器或存储过程来实现。
以下是我的尝试
create or replace PROCEDURE NONEWACCOUNT(newcustomerID in number,
newStatus in varchar)
AS
begin
select
from a2_watermeter
where customerid = newcustomerid and Status = newStatus;
if (newStatus = 'Inactive') then
DBMS_OUTPUT.put_line('Can not make new account as customer has to pay his/or account');
end if;
end;
以下是我的数据库。
但是因为我对sql完全不熟悉,所以我不知道要改变什么
欢呼任何能够对此有所了解的人。
我的数据库
a2_METERREADER
- EMPLOYEEID
- FIRSTNAME
- LASTNAME
a2_READING
- READINGID
- METERID
- EMPLOYEEID
- BILLNUMBER
- READING
- DATERECORD
a2_Watermeter
- METERID
- ADDRESS
- SUBURB
- POSTCODE
- STATUS
- CUSTOMERID
- REPLACE
- INSTALLDATE
a2_customer
- customerid
- firstname
- lastname
- address
- suburb
- postcode
- email
- phone
- businessname
- dateofbirth
a2_bill
- billnumber
- address
- suburb
- postcode
- customerid
- readingid
- amount
- reading
- firstname
- lastname
- paid
- duedate
答案 0 :(得分:1)
您需要编写a stored procedure,而不是触发器。可以从a scheduled job调用存储过程,可能每天一次。触发器响应DML活动而触发,这是不可预测的。
您还没有提供太多信息,因此很难为您提供定义解决方案。除此之外,我们在SO上的角色不是为你做好自己的工作。但存储过程的核心将是这样的查询:
select *
from a2_bill
where paid = 'NO'
and duedate <= (sysdate - interval '60' day)
答案 1 :(得分:1)
CREATE OR REPLACE PROCEDURE
procname
IS
rec a2_watermeter%ROWTYPE;
CURSOR cur
IS
SELECT
*
FROM
a2_watermeter
WHERE
status = 'active'
FOR UPDATE;
BEGIN
OPEN cur;
LOOP
FETCH cur INTO rec;
EXIT WHEN cur%NOTFOUND;
IF (SYSDATE - (
SELECT MIN(duedate)
FROM a2_bill b
WHERE b.customerid = rec.customerid AND b.paid = FALSE
GROUP BY(customerid, paid)
)) > 60
THEN
UPDATE
a2_watermeter w
SET
status = 'inactive'
WHERE CURRENT OF cur;
END IF;
END LOOP;
CLOSE cur;
END;
答案 2 :(得分:1)
您可以创建具有计算值状态的视图。这可以在您的程序中使用并在其他地方使用。这样,业务逻辑就在一个地方 像这样:
create view customerstatus
select c.customerid
, decode(b.custimerid,null,'Active','Inactive') as status
from customer c
left outer join bill b on (b.customerid = c.custimerid)
where b.payed = 'N'
and b.duedate <= (sysdate - interval '60' day);