我可以为update语句中的变量赋值吗?

时间:2013-12-09 12:15:12

标签: sql oracle

我是Oracle的新手,我想知道,如果我可以为update statement case内的变量赋值。

到目前为止我有这个代码:

PROCEDURE insert_repayment_data_inst(

  date_of_payment_l             IN credit_mo_repayment.date_of_payment%TYPE,
  amoount_to_pay_l              IN credit_mo_repayment.ammount_to_pay%TYPE,
  client_number_l               IN credit_mo_repayment.client_number%TYPE,
  is_paid_l                     IN credit_mo_repayment.is_paid%TYPE, 
  month_l                       IN credit_mo_repayment.is_paid%TYPE,
  year_l                        IN credit_mo_repayment.is_paid%TYPE,
  ending_balance_l              IN credit_mo_repayment.ending_balance%TYPE,
  tests                         IN credit_mo_repayment.ending_balance%TYPE
) AS
  BEGIN

  select monthly_installment into tests
  from credit_mo_repayment
   WHERE (to_char(date_of_payment, 'mm') = to_char(to_date(date_of_payment_l), 'mm'))
         AND (to_char(date_of_payment, 'yy') = to_char(to_date(date_of_payment_l), 'yy'));

  UPDATE credit_mo_repayment
  SET

  monthly_installment = CASE 
                          WHEN (Monthly_installment - amoount_to_pay_l >= 0)  THEN Monthly_installment - amoount_to_pay_l
                          WHEN (Monthly_installment - amoount_to_pay_l < 0) THEN 0
                        END,

  ending_balance = CASE
                     WHEN (Monthly_installment - amoount_to_pay_l < 0) THEN ending_balance - ABS(Monthly_installment - amoount_to_pay_l) tests:= ending_balance - ABS(Monthly_installment - amoount_to_pay_l)
                     WHEN (Monthly_installment - amoount_to_pay_l >= 0)  THEN ending_balance
                   END,
  ammount_to_pay = ammount_to_pay + amoount_to_pay_l
  WHERE (to_char(date_of_payment, 'mm') = to_char(to_date(date_of_payment_l), 'mm'))
         AND (to_char(date_of_payment, 'yy') = to_char(to_date(date_of_payment_l), 'yy'));                                  
END insert_repayment_data_inst;

正如您所看到的,我正在尝试将值分配给案例中的变量 tests:= ending_balance - ABS(Monthly_installment - amoount_to_pay_l)

但没有运气。

我还尝试将monthly_installment值分配给tests变量,然后只是为了进行数学运算,但我也没有运气。

 select monthly_installment into tests
 from credit_mo_repayment
 WHERE (to_char(date_of_payment, 'mm') = to_char(to_date(date_of_payment_l), 'mm'))
        AND (to_char(date_of_payment, 'yy') = to_char(to_date(date_of_payment_l), 'yy'));

我知道我在语法中遗漏了一些内容,但我无法发现它。

2 个答案:

答案 0 :(得分:1)

首先,您必须将目标变量定义为OUT - 参数或新变量。例如:

PROCEDURE insert_repayment_data_inst(

  date_of_payment_l             IN credit_mo_repayment.date_of_payment%TYPE,
  amoount_to_pay_l              IN credit_mo_repayment.ammount_to_pay%TYPE,
  client_number_l               IN credit_mo_repayment.client_number%TYPE,
  is_paid_l                     IN credit_mo_repayment.is_paid%TYPE, 
  month_l                       IN credit_mo_repayment.is_paid%TYPE,
  year_l                        IN credit_mo_repayment.is_paid%TYPE,
  ending_balance_l              IN credit_mo_repayment.ending_balance%TYPE,
  tests                         IN credit_mo_repayment.ending_balance%TYPE
) AS
-- new variable
  monthly_payments_output credit_mo_repayment.ending_balance%TYPE;
  BEGIN

第二:您可以尝试RETURNING - 子句,如下所述: http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/collections.htm#BABHDGIG和这里 http://docs.oracle.com/cd/B19306_01/appdev.102/b14261/sqloperations.htm#BABEBDIA
哪个会产生类似的结果(未经测试):

UPDATE credit_mo_repayment
  SET

  monthly_installment = CASE 
                          WHEN (Monthly_installment - amoount_to_pay_l >= 0)  THEN Monthly_installment - amoount_to_pay_l
                          WHEN (Monthly_installment - amoount_to_pay_l < 0) THEN 0
                        END,

  ending_balance = CASE
                     WHEN (Monthly_installment - amoount_to_pay_l < 0) THEN ending_balance - ABS(Monthly_installment - amoount_to_pay_l)
                     WHEN (Monthly_installment - amoount_to_pay_l >= 0)  THEN ending_balance
                   END,
  ammount_to_pay = ammount_to_pay + amoount_to_pay_l
  WHERE (to_char(date_of_payment, 'mm') = to_char(to_date(date_of_payment_l), 'mm'))
         AND (to_char(date_of_payment, 'yy') = to_char(to_date(date_of_payment_l), 'yy'))
RETURNING monthly_installment INTO tests;

答案 1 :(得分:1)

你可以这样做:

UPDATE credit_mo_repayment SET
monthly_installment = 
    CASE 
    WHEN (Monthly_installment - amoount_to_pay_l >= 0) THEN Monthly_installment - amoount_to_pay_l
    WHEN (Monthly_installment - amoount_to_pay_l < 0) THEN 0
    END,
ending_balance = 
    CASE
    WHEN (Monthly_installment - amoount_to_pay_l < 0) THEN ending_balance - ABS(Monthly_installment - amoount_to_pay_l) 
    WHEN (Monthly_installment - amoount_to_pay_l >= 0) THEN ending_balance
    END,
ammount_to_pay = ammount_to_pay + amoount_to_pay_l
WHERE (TO_CHAR(date_of_payment, 'mm') = TO_CHAR(TO_DATE(date_of_payment_l), 'mm'))
    AND (TO_CHAR(date_of_payment, 'yy') = TO_CHAR(TO_DATE(date_of_payment_l), 'yy'))
RETURNING  
    CASE
   WHEN (Monthly_installment - amoount_to_pay_l < 0) THEN ending_balance - ABS(Monthly_installment - amoount_to_pay_l)
    ELSE NULL
    END         
INTO tests;

未测试 - 我不确定RETURNING子句中是否允许使用CASE表达式。