Order by clause in View Definition when Select top 100 percent is used SQL Server 2005

I am getting errors such as ..."invalid column name 'Name'" and "Msg 4104, Level 16, State 1, Procedure vw_MWOrderValues, Line 3 The multi-part identifier "dbo.Orders.OrderID" could not be bound." when trying to use an Order by clause in View Definitions. (The second error happened when trying to subscribe to the view) See examples below. These were created in SQL Server 2000 and they work by using the TOP 100 PERCENT clause, but when I try to recreate them in SQL Server 2005, I keep getting errors. Anybody know why? Thank you.

SET ANSI_NULLSON
GO
SET QUOTED_IDENTIFIERON
GO
CREATEVIEW dbo.vw_ServiceCenters
AS
SELECT
TOP 100PERCENT dbo.ServiceCenters.Id, dbo.ServiceCenters.NameMsgId,ISNULL(WebMessage_3.EnglishMsg,'')AS Name, dbo.ServiceCenters.ShortNameMsgId,ISNULL(WebMessage_1.EnglishMsg,'')AS ShortName, dbo.ServiceCenters.BusinessUnit, dbo.ServiceCenters.DivisionId, dbo.Divisions.DivisionMsgId,ISNULL(WebMessage_2.EnglishMsg,'')AS DivisionName, dbo.ServiceCenters.FileServerName, dbo.ServiceCenters.DBServerName, dbo.ServiceCenters.SalesRegionAS SalesRegionID,dbo.SalesRegions.SalesRegionAS SalesRegionName, dbo.SalesRegions.SalesRegionShortAS SalesRegionShortName, dbo.ServiceCenters.Zip,ISNULL(dbo.zipcode_info.Lat,N'0')AS Lat,ISNULL(dbo.zipcode_info.Long,N'0')AS Long
FROM dbo.ServiceCentersINNERJOIN
dbo.DivisionsON dbo.ServiceCenters.DivisionId= dbo.Divisions.DivisionIDLEFTOUTERJOIN
dbo.zipcode_infoON dbo.ServiceCenters.Zip= dbo.zipcode_info.ZIPLEFTOUTERJOIN
dbo.SalesRegionsON dbo.ServiceCenters.SalesRegion= dbo.SalesRegions.SalesRegionIDLEFTOUTERJOIN
dbo.WebMessage WebMessage_2ON dbo.Divisions.DivisionMsgId= WebMessage_2.IdLEFTOUTERJOIN
dbo.WebMessage WebMessage_3ON dbo.ServiceCenters.NameMsgId= WebMessage_3.IdLEFTOUTERJOIN
dbo.WebMessage WebMessage_1ON dbo.ServiceCenters.ShortNameMsgId= WebMessage_1.Id

ORDERBY dbo.ServiceCenters.Name
GO
SET ANSI_NULLSOFF
GO
SET QUOTED_IDENTIFIEROFF

***************************************************************
***************************************************************

SET ANSI_NULLSON
GO
SET QUOTED_IDENTIFIERON
GO
CREATEVIEW dbo.vw_MWOrderValues
AS
SELECT
TOP 100PERCENT dbo.Orders.OrderID, dbo.MWValues.MwId,ISNULL(dbo.MWValues.EntryTypeId, 0)AS EntryTypeId,ISNULL(dbo.MWEntryTypes.MessageId, 0)AS EntryTypeMessageId,ISNULL(dbo.WebMessage.EnglishMsg,'')AS EntryTypeDesc,ISNULL(dbo.MWValues.[Value], 0)AS [Value],ISNULL(dbo.MWEntryTypes.CategoryId, 0)AS CategoryId,ISNULL(dbo.MWCategoryTypes.MessageId,'')AS CategoryMessageId,ISNULL(dbo.MWEntryTypes.ValueUnitId, 0)AS ValueUnitId,ISNULLdbo.MWValueUnitsTypes.MessageId, 0)AS ValueUnitMessageId,ISNULL(dbo.MWEntryTypes.GroupBy, 0)AS GroupById,ISNULL(dbo.MWGroupByTypes.MessageId, 0)AS GroupByMessageId,ISNULL(dbo.MWGroupByTypes.Report, 0)AS Report,ISNULL(dbo.MWGroupByTypes.PairedId, 0)AS PairedId,ISNULL(dbo.MWEntryTypes.Editable, 0)AS Editable,ISNULL(dbo.MWEntryTypes.ContractValue, 0)AS ContractValue,ISNULL(dbo.MWEntryTypes.DisplayExpanded, 0)AS DisplayExpanded,ISNULL(dbo.MWEntryTypes.DisplaySummary, 0)AS DisplaySummary,ISNULL(dbo.MWEntryTypes.DisplayIndex, 0)AS DisplayIndex,isnull(dbo.MWValues.ViewType, 0)AS ViewType, Display

