Statement Where clause errors

  • Using the following in a Stored Proc the WHERE clause says that RANK is not defined, why? Note: The ORDER BY clause has no problems with RANK.

    INSERT INTO @Products

    SELECT Row_Number() OVER ( ORDER BY idProduct ) as RowNumber, idProduct, Description,

    SUBSTRING(Details, 1, @DescriptionLength) + '...' AS Details,

    ListPrice, Price, smallImageUrl, imageUrl, sku,

    3 * dbo.WordCount(@Word1, Description) + dbo.WordCount(@Word1, Details) +

    3 * dbo.WordCount(@Word2, Description) + dbo.WordCount(@Word2, Details) +

    3 * dbo.WordCount(@Word3, Description) + dbo.WordCount(@Word3, Details) +

    3 * dbo.WordCount(@Word4, Description) + dbo.WordCount(@Word4, Details) +

    3 * dbo.WordCount(@Word5, Description) + dbo.WordCount(@Word5, Details)

    AS [RANK]

    FROM [dbo].CPNET_Product

    WHERE RANK > 0

    ORDER BY [RANK] DESC

  • Am i right in saying that you cannot use a name that you give a calculation in a select statement within the where clause.

    Therefore RANK is not recognised, but if you type the calculation again within the where clause (no need to specificy 'As RANK') it should work.

    I also think that the reason that the Order By clause doesnt fall over is because SQL server never got to it as it fell over on the Where clause before.

    Correct me if im wrong, but hope this helps.

  • If you see the Execution Plan for the query, the picture will be clear. The WHERE Clause is actually evaluated before your SELECT Clause and the ORDER BY Clause. Hence you get the error.

    ORDER BY Clause gets evaluated after the SELECT Clause and hence you don't get any error if you use it.


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • If you remove the WHERE clause the ORDER BY works just fine.

    I'll try your suggestion.

  • Hi Kingston,

    Thanks for the reply. I believe that makes perfect sense.

    As a teachable moment - how can I see the "Execution Plan"

  • That worked! Thanks.

  • To see the execution plan, you can select the option Query -> Include Actual Execution Plan. Now if you run the SELECT query you will see the Execution Plan after the Results and Messages Tab.

    You don't have to actually remove the the Where Clause to make the query work. Just put the SELECT query in a sub query and then apply the Where Clause, it will work.

    INSERT INTO @Products

    SELECT *

    FROM (

    SELECT Row_Number() OVER ( ORDER BY idProduct ) as RowNumber, idProduct, Description,

    SUBSTRING(Details, 1, @DescriptionLength) + '...' AS Details,

    ListPrice, Price, smallImageUrl, imageUrl, sku,

    3 * dbo.WordCount(@Word1, Description) + dbo.WordCount(@Word1, Details) +

    3 * dbo.WordCount(@Word2, Description) + dbo.WordCount(@Word2, Details) +

    3 * dbo.WordCount(@Word3, Description) + dbo.WordCount(@Word3, Details) +

    3 * dbo.WordCount(@Word4, Description) + dbo.WordCount(@Word4, Details) +

    3 * dbo.WordCount(@Word5, Description) + dbo.WordCount(@Word5, Details)

    AS [RANK]

    FROM [dbo].CPNET_Product

    ) CPNET_Product

    WHERE RANK > 0

    ORDER BY [RANK] DESC


    Kingston Dhasian

    How to post data/code on a forum to get the best help - Jeff Moden
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks, most helpful and the suggestion to include it in a sub-query makes the maintenance much easier.

    Thanks again.

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply