Search code examples
javadatabasederbyscalar-subquery

Trigger After Update ,error: Scalar subquery is only allowed to return a single row


I need solution for my problem. Thanks in advance. I use Derby DB.

I have one table with few columns. I created trigger after update for specific columns that I need. When I try to update columns in row I get this error.

Error code 30000, SQL state 21000: Scalar subquery is only allowed to return a single row.

This error only appears when I have two or more rows in main table. If I have only one row in table "accounts" everything works fine.

This is code for trigger: (accounts is main table, accounts_history new table)

CREATE TRIGGER aft_update AFTER UPDATE of balance,date
           ON  accounts  

FOR EACH ROW MODE DB2SQL

insert into accounts_history   

(old_id,new_name,new_balance,new_date) values
            (

(select id from accounts),(select name from accounts),

(select balance from accounts),(select date from accounts) 
            );

Solution

  • A scalar subquery is supposed to return at most one row and exactly one column.

    Just use insert . . . select:

    insert into accounts_history(old_id, new_name, new_balance, new_date)
        select id, name, balance, date
        from accounts;