我正在尝试发表观点。到目前为止,我已经写了:
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;
不幸的是,我在包含 CREATE VIEW 显示
的行上收到错误
"CREATE VIEW must be the only statement in the batch"
我该如何解决这个问题?!
最佳答案
正如错误所示,CREATE VIEW
语句必须是查询批处理中的唯一语句。
在这种情况下,您有两种选择,具体取决于您想要实现的功能:
将
CREATE VIEW
查询放在开头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 )
在 CTE 之后、
CREATE VIEW
查询之前使用GO
-- 选项#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;
关于sql - CREATE VIEW 必须是批处理中唯一的语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27272194/