Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
customer table (CUST_ID,CUST_NAME,CUST_ADDRESS,EMP_ID)
has table (CUST_ID,ACCOUNT_NUMBER)
table account(ACCOUNT_NUMBER,ACCOUNT_BALANCE)

Customer with id=3 had his account balances increased by 10%

What I have tried:

SQL
UPDATE account
SET account.account_balance = account.account_balance * 1.1
WHERE EXISTS (SELECT a.account_number
             FROM account a,has h
             where a.account_number = h.account_number
             and h.cust_id=3)


IT IS UPDATING ALL ROWS I NEED TO UPDATE ONLY WHEN H.CUST_ID=3
Posted
Updated 2-Mar-20 16:53pm
v4
Comments
PIEBALDconsult 2-Mar-20 21:55pm    
Was the help you receive before not helpful?
https://www.codeproject.com/Questions/5260382/Sql-query-to-be-solved-the-salary-of-employee-deal
Abed Al Rahman Hussien Balhawan 2-Mar-20 22:01pm    
hello, i tried those queries and they did not work on oracle database10G

I tried this one on a similar case it works pretty fine , but here it us updating all rows, i do not understand why it is not reading the last statment
(((and h.cust_id=3)))
PIEBALDconsult 2-Mar-20 22:08pm    
Please rest assures that it is doing exactly what you asked it to.
Abed Al Rahman Hussien Balhawan 2-Mar-20 22:14pm    
i fixed it
UPDATE account
SET account.account_balance = account.account_balance * 1.1
WHERE EXISTS (SELECT h.account_number
FROM has h
where account.account_number = h.account_number
and h.cust_id=3)
PIEBALDconsult 2-Mar-20 22:08pm    
EXISTS is a cry for help, don't use it, just use a JOIN (though I don't understand Oracle's JOIN syntax, bleah).

You really need to do some more research into the EXISTS operator, and use what you have learned earlier about using it versus a JOIN
SQL
UPDATE a
SET    a.Account_Balance = a.Account_Balance * 1.1
FROM   Account a
INNER JOIN Has h ON a.Account_Number = h.Account_Number
WHERE h.Cust_ID = 3
The beauty of using a JOIN statement for the UPDATE is the ability to pre-check it easily... simply comment out the UPDATE...SET lines in the above query and add in a simple SELECT like this to see what rows will be affected and before/after values; like this
SQL
--	UPDATE a
--	SET    a.Account_Balance = a.Account_Balance * 1.1
	SELECT h.Cust_ID, a.Account_Balance, NewBalance = a.Account_Balance * 1.1
	FROM   Account a
	INNER JOIN Has h ON a.Account_Number = h.Account_Number
	WHERE h.Cust_ID = 3
 
Share this answer
 
Comments
Abed Al Rahman Hussien Balhawan 2-Mar-20 23:28pm    
your version did not work on my oracle10G

i modified it as the following

SELECT Cust_ID, Account_Balance,Account_Balance*1.1 as NewBalance
FROM Account a
INNER JOIN Has h ON a.Account_Number = h.Account_Number
WHERE h.Cust_ID = 3

Resulting:
CUST_ID ACCOUNT_BALANCE NEWBALANCE
3 440 484
3 550 605

thank you for the information Madmyche i really appreciate :)
MadMyche 3-Mar-20 7:18am    
Oracle and Sql Server have slightly different allowed syntaxes when it comes to aliases, good job figuring that out.
And your results are not unexpected; as it is possible for 1 customer to have multiple accounts
Abed Al Rahman Hussien Balhawan 2-Mar-20 23:42pm    
i tried update using inner join method, it did not work
error code : ORA-00933: SQL command not properly ended
UPDATE account
SET account.account_balance = account.account_balance * 1.1
WHERE EXISTS (SELECT h.account_number
             FROM has h
             where account.account_number = h.account_number
             and h.cust_id=3) 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900