How to solve 'CREATE VIEW' must be the first statement in a query batch.

+1 vote
1,591 views
asked May 22, 2017 by Hitesh Garg (799 points)  

While executing a batch of statements in SQL server 2012 I am getting the following error .

com.microsoft.sqlserver.jdbc.SQLServerException: 'CREATE VIEW' must be the first statement in a query batch.

Sample code is attached below.

CREATE TABLE source (
    a int  NOT NULL,
    b int  NOT NULL,
    c int  NOT NULL,
    d int  NOT NULL,
    CONSTRAINT source_pk PRIMARY KEY  (a)
);

CREATE VIEW view1
AS
SELECT a, b FROM source;

CREATE VIEW view2
AS
SELECT c, d FROM source;

Any ideas where I'm going wrong because this is a schema recreate script in which database is created from the beginning.

Please log in or register to answer this question.

...