ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效错误消息msg 1033

时间:2015-07-26 07:11:54

标签: sql sql-server database subquery

此查询有什么问题?此外,我尝试使用(选择前100%)它有效但无效。

ALTER VIEW [dbo].[viewTblDaily] 
AS 
   SELECT
       tblTasks.TaskID,TUsers.UserFirstName + ' ' + TUsers.UserLastName AS [UserFullName],
       TTasksType.TaskDescription AS TaskType,
       CONVERT(NVARCHAR, tblTasks.DateTask, 103) DateTask,
       tblTasks.TimeTask,
       tblTasks.TaskDescription,
       tblTasks.statusTask,
       TUsers.UserID AS [userID]
   FROM 
       tblTasks
   LEFT JOIN 
       tblTaskType AS TTasksType ON tblTasks.TaskTypeID = TTasksType.TaskID
   LEFT JOIN 
       tblUsers AS TUsers ON TUsers.UserID = tblTasks.UserID
   WHERE 
       CONVERT(VARCHAR(10), tblTasks.DateTask, 102) = CONVERT(VARCHAR(10), getdate(), 102) 
       AND tblTasks.StatusTask = 0  
   ORDER BY
       TaskID

2 个答案:

答案 0 :(得分:0)

视图不是DB中的实际表。 (实际上MySQL DB也不喜欢有序的表定义) 它仅作为等待您使用它的架构存在。 订购无关

答案 1 :(得分:-1)

我建议你这个

ALTER VIEW [dbo].[viewTblDaily] 
AS 
   SELECT
       tblTasks.TaskID,TUsers.UserFirstName + ' ' + TUsers.UserLastName AS [UserFullName],
       TTasksType.TaskDescription AS TaskType,
       CONVERT(NVARCHAR, tblTasks.DateTask, 103) DateTask,
       tblTasks.TimeTask,
       tblTasks.TaskDescription,
       tblTasks.statusTask,
       TUsers.UserID AS [userID],
       ROW_NUMBER() OVER (ORDER BY TaskID) As RowNum
   FROM 
       tblTasks
   LEFT JOIN 
       tblTaskType AS TTasksType ON tblTasks.TaskTypeID = TTasksType.TaskID
   LEFT JOIN 
       tblUsers AS TUsers ON TUsers.UserID = tblTasks.UserID
   WHERE 
       CONVERT(VARCHAR(10), tblTasks.DateTask, 102) = CONVERT(VARCHAR(10), getdate(), 102) 
       AND tblTasks.StatusTask = 0

您会看到其结果将由RowNum订购,或者在选择后您也可以使用RowNum保证结果的顺序。