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?!
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.
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.
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.
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.
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
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 )
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;
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];');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With