Reputation: 1849
I have created a view in SQL Server which is working properly and sorting exactly as I require. Here is the view ...
CREATE VIEW [vwInventoryList]
AS
SELECT TOP 100 PERCENT x.[Cols_I_Need]
FROM dbo.vwInventoryListSummary x
GROUP BY x.[columns]
ORDER BY dbo.x.ID,dbo.x.[Description],[with_more_4_cols]
GO
The issue is when I execute the below sql from C# or even in SQL Server Management Studio, the result is not sorted as I have specified in my view and I don't understand why.
Select * from vwInventoryList
I would like the view to control the sorting of the data and my code (select statement) should simply select from the view without specifying the order by in the C# code.
Any ideas why this is happening ?
Upvotes: 0
Views: 3472
Reputation: 639
See Create a view with ORDER BY clause to understand the order by
clause in a view. It is very helpful. One way that you don't have to use an order by in the C# code is to create a stored procedure that calls this view. In the procedure you can put the sql below
SELECT * FROM [vwInventoryList] ORDER BY {Column}
It keeps the C# code from becoming speckled with select statements and allows you to better keep track of procedures. Ultimately it is up to you. Also see What are the pros and cons to keeping SQL in Stored Procs versus Code for more information on when to use sql inline vs stored procs.
Upvotes: 1