Domanda

I am trying to create a trigger to automatically update the status level of users in my database. The status level is based on the amount of revenue a user generates. This is what I have come up with so far.

create or replace trigger update_hirer_status
after insert or update of payment_id, status_id on payments
for each row
declare 
user_id_temp number := 0;
total_amount number := 0;

begin
select external_users.external_user_id into user_id_temp
from external_users
join financial_accounts
using (external_user_id)
join payments
on financial_accounts.account_number = payments.sending_account_number
where financial_accounts.account_number = payments.sending_account_number;

select sum(amount) into total_amount
from payments
join
financial_accounts
on payments.sending_account_number = financial_accounts.account_number
join external_users
on financial_accounts.external_user_id = external_users.external_user_id
where payments.status_id = 1
and financial_accounts.external_user_id = user_id_temp;

if total_amount >= 2000
then update external_users
set status_id = 4
where external_user_id = user_id_temp;

elsif total_amount >= 500
then update external_users
set status_id = 3
where external_user_id = user_id_temp;

elsif total_amount >= 100
then update external_users
set status_id = 2
where external_user_id = user_id_temp;

end if;
end;

When trying to create this trigger I receive the following error message:

Error at line 6: PL/SQL: SQL Statement ignored

declare
user_id_temp number := 0;
total_amount number := 0;
begin
select external_users.external_user_id into user_id_temp

Does anyone know what the problem might be? This is the first time I have tried creating a trigger so feedback would be much appreciated.

È stato utile?

Soluzione

The problem is that in the following statement

select external_users.external_user_id into user_id_temp
  from external_users
  join financial_accounts
    using (external_user_id)
  join payments
    on financial_accounts.account_number = payments.sending_account_number
  where financial_accounts.account_number = payments.sending_account_number;

the USING clause merges EXTERNAL_USERS.EXTERNAL_USER_ID and FINANCIAL_ACCOUNTS.EXTERNAL_USER_ID into a single EXTERNAL_USER_IDfield, and thus you can no longer say EXTERNAL_USERS.EXTERNAL_USER_ID. Change the statement to

select external_user_id
  into user_id_temp
  from external_users
  join financial_accounts
    using (external_user_id)
  join payments
    on financial_accounts.account_number = payments.sending_account_number
  where financial_accounts.account_number = payments.sending_account_number;

and give it a try.

Share and enjoy.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top