sql问题,我创建了一个视图,但问题来自何时 我试图从视图中使用select *来显示视图。
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'.**
答案 0 :(得分: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