I'm writing a script to create a view, only IF the view does not already exist. If the view does already exist, I don't want to alter it or drop and re-create it. The syntax below is obviously not complete, and generates an error because CREATE VIEW
needs to be in its own batch - but what is the proper way to construct my use case?
IF OBJECT_ID('dbo.view_name') IS NULL
BEGIN
CREATE VIEW [dbo].[view_name]
AS
SELECT ...;
END
ELSE
...