FROM dbo.MWValuesINNERJOIN
dbo.MWEntryTypesON dbo.MWValues.EntryTypeId= dbo.MWEntryTypes.Id
INNERJOIN
dbo.WebMessageON dbo.MWEntryTypes.MessageId= dbo.WebMessage.Id
INNERJOIN
dbo.MWValueUnitsTypesON dbo.MWEntryTypes.ValueUnitId= dbo.MWValueUnitsTypes.Id
LEFTOUTERJOIN
dbo.MWGroupByTypesON dbo.MWEntryTypes.GroupBy= dbo.MWGroupByTypes.IdLEFTOUTERJOIN
dbo.MWCategoryTypesON dbo.MWEntryTypes.CategoryId= dbo.MWCategoryTypes.Id
RIGHT
OUTERJOINdbo.OrdersON dbo.MWValues.MwId= dbo.Orders.MoneyWeightID
UNION
SELECTTOP 100PERCENT dbo.OrderRevisions.OrderIDAS OrderId, dbo.MWValues.MwId,ISNULL(dbo.MWValues.EntryTypeId, 0)AS EntryTypeId,ISNULL(dbo.MWEntryTypes.MessageId, 0)AS EntryTypeMessageId,ISNULL(dbo.WebMessage.EnglishMsg,'')AS EntryTypeDesc,ISNULL(dbo.MWValues.[Value], 0)AS [Value],ISNULL(dbo.MWEntryTypes.CategoryId, 0)AS CategoryId,ISNULL(dbo.MWCategoryTypes.MessageId, 0)AS CategoryMessageId,ISNULL(dbo.MWEntryTypes.ValueUnitId, 0)AS ValueUnitId,ISNULL(dbo.MWValueUnitsTypes.MessageId, 0)AS ValueUnitMessageId,ISNULL(dbo.MWEntryTypes.GroupBy, 0)AS GroupById,ISNULL(dbo.MWGroupByTypes.MessageId, 0)AS GroupByMessageId,ISNULL(dbo.MWGroupByTypes.Report, 0)AS Report,ISNULL(dbo.MWGroupByTypes.PairedId, 0)AS PairedId,ISNULL(dbo.MWEntryTypes.Editable, 0)AS Editable,ISNULL(dbo.MWEntryTypes.ContractValue, 0)AS ContractValue,ISNULL(dbo.MWEntryTypes.DisplayExpanded, 0)AS DisplayExpanded,ISNULL(dbo.MWEntryTypes.DisplaySummary, 0)AS DisplaySummary,ISNULL(dbo.MWEntryTypes.DisplayIndex, 0)AS DisplayIndex,isnull(dbo.MWValues.ViewType, 0)AS ViewType, Display

FROM dbo.MWValuesINNERJOIN
dbo.MWEntryTypesON dbo.MWValues.EntryTypeId= dbo.MWEntryTypes.Id
INNERJOIN
dbo.WebMessageON dbo.MWEntryTypes.MessageId= dbo.WebMessage.Id
INNERJOIN
dbo.MWCategoryTypesON dbo.MWEntryTypes.CategoryId= dbo.MWCategoryTypes.Id
INNERJOIN
dbo.MWValueUnitsTypesON dbo.MWEntryTypes.ValueUnitId= dbo.MWValueUnitsTypes.Id
INNERJOIN
dbo.MWGroupByTypesON dbo.MWEntryTypes.GroupBy= dbo.MWGroupByTypes.IdRIGHTOUTERJOIN
dbo.OrderRevisionsON dbo.MWValues.MwId= dbo.OrderRevisions.MoneyWeightID

