Click here to Skip to main content
15,885,906 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
So I have this Query as follows:

SQL
UPDATE CustomerBankInfo
SET [Money] = ([Money] - @orderValue)
WHERE CustomerBankInfo.CustomerID = (SELECT CustomerID
                                     FROM Customer
                                     WHERE FirstName = @custFName
                                     AND LastName =@custLName)


However I don't want it to run if 'Money' is less than 0. I've tried doing:

SQL
UPDATE CustomerBankInfo
SET [Money] = ([Money] - @orderValue)
IF [Money] > 0
WHERE CustomerBankInfo.CustomerID = (SELECT CustomerID
                                     FROM Customer
                                     WHERE FirstName = @custFName
                                     AND LastName =@custLName)


Doesn't work. Any suggestions? Thanks.
Posted

You didn't provide enough detail regarding datatype of your table attributes and variable, let me guess you are using datatype money for your [Money] field. If i am right, try using the following code:

SQL
UPDATE CustomerBankInfo
SET [Money] = ([Money] - CONVERT(money,@orderValue)
WHERE CustomerBankInfo.CustomerID = (SELECT CustomerID
                                     FROM Customer
                                     WHERE FirstName = @custFName 
                                     AND LastName =@custLName) 
AND ([Money] > CONVERT(money,0))
 
Share this answer
 
Comments
WurmInfinity 13-Dec-11 13:53pm    
Apologies. Its misleading the field is actually an integer. But thanks for the advice I'll keep that one for another day I'm sure it'll come in handy!
Monjurul Habib 13-Dec-11 13:56pm    
:)
Sergey Alexandrovich Kryukov 13-Dec-11 14:13pm    
Exactly, a 5. Recommended as a final answer for OP to accept formally.
--SA
Monjurul Habib 13-Dec-11 14:16pm    
thank you very much for your recommendation. I got the acceptance :) thanks again
Sergey Alexandrovich Kryukov 13-Dec-11 14:23pm    
That was the recommendation to OP who should formally accept this answer. It is most comprehensive so far.
--SA
Put AND Money > 0 into your where clause.
 
Share this answer
 
Comments
WurmInfinity 13-Dec-11 13:48pm    
Cheers
Sergey Alexandrovich Kryukov 13-Dec-11 14:12pm    
Exactly, my 5.
--SA
Monjurul Habib 13-Dec-11 14:17pm    
5!
The right idea is: put your condition for Money in WHERE clause.

—SA
 
Share this answer
 
v3
Comments
WurmInfinity 13-Dec-11 13:48pm    
Thanks
Sergey Alexandrovich Kryukov 13-Dec-11 14:12pm    
You are welcome.
--SA
Monjurul Habib 13-Dec-11 14:17pm    
5!
Sergey Alexandrovich Kryukov 13-Dec-11 14:22pm    
Thank you, Monjurul. You are too nice to this rather lazy answer. :-)
--SA
Monjurul Habib 13-Dec-11 14:25pm    
:)

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