0

此过程代码用于对新插件应用折扣。我遇到的问题是正在获取太多记录。我将如何解决这个问题?我使用触发器还是函数?

好的,我已经创建了一个触发器,但我得到了 PLS-00103:在预期以下情况之一时遇到符号“=”::=。(@%;

CREATE OR REPLACE PROCEDURE CHECK_DISCOUNT
AS

V_COUNT NUMBER;
V_C_NO APPOINTMENT.C_NO%TYPE;
V_BILL APPOINTMENT.BILL%TYPE;

BEGIN

SELECT C_NO,COUNT(C_NO)
INTO V_C_NO,V_COUNT
FROM APPOINTMENT
GROUP BY C_NO;

SELECT BILL
INTO V_BILL
FROM APPOINTMENT;

IF V_COUNT=3 THEN
V_BILL:=V_BILL * 0.9;
END IF;

UPDATE APPOINTMENT
SET BILL = V_BILL 
WHERE C_NO=:new.C_NO;
COMMIT;

END;
/

Create or replace TRIGGER CHECK_DISCOUNT 
BEFORE INSERT OR UPDATE OF C_NO ON APPOINT     
FOR EACH ROW 

DECLARE CURSOR C_APPTMENT IS 
SELECT C_NO,COUNT(C_NO) 
FROM APPOINTMENT GROUP BY C_NO; 

VISIT NUMBER; V_C_NO APPOINTMENT.C_NO%TYPE; 
V_BILL APPOINTMENT.BILL%TYPE; 
V_TEN NUMBER(3):=0.9; 

BEGIN 
   LOOP 
            OPEN C_APPTMENT; FETCH C_APPTMENT INTO V_C_NO, VISIT;
            EXIT WHEN C_APPTMENT%NOTFOUND; 

  SELECT BILL INTO V_BILL FROM APPOINTMENT; 
  IF VISITS =3 THEN V_BILL=V_BILL * V_TEN 
  WHERE :NEW.C_NO=V_C_NO; 
  UPDATE APPOINTMENT SET BILL:= V_BILL 
  WHERE:NEW.C_NO=V_C_NO; 
    END LOOP; 
      CLOSE C_APPTMENT; 

结尾;

4

1 回答 1

2

使用 select ... into 时,您必须确保只选择一行。通常,您将添加带有主键或唯一键列的 where 子句。这样您就可以确保只选择一行。您还必须提供未选择行的实例。添加异常处理程序以捕获 no_data_found 异常

declare
   v_bill appointment.bill%type;
begin
   begin
      select a.bill
        into v_bill
        from appointment a
       where a.id = :my_id;  -- assuming ID is a primary key colomn
   exception
      when no_data_found then
         null;
   end;

   if v_bill is not null
   then
      --.. do something with v_bill
   end if;
end;
于 2012-11-14T11:54:55.953 回答