Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CREATE VIEW must be the only statement in the batch

Tags:

I'm trying to make a view. So far, I have written this:

with ExpAndCheapMedicine(MostMoney, MinMoney) as (     select max(unitprice), min(unitprice)     from Medicine ) , findmostexpensive(nameOfExpensive) as (     select tradename     from Medicine, ExpAndCheapMedicine     where UnitPrice = MostMoney ) , findCheapest(nameOfCheapest) as (     select tradename     from Medicine, ExpAndCheapMedicine     where UnitPrice = MinMoney )  CREATE VIEW showing as select tradename, unitprice, GenericFlag from Medicine; 

Unfortunately, I get an error on the line containing CREATE VIEW showing

"CREATE VIEW must be the only statement in the batch"

How can I fix this?!

like image 994
Kadaj13 Avatar asked Dec 03 '14 12:12

Kadaj13


People also ask

Which statement should be the only statement in the query batch?

Just as the error says, the CREATE VIEW statement needs to be the only statement in the query batch. You have two option in this scenario, depending on the functionality you want to achieve: Place the CREATE VIEW query at the beginning.

Why can't I create a view in a batch file?

Because create view must be the first statement in the batch. In other words, you may have other statements that are processed at the same time before this code, so you can add go to the previous line of the code, and then go on the line after the end of the code.

What does the syntax error create view must be the only?

When we create a view in SQL Server, the syntax error "CREATE view must be the only statement in batch" appears, in fact, there is nothing wrong with it! Because create view must be the first statement in the batch.

Where is the text of the CREATE VIEW statement stored?

The text of the CREATE VIEW statement is stored in the sys.sql_modules catalog view. A query that uses an index on a view defined with numeric or float expressions may have a result that is different from a similar query that does not use the index on the view.


2 Answers

Just as the error says, the CREATE VIEW statement needs to be the only statement in the query batch.

You have two option in this scenario, depending on the functionality you want to achieve:

  1. Place the CREATE VIEW query at the beginning

    CREATE VIEW showing as select tradename, unitprice, GenericFlag from Medicine;  with ExpAndCheapMedicine(MostMoney, MinMoney) as (     select max(unitprice), min(unitprice)     from Medicine ) , findmostexpensive(nameOfExpensive) as (     select tradename     from Medicine, ExpAndCheapMedicine     where UnitPrice = MostMoney ) , findCheapest(nameOfCheapest) as (     select tradename     from Medicine, ExpAndCheapMedicine         where UnitPrice = MinMoney     ) 
  2. Use GO after the CTE and before the CREATE VIEW query

    -- Option #2

    with ExpAndCheapMedicine(MostMoney, MinMoney) as (     select max(unitprice), min(unitprice)     from Medicine ) , findmostexpensive(nameOfExpensive) as (     select tradename     from Medicine, ExpAndCheapMedicine     where UnitPrice = MostMoney ) , findCheapest(nameOfCheapest) as (     select tradename     from Medicine, ExpAndCheapMedicine     where UnitPrice = MinMoney )  GO      CREATE VIEW showing as select tradename, unitprice, GenericFlag from Medicine; 
like image 67
Radu Gheorghiu Avatar answered Sep 19 '22 16:09

Radu Gheorghiu


I came across this question when I was trying to create a couple of views within the same statement, what worked well for me is using dynamic SQL.

    EXEC('CREATE VIEW V1 as SELECT * FROM [T1];');     EXEC('CREATE VIEW V2 as SELECT * FROM [T2];'); 
like image 27
Mozart AlKhateeb Avatar answered Sep 18 '22 16:09

Mozart AlKhateeb