需要帮助创建视图

时间:2017-11-19 22:41:53

标签: sql sql-server tsql

CREATE VIEW custowner
AS
   (SELECT        
        RTRIM(p.FirstName)+ ' ' + p.LastName AS [name], 
        LEFT(p.Phone, 12) AS phone, 
        p.CellPhone AS cellphone, 
        LEFT(p.EMail, 22) AS email
    FROM  
        CUSTOMER_OWNER co 
    INNER JOIN 
        PEOPLE p ON co.PeopleID = p.PeopleId
    ORDER BY 
        name ASC);

我收到错误:

  

ORDER BY子句在视图,内联函数,派生表,子查询和公用表表达式中无效,除非还指定了TOP,OFFSET或FOR XML。

我该如何解决这个问题?

2 个答案:

答案 0 :(得分:3)

此处的解决方案是从查询中删除order by子句。如果您希望以name的特定顺序显示结果,则可以在从视图中选择数据时执行此操作。

答案 1 :(得分:0)

有一种方法可以做到这一点,但您应该遵循建议在视图上进行排序。 但是要破解它,你可以在SELECT语句中使用TOP 100 PERCENT

create view custowner
as
(SELECT TOP 100 PERCENT     
rtrim(p.FirstName)+ ' ' + p.LastName as [name], 
left(p.Phone, 12) as phone, 
p.CellPhone as cellphone, 
left(p.EMail, 22) as email
FROM CUSTOMER_OWNER co INNER JOIN PEOPLE p
ON co.PeopleID = p.PeopleId
order by name ASC);

它应该现在可以使用