Ashan アシャン February 2016

how to CREATE and UPDATE VIEW in SQL Server 2008 using single query

I have two query. create new view and update view query.

1) vw_delay (new),

CREATE VIEW vw_delay AS
        SELECT ....

2) vw_RDoc (exist)

 ALTER VIEW vw_RDoc AS
        SELECT ....

I want to run both query as single query. I tried to run like this,

CREATE VIEW vw_delay AS
            SELECT ....
  ALTER VIEW vw_RDoc AS
            SELECT ....

but it returns this error:

Msg 156, Level 15, State 1, Procedure vw_delay, Line 7
Incorrect syntax near the keyword 'ALTER'.

Msg 111, Level 15, State 1, Procedure vw_delay, Line 7
'ALTER VIEW' must be the first statement in a query batch.

How to do this? Please help me.

Answers


Kamil Stachowiak February 2016

What is necessary to do is add GO after first CREATE VIEW.

CREATE VIEW vw_delay AS
            SELECT ....
GO
  ALTER VIEW vw_RDoc AS
            SELECT ....

Then it will work.


Rahul Tripathi February 2016

You need to put the GO after the Create View. Do remember that GO is used as a delimiter for the BATCHES

CREATE VIEW vw_delay AS
SELECT ....
GO
ALTER VIEW vw_RDoc AS
SELECT ....


moks March 2016

Use batch terminator 'GO'. it will separate the both batches and then it will work fine

CREATE VIEW YourViewName AS
SELECT * FROM tablex

GO

ALTER VIEW YourViewName AS
SELECT * FROM tabley

Post Status

Asked in February 2016
Viewed 3,122 times
Voted 12
Answered 3 times

Search




Leave an answer