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

标签 sql sql-server ddl create-view

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

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/

相关文章:

sql - postgresql 错误 : syntax error at or near "PRIMARY"

c# - 如何将 SQL 结果转换为 C# 中的对象列表?

mysql - 查找作为给定字符串的起始字符串的列值。

sql-server - 在 SELECT 中设置列​​的值?

sql-server - 从 SQL Server 获取数据库列表

sql-server - 当执行 CREATE TABLE 或 ALTER TABLE 操作时,sql server 存储什么类型的审计信息?

postgresql 将 JSON 迁移到 JSONB

php - 我的日期时间格式的日期被插入到我的表中,其中包含更多应该的零

php - php 中不同的 if 语句

sql-server - Clojure/SQLServer : How to Call Stored Procedure with C3P0 Connection Pool