Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

when i try to create this Stored Procedure :


SQL
CREATE PROCEDURE dbo.uspSelectTopicsForEdit

    @SortDirection varchar(20)
as
select TopicID,TopicAddress,
(select sum(TopicViewNum) from TopicViews where TopicViews.TopicID=Topics.TopicID) as TopicViews,
(select count(Favorites.UserID) from Favorites where Favorites.TopicID=Topics.TopicID)as BookMarks
from Topics
ORDER BY
    CASE @SortDirection
        WHEN 'MostViewed' THEN TopicViews
        WHEN 'MostBookMarked' THEN BookMarks
        WHEN 'Oldest' THEN Topics.TopicID
    END
go


it gives me this errors:

Invalid column name 'TopicViews'

Invalid column name 'BookMarks'


What can i do please
Posted

These fields are calculated by you so the don't, in any real sense, exist for the user. What you need to do is reuse those values in your case statement.
SQL
CREATE PROCEDURE dbo.uspSelectTopicsForEdit

    @SortDirection varchar(20)
as
select TopicID,TopicAddress,
(select sum(TopicViewNum) from TopicViews where TopicViews.TopicID=Topics.TopicID) as TopicViews,
(select count(Favorites.UserID) from Favorites where Favorites.TopicID=Topics.TopicID)as BookMarks
from Topics
ORDER BY
    CASE @SortDirection
        WHEN 'MostViewed' THEN (select sum(TopicViewNum) from TopicViews where TopicViews.TopicID=Topics.TopicID)
        WHEN 'MostBookMarked' THEN (select count(Favorites.UserID) from Favorites where Favorites.TopicID=Topics.TopicID)
        WHEN 'Oldest' THEN Topics.TopicID
    END
In a situation like this, I would far prefer to use a stored function or handle the sorting on the client side if it's an appropriate business requirement, but this should get you going.

Why do I say handle the sorting at the client side? Well, if the user can change the sort direction, then your sample here requires you to go to the database again (which could return you different results). If you are sorting an in memory collection, then it's one less roundtrip you have to perform. There are caveats, of course, if you were performing paging then you'd want to do this at the server end, but you aren't doing the paging here so it's not an issue.
 
Share this answer
 
Comments
Pratik Bhesaniya 13-May-11 5:25am    
Very well said...
it's so simply...
TopicViews and BookMarks are tables...not columns...
you can order the output result only by columns
 
Share this answer
 
Comments
Pete O'Hanlon 13-May-11 8:13am    
If you look at his Sql, you will see they are also column aliases.
Nick Reshetinsky 13-May-11 11:26am    
I see thanx
Pete O'Hanlon 13-May-11 16:01pm    
No problem. I missed that on my first read through as well.
From what you've posted, TopicViews and BookMarks are table names. You need to modify your ORDER BY clause to use column (field) names e.g.

ORDER BY
  CASE @SortDirection
    WHEN 'MostViewed' THEN TopicViews.TopicViewsColumnName
    WHEN 'MostBokMarked' THEN BookMarks.BookMarksColumnName
    WHEN 'Oldest' THEN Topics.TopicID
  END


where you need to replace TopicViewsColumnName and BookMarksColumnName with the appropriate column names from your tables.
 
Share this answer
 
Thanks all guys,

I am also found nice solution that:

SQL
CREATE PROCEDURE dbo.uspSelectTopicsForEdit

    @SortDirection varchar(20)
as
select * from (select TopicID,TopicAddress,
(select sum(TopicViewNum) from TopicViews where TopicViews.TopicID=Topics.TopicID) as TopicViews,
(select count(Favorites.UserID) from Favorites where Favorites.TopicID=Topics.TopicID)as BookMarks
from Topics) tmp
ORDER BY
    CASE @SortDirection
        WHEN 'MostViewed' THEN TopicViews
        WHEN 'MostBookMarked' THEN BookMarks
        WHEN 'Oldest' THEN TopicID
    END
go



Thanks every body
 
Share this answer
 

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