I’m trying to achieve what the code below suggests, but I’m getting the error Incorrect syntax near the keyword 'view' on both the create and alter lines.
IF Object_ID('TestView') IS NULL BEGIN create view TestView as . . . END ELSE BEGIN ALTER view TestView as . . . END
Because ALTER/CREATE commands can’t be within BEGIN/END blocks. You need to test for existence and the drop it before doing a create
If you are woried about the permissions being lost you can script the GRANT statements as well and re-run those at the end.
You could wrap the create/alter into a string and do an EXEC – that might get ugly for large views