Im wondering if there is a way to determine which views in my database use the "order by" statement.
The reason I need this is because we need to migrate over to MS SQL 2005 where the order by statements are ignored within the views themselves. Now(in mssql 2005) you need to explicityly state the order by now when calling a view
select * from [viewname] order by column x, y desc, z
select * from [viewname]
where the view already had the applicable sorting done within the view.
If those order by statements are ignored, some production software which rely on the ordered data will corrupt.
Please let me know if there's a way to query the actual database and determine which views have 'order by' statements in them.
you can use this script to list the view names having order by clause
You need to put a BIG caveat on this idea... While it works for views with nicely formatted source code, it can generate both false positives (for any view with either a commented ORDER BY clause, or the text ORDER BY), and false negatives (where the words order and by have something other than a space between them, such as a line-end, tab, multiple spaces, comments, etc).
The only way to be truly certain is to actually parse the view and check the tokenized output for an ORDER BY. This is beyond the reach of simple Transact-SQL, although I'm thinking about a couple of ways that could let you work around this limitation.
Thx for all the responses. I will try the following:
select name from dbo.sysobjects where objectproperty(id, N'IsView') = 1 and
id in (select id from dbo.syscomments where text like '%order by%')
as suggested by kadapa22. Although it may result in both false postives and negatives, I think it's worth a try. Better than manually looking through over 800 views manually. I will let you know how it goes.