I am trying check if a SQL Server view exists and if it doesn't exist, create a view with dynamic script and then alter it.
I am using the following script
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[test]') AND OBJECTPROPERTY(id,N'IsView') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N' CREATE VIEW [dbo].[test] AS '
END
GO
ALTER VIEW [dbo].[test]
---
---
The above script throws this error
Msg 102, Level 15, State 1, Procedure test, Line 1
Incorrect syntax near 'AS'.
May I know the correct way to do it?