Đúng như lỗi cho biết, CREATE VIEW câu lệnh cần phải là câu lệnh duy nhất trong lô truy vấn.
Bạn có hai tùy chọn trong trường hợp này, tùy thuộc vào chức năng bạn muốn đạt được:
-
Đặt
CREATE VIEWtruy vấn ở đầuCREATE 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 ) -
Sử dụng
GOsau CTE và trướcCREATE VIEWtruy vấn- Tùy chọn số 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;