必须是批处理中的唯一语句

必须是批处理中的唯一语句

本文介绍了CREATE VIEW 必须是批处理中的唯一语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作视图.到目前为止,我已经写了这个:

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;

不幸的是,我在包含 CREATE VIEW 显示

CREATE VIEW 必须是批处理中唯一的语句"

我该如何解决这个问题?!

How can I fix this?!

推荐答案

正如错误所说,CREATE 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:

  1. 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;
    

  • 这篇关于CREATE VIEW 必须是批处理中的唯一语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

    08-19 11:30