Sandhya Anand
Sandhya Anand

Reputation: 1

Cannot create a view in 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

I want to create a view but I keep receiving this error

Msg 156, Level 15, State 1, Procedure PercentPopulationVaccinated, Line 12 [Batch Start Line 127]
Incorrect syntax near the keyword 'Select'.

Upvotes: 0

Views: 237

Answers (1)

squillman
squillman

Reputation: 13641

You have 2 statements in this batch. CREATE VIEW has to be the only statement in the batch. Remove that 2nd SELECT statement from your batch. If you are doing this in SSMS you can also add GO in between the CREATE VIEW statement and that last SELECT.

 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

Upvotes: 1

Related Questions