我想尝试两个参数。客户ID和购买金额。购买金额不在我将参考的表格中。它将与分配给所述客户ID的信用额度进行比较,并且对于允许的金额或其罚款,信用额度过低而导致的msms输出为ethier。
我无法实施购买金额参数的程序,并将其与表格中的实际信用额度进行比较
create or replace PROCEDURE check_available_credit(
c_cust_id IN demo_customer.custid%TYPE,
c_purchase_amount IN NUMBER
) AS c_credit_check VARCHAR(50);
climit demo_customer.creditlimit%type;
BEGIN
SELECT climit
INTO c_credit_check--PLACE INTO PROCEDURE
FROM demo_customer
WHERE custid = c_cust_id;
if(c_purchase_amount > climit)
THEN dbms_output.put_line('Amount is too high');
elsif(c_purchase_amount < climit)
THEN dbms_output.put_line('Amount is perfect');
COMMIT;
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END check_available_credit;
我将使用100作为custid,使用4000作为购买金额。这将拉出一个信用额度为5000的客户的记录,因此它应该报告“金额是完美的”
您在select语句中错误地使用了climit
。我不确定为什么你使用变量c_credit_check
。
以下程序似乎适用于上述变更。对于异常块,我添加了一个dbms_output
来显示错误堆栈。除非您的程序正文中有其他DML,否则不需要Rollback
。理想情况下,Commit应位于调用部分中,如果没有DML,则可以将其删除
测试表/数据
create table demo_customer( custid int,creditlimit number );
insert into demo_customer(custid,creditlimit) values (100,5000);
程序
CREATE OR REPLACE PROCEDURE check_available_credit(
c_cust_id IN demo_customer.custid%TYPE,c_purchase_amount IN NUMBER
) AS
climit demo_customer.creditlimit%TYPE;
BEGIN
SELECT creditlimit
INTO climit
FROM demo_customer
WHERE custid = c_cust_id;
IF(c_purchase_amount > climit )
THEN dbms_output.put_line('Amount is too high');
ELSIF ( c_purchase_amount < climit ) THEN
dbms_output.put_line('Amount is perfect');
--COMMIT; --not needed if there are no dmls, move this to calling
--block if there are any.
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK; --not needed unless there's a dml
dbms_output.put_line(dbms_utility.format_error_stack);
END check_available_credit;
执行
SET SERVEROUTPUT ON
BEGIN
check_available_credit(100,4000);
END;
/
Amount is perfect
PL/SQL procedure successfully completed.