gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:51:31 24 4
gpt4 key购买 nike

我正在尝试发表观点。到目前为止,我已经写了:

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 语句必须是查询批处理中的唯一语句。

在这种情况下,您有两种选择,具体取决于您想要实现的功能:

  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
    )
  2. 在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com