问题更新在PostgreSQL中选择Case With Function

时间:2017-05-05 14:22:57

标签: postgresql

我有一个问题是用PostgreSQL数据库中的函数更新数据。我确实更新了tbltemp表,它在第1行得到了很好的结果,但在第2行它没有更新,sql代码和表格如下:

tblbuy_transactionhdr表:

-----------------------------------------------------------------------------
header_id|transaction_type|account_no|accounts_code|amount|type
-----------------------------------------------------------------------------
BTR100001|Credit          |1700001 |           201 |60000 | 103
-----------------------------------------------------------------------------

tbltemp table:

 -----------------------------------------------------------------------------
    temp_id|accounts_code|accounts_name|description|debit|credit
    -----------------------------------------------------------------------------
    1      |103        |Inventory     |BTR1000001 |   600 |  0
    -------------------------------------------------------------------------
    2      |101        |Cash          |BTR1000001 |     0 |600 
    -------------------------------------------------------------------------

这个更新tbltemp表的函数:

IF TG_OP = 'UPDATE' THEN 
//this for update 1st row and the result is ok 
update tbltemp set debit=new.sub_amount, credit=0, 
temp_status=temp_status where description=new.header_id and accounts_code 
= '103' and temp_id=temp_id;

//this for update 2nd rows but this is the problem       
update tbltemp set 
accounts_code = (SELECT CASE WHEN transaction_type='Cash' THEN  '101'
         ELSE '201'
         END AS accounts_code from tblbuy_transactionhdr where 
         transaction_type=transaction_type order by header_id desc LIMIT 1),

accounts_name= (SELECT CASE WHEN transaction_type='Cash' THEN 'Cash'
        ELSE 'Account Payable'       
        END AS accounts_name from tblbuy_transactionhdr  where 
        transaction_type=transaction_type order by header_id desc LIMIT 1),

        debit=0, credit=new.sub_amount, temp_status=temp_status where accounts_code=(SELECT CASE WHEN transaction_type='Cash' THEN '101'
        ELSE '201'
        END AS accounts_code from tblbuy_transactionhdr where 
        transaction_type=transaction_type order by header_id desc LIMIT 1) 
        and description=new.header_id and temp_id=temp_id;  
        return new;
        END IF;
        end;

当我使用transaction_type ='来信息'更新tblbuy_transaction上的数据时,我想更新第二行那么resuslt就是accounts_code =' 201'和accounts_name ='应付帐款'但它不起作用,功能脚本有什么问题?谢谢。

0 个答案:

没有答案