0
INSERT INTO Table1 (Amount, ContactName, State, City) 
values 
(
    (Select Amount from Table2 where id=12)+
    (Select Amount from Table3 where id=13),
    'Sam',Newyork,'Newyork'
);

I was trying to Store the value of amount in Table1 by the sum of Table2 + Table3 Amount

4
  • And what is the Problem with it? Commented Jun 7, 2016 at 5:41
  • Any database exception? Commented Jun 7, 2016 at 5:43
  • You have missed out on '' around the State variable. Commented Jun 7, 2016 at 5:46
  • So what you are getting ? any result or an error ? Commented Jun 7, 2016 at 5:52

2 Answers 2

1

You could also try another way:

INSERT INTO Table1 (Amount, ContactName, State, City) 
values 
(
     select sum(Amount) Amount
       from (Select Amount from Table2 where id=12
             union all
             Select Amount from Table3 where id=13),
    'Sam',Newyork,'Newyork'
);
Sign up to request clarification or add additional context in comments.

Comments

1

Try with nvl(sum(Amount),0). The SUM will sum all values in the table and if there will be no rows you will get NULL. The NVL will remove possible NULL and it will replace it with 0.

INSERT INTO Table1 (Amount, ContactName, State, City) 
values 
(
    (Select nvl(sum(Amount),0) from Table2 where id=12)+
    (Select nvl(sum(Amount),0) from Table3 where id=13),
    'Sam','Newyork','Newyork'
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.