Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Palermo
Starting Member
25 Posts |
Posted - 2013-10-24 : 17:29:58
|
I want to run a transaction query that raises the value in a field by a certain number e.g. 10 but outputs an error if the value in that field goes above say 50. I have :SELECT 'BEFORE', ItemCode,feeFROM db.itemsWHERE ItemCode = 1BEGIN TRAN IncreaseBy10 UPDATE db.itemsSET fee = fee+10 WHERE ItemCode = 1IF @@ROWCOUNT = 50PRINT 'Item price is too high please reduce';SELECT 'WITHIN', * FROM db.itemsThis increases the item prices and now several are above 50 in ItemCode 1 but it does not output an error. |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-10-24 : 17:38:21
|
You could simplify it like shown belowIF EXISTS( SELECT * FROM db.Items WHERE fee > 40 AND ItemCode = 1)-- handle your error condition herePRINT 'Prices too high'ELSE UPDATE db.Items SET fee = fee + 10 WHERE ItemCode = 1; I may have misunderstood your requirement; you are using @@rowcount - which counts the number of rows affected. Can there be more than one row with ItemCode = 1? |
|
|
Palermo
Starting Member
25 Posts |
Posted - 2013-10-24 : 19:06:26
|
quote: Originally posted by James K You could simplify it like shown belowIF EXISTS( SELECT * FROM db.Items WHERE fee > 40 AND ItemCode = 1)-- handle your error condition herePRINT 'Prices too high'ELSE UPDATE db.Items SET fee = fee + 10 WHERE ItemCode = 1; I may have misunderstood your requirement; you are using @@rowcount - which counts the number of rows affected. Can there be more than one row with ItemCode = 1?
Yes there can be more than one row with the same item code. Could you show me how to do it by amending my code? I want to learn it this way before simplifying it as I think I will get a better understanding. |
|
|
|
|
|