首页 > 解决方案 > 无法在 SQL 中创建视图

问题描述

 Create view PercentPopulationVaccinated as 
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
,sum(convert(int,vac.new_vaccinations)) OVER (Partition by dea.location order by dea.location,
dea.date) as rolling_people_vaccinated
from PortfolioProject..['Covid vaccinations$'] vac
Join PortfolioProject..['Covid deaths$'] dea
     On dea.location=vac.location
     and dea.date=vac.date
where dea.continent is not null
--order by 2,3

Select * 
from PercentPopulationVaccinated

我想创建一个视图,但我不断收到此错误

消息 156,级别 15,状态 1,过程 PercentPopulationVaccinated,第 12 行 [批处理开始行 127]
关键字“选择”附近的语法不正确。

标签: sql-server

解决方案


这批中有 2 条语句。CREATE VIEW 必须是批处理中的唯一语句。从批处理中删除第二个 SELECT 语句。如果您在 SSMS 中执行此操作,您还可以在 CREATE VIEW 语句和最后一个 SELECT 之间添加 GO。

 Create view PercentPopulationVaccinated as 
Select dea.continent, dea.location, dea.date, dea.population, vac.new_vaccinations
,sum(convert(int,vac.new_vaccinations)) OVER (Partition by dea.location order by dea.location,
dea.date) as rolling_people_vaccinated
from PortfolioProject..['Covid vaccinations$'] vac
Join PortfolioProject..['Covid deaths$'] dea
     On dea.location=vac.location
     and dea.date=vac.date
where dea.continent is not null
--order by 2,3
GO

Select * 
from PercentPopulationVaccinated

推荐阅读