SQL 创建视图,显示视图,关键字“SELECT”附近的语法不正确
Posted
技术标签:
【中文标题】SQL 创建视图,显示视图,关键字“SELECT”附近的语法不正确【英文标题】:SQL Create View ,Show view,Incorrect syntax near the keyword 'SELECT' 【发布时间】:2016-04-22 06:15:56 【问题描述】:sql问题,我创建了一个视图,但是问题来自于什么时候 我尝试使用从视图中选择 * 来显示视图。
CREATE VIEW YearByYearSalesDelta as
WITH temp
as (select SalesPersonID,YEAR(DueDate) as SaleYear, SUM(SubTotal) as Total
from Sales.SalesOrderHeader
group by SalesPersonID, year(DueDate)
)
select NewYear.SalesPersonID,
OldYear.SaleYear AS OldYear,
NewYear.SaleYear as NewYear,
NewYear.Total-OldYear.Total as Delta
from temp AS OldYear
JOIN temp AS NewYear on NewYear.SalesPersonID=OldYear.SalesPersonID
AND NewYear.SaleYear-1=OldYear.SaleYear
SELECT * FROM dbo.YearByYearSalesDelta
消息显示
**Msg 156, Level 15, State 1, Procedure YearYearSalesDelta, Line 15
Incorrect syntax near the keyword 'SELECT'.**
【问题讨论】:
【参考方案1】:你应该在视图之后使用批处理分隔符 GO
CREATE VIEW YearByYearSalesDelta as
WITH temp
as (select SalesPersonID,YEAR(DueDate) as SaleYear, SUM(SubTotal) as Total
from Sales.SalesOrderHeader
group by SalesPersonID, year(DueDate)
)
select NewYear.SalesPersonID,
OldYear.SaleYear AS OldYear,
NewYear.SaleYear as NewYear,
NewYear.Total-OldYear.Total as Delta
from temp AS OldYear
JOIN temp AS NewYear on NewYear.SalesPersonID=OldYear.SalesPersonID
AND NewYear.SaleYear-1=OldYear.SaleYear
GO
SELECT * FROM dbo.YearByYearSalesDelta
【讨论】:
或者使用;
sqlblog.com/blogs/aaron_bertrand/archive/2012/09/25/16567.aspx正确终止语句以上是关于SQL 创建视图,显示视图,关键字“SELECT”附近的语法不正确的主要内容,如果未能解决你的问题,请参考以下文章