WHERE(dbo.OrderRevisions.Finalized<> 0)AND(dbo.OrderRevisions.ModificationTypeIDIN
(SELECT [id]

FROM vw_ModificationTypesContractValue))

ORDERBY dbo.Orders.OrderID

GO
SET ANSI_NULLSOFF
GO
SET QUOTED_IDENTIFIEROFF

[42538 byte] By [Debra] at [2008-3-1]
# 1
You have to change it to just OrderId the column alias. Note that due to a bug in the parser, SQL Server 2000 does accept certain syntaxes that are invalid. You can take a look at these changes in the BOL topic below:

Behavior Changes to Database Engine Features in SQL Server 2005

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/instsql9/html/65eaafa1-9e06-4264-b547-cbee8013c995.htm

Lastly, this technique of using TOP 100 PERCENT in view doesn't guarantee that the rows will be sorted correctly when you query it. ORDER BY clause in the outer-most query is the one that guarantees that rows are sorted properly and sent to the client/consumer. This is even more so in case of SQL Server 2005. So you should not rely on this behavior and modify the definition since it will not work in SQL Server 2005. For a list of rules, see the post at:

http://blogs.msdn.com/sqltips/archive/2005/07/20/441053.aspx

# 2
This is totaly absurd...
If I define a view as

CREATE VIEW [dbo].[vwERep_ActualColNotInEntProp]

AS

(

SELECT TOP 100 PERCENT

dbo.vwNGMDBPhysicalSchema.TableName,

dbo.vwNGMDBPhysicalSchema.FieldName

FROM

dbo.vwNGMDBPhysicalSchema LEFT OUTER JOIN dbo.EntityProperty ON

dbo.vwNGMDBPhysicalSchema.TableName = dbo.EntityProperty.EntityInternalName

WHERE

dbo.EntityProperty.PropertyInternalName IS NULL

ORDER BY

dbo.vwNGMDBPhysicalSchema.TableName,

dbo.vwNGMDBPhysicalSchema.FieldName

)
it should by hell work the same way it did in SQL 2k and honor the Order By.

Changing this is tantamount to breaking a lot, a LOT of code that's out there...

Bogus.

jcraigue at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...
# 3
CREATE VIEW ... ORDER BY does not make sense, any more than CREATE TABLE ... ORDER BY makes sense. Just because something "worked" in SQL Server 2000 does not make it right. Can you find any official Microsoft source that documents how "ORDER BY in a view" should work? What exactly does your ORDER BY accomplish? So other queries can run SELECT * without an ORDER BY, and always get the same result? ORDER BY belongs to the query, not the structure behind it. wrote in message news:fcdb414c-9e56-440d-851c-640a7fdb3a59@discussions.microsoft.com...
> This is totaly absurd...
> If I define a view as > >
> CREATE VIEW [dbo].[vwERep_ActualColNotInEntProp] >
> AS >
> ( >
> SELECT TOP 100 PERCENT >
> dbo.vwNGMDBPhysicalSchema.TableName, >
> dbo.vwNGMDBPhysicalSchema.FieldName >
> FROM >
> dbo.vwNGMDBPhysicalSchema LEFT OUTER JOIN dbo.EntityProperty ON >
> dbo.vwNGMDBPhysicalSchema.TableName =
> dbo.EntityProperty.EntityInternalName >
> WHERE >
> dbo.EntityProperty.PropertyInternalName IS NULL >
> ORDER BY >
> dbo.vwNGMDBPhysicalSchema.TableName, >
> dbo.vwNGMDBPhysicalSchema.FieldName >
> )
> it should by hell work the same way it did in SQL 2k and honor the Order
> By. >
> Changing this is tantamount to breaking a lot, a LOT of code that's out
> there... >
> Bogus. > > >
>
MVPUser at 2007-9-9 > top of Msdn Tech,SQL Server,Transact-SQL...

SQL Server

Site Classified