locked
Help! Problem with Visual Studio 2010 (Visual Basic) Query Builder

    Question

  • I have a Visual Basic Project using Visual Studio 2010. I have connected to a SQL Server using "Data Source Configuration Wizard". After selecting some fields of a table named "Scores" in my Database, I came up with my new dataset on my Solution Explorer.

    The fields that I have selected in the "Scores" table are "Scores.score", "Scores.numitems", "Scores.percentages".

    I right clicked the "ScoresTableAdapter" and I selected "Preview Data", it is working fine and displays all the result.

    But I need to make a computation. So, I configured the ScoresTableAdapter and clicked the "Query Builder" button then it will show me the SELECT statement.

    SELECT score, numitems, percentages FROM Scores

    I want the "score" to be divided by "numitems" and then multiplied by "percentages" so I changed the query like this:

    SELECT ((score / numitems) * percentages) AS totalscore FROM Scores

    I put the parenthesis so that it will divide first and then it will multiply.

    NOW my PROBLEM is, when I click "OK" Button, the Query Builder automatically removes my parenthesis, so instead of:

    SELECT ((score / numitems) * percentages) AS totalscore FROM Scores

    it will become:

    SELECT (score / numitems * percentages) AS totalscore FROM Scores

    This is wrong because it will multiply the numitems and percentages before it will divide by score. It will produce a wrong result.

    Why is it the query builder removes the parenthesis?

    Please Help Me. What should I do?

    Allan

    Saturday, August 11, 2012 12:41 AM

Answers

  • Are you sure there's a difference? Multiplication has not a higher precedence than division: Operator Precedence (Transact-SQL) 

    They have the same level, and "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."


    Armin


    Saturday, August 11, 2012 12:58 AM

All replies

  • Are you sure there's a difference? Multiplication has not a higher precedence than division: Operator Precedence (Transact-SQL) 

    They have the same level, and "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."


    Armin


    Saturday, August 11, 2012 12:58 AM
  • Mr. Armin Zingler,

    Please forgive my ignorance. What I'm thinking that maybe SQL follows the MDAS arithmetic rule. But with the link you gave me, I've learned a lot from it. Thank You!

    Saturday, August 11, 2012 3:01 AM