title: Check if a sql view exists and then create it dynamically before altering it
body: <p>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.</p>
  
  <p>I am using the following script</p>
  
  <pre><code>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]
  ---
  ---
  </code></pre>
  
  <p>The above script throws this error</p>
  
  <blockquote>
    <p>Msg 102, Level 15, State 1, Procedure test, Line 1<br>
    Incorrect syntax near 'AS'.</p>
  </blockquote>
  
  <p>May I know the correct way to do it?</p>