Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


4 Ways to Increase Your Salary (Using UPDATE)


4 Ways to Increase Your Salary (Using UPDATE)

Author
Message
azaroth_lettar
azaroth_lettar
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 11
Please note that id as identity, so using
count (*)

do not deliver the last record but much better to use
max (id)

.
Besides the set should be:
set salary = salary * 1.15


dividing machines are bad.
fzaynoun
fzaynoun
Grasshopper
Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)Grasshopper (14 reputation)

Group: General Forum Members
Points: 14 Visits: 58
This article will improve your skills by reading it, then going into the discussion section and learning how the solution provided is wrong, why not use it, and what to use instead.
jarred.nicholls
jarred.nicholls
Grasshopper
Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)Grasshopper (12 reputation)

Group: General Forum Members
Points: 12 Visits: 14
fzaynoun (11/26/2009)
This article will improve your skills by reading it, then going into the discussion section and learning how the solution provided is wrong, why not use it, and what to use instead.


rofl
dalcock
dalcock
Valued Member
Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)Valued Member (69 reputation)

Group: General Forum Members
Points: 69 Visits: 53
I'm sure I will receive much abuse for this post, but I'm going to share my thoughts anyway because I am a big fan of SQLServer Central and I care about the quality, accuracy and relevancy of the content on this site.

That being said, I found this article extremely disappointing on many levels. First off, as has been mentioned already, the operator precedence applied, as well as the corresponding result, are identical for the expressions (X * Y) / Z and X*Y/Z.

Second, as the author mentions, SQL is built upon set theory and the performance of set based operations dramaticaly outperforms sequential operations, which is why I can't understand why the artical is demonstrating alternatives to set-based solutions when a real-world premise for doing so hasn't even been provided.

Last, there are several set-based operations that can accomplish sophisticated updates (like using ROWCOUNT) that numerous prior articles on this very site have identified. So, as I said, I am very disappointed with this article and fail to see how it is relevant (or helpful) at all to SQL programmers new and old.

Am I missing something here?
David Walker-278941
David Walker-278941
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 231
"Don't use salary * 115 / 100. This violates the operator precedence rule and gives the wrong result."

That statement is absolutely wrong. The Microsoft page that the article links to says "When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression."

Since, as has been mentioned in the comments, multiply and divide have the same precedence, the expression

salary * 115 / 100

is exactly and precisely the same as

(salary * 115)/ 100

Yes, dividing 115 / 100 returns 1, but that doesn't happen here if parentheses are not used. Salary is multiplied by 115 before the result of that is divided by 100. Left to right. (I prefer Salary * 1.15, which someone mentioned above.)

It would be nice to see the original article get corrected by the author!

David Walker
Colin Heming
Colin Heming
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 30
Operator precedence isn't just a programming or SQL server concept - it's a basic rule of mathematics. And no, multiplication doesn't occur before division or vice versa. The left-most operation is evaluated first. This is even clearly conveyed in the linked microsoft article.

The reason you may get differences where division is used is not due to operator precedence - it's due to SQL server interpreting the numerator and denominator as integers and therefore defining the result to be an integer, truncating the fractional portion.

The simpliest way to avoid this is to explicitly make either the numerator or denominator a decimal term, ie use / 100.0 instead of just / 100 or explicitly cast either as a decimal type. The result will then also be a decimal type. (Casting the result after the calculation has no effect.)

However, in this particular case, the numerator would already be a decimal type by the time the division occurs - salary is a float so salary * 115 produces a decimal type. So this whole concept doesn't even apply to this example. Furthermore, the reason for not simply using salary * 1.15 eludes me.

It may seem silly to harp on about a minor technicality, but getting such a basic concept wrong invalidates the credibility of the rest of the article. I'd advise a bit more fact checking and testing for next time.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search