创建视图中的SQL重复列名称错误,即使使用别名也是如此

时间:2016-12-08 08:42:04

标签: sql-server oracle

这个错误很奇怪,但我只是在它发生的地方磨练了。标准修复似乎不起作用。

此处发生错误。这是一个很重要的陈述,所以我只是张贴我发现错误的地方

 CREATE OR REPLACE VIEW SalesInvoiceDoc 
 AS 
 ( SELECT si.salinv_Num, si.salinv_Terms, si.salinv_SaleDate, 
          es.empName AS SalesAgent, man.empName AS ApprovingManager,
          si.salinv_ApproveDate, ...

 ... FROM service_invoice si
 JOIN employee es
    ON (es.empID = si.salinv_EmpID)
 JOIN employee man
    ON (man.empID = si.salinv_ManID)

基本上它是销售代理和批准经理的销售发票文档。两者都存储在员工表中,并为“经理”设置了子类型鉴别器。或者' sales'以及子类型子表的视图,以避免重复错误。

我提供了别名来尝试避免重复列,但是当我运行此脚本时,我得到重复的列名错误:

ORA-00957: duplicate column name

有谁知道如何解决这个问题?它甚至可能吗?任何帮助将不胜感激。提前谢谢!

编辑:这里是完整的陈述

 CREATE OR REPLACE VIEW SalesInvoiceDoc
 AS
 ( SELECT si.salinv_Num, si.salinv_Terms, si.salinv_SaleDate,
    es.empName AS SalesAgent,
    man.empName AS ApprovingManager, si.salinv_ApproveDate,
    sc.custName, sc.custHouse, sc.custCity, 
    sc.custState, sc.custZIP, sc.custPhone, sc.custEmail,
    sv.vehicle_VIN, sv.vehicle_year, sv.vehicle_make, 
        sv.vehicle_model, sv.vehicle_ext_color, sv.vehicle_trim,
        sv.vehicle_list_base_price, sv.vehicle_mileage, sv.vehicle_condition,
        sv.vehicle_description,
     ti.vehicle_make, ti.vehicle_year, ti.vehicle_model, ti.vehicle_VIN, 
        ti.tradein_allowance,
    sv.vehicle_list_base_price "SellingPrice", sv.shipping "Shipping",
        ti.tradein_allowance "TradeAllowance",
(sv.vehicle_list_base_price + sv.shipping - ti.tradein_allowance) "Subtotal",
(sv.vehicle_list_base_price + sv.shipping - ti.tradein_allowance)*.0825 "Taxes",
(sv.vehicle_list_base_price + sv.shipping - ti.tradein_allowance)*1.0825 "TotalSellingPrice"

 FROM sales_invoice si
JOIN employee es
    ON (es.empID = si.salinv_EmpID)
JOIN employee man
    ON (man.empID = si.salinv_ManID)
JOIN customer sc
    ON (sc.custID = si.salinv_CustID)
JOIN vehicle sv
    ON (sv.vehicle_VIN = si.salinv_SalVIN)
LEFT OUTER JOIN vehicle ti
    ON (ti.vehicle_VIN = si.salinv_tiVIN)
 );

2 个答案:

答案 0 :(得分:1)

您正在复制2个不同表中的列名:

sv.vehicle_VIN, 
sv.vehicle_year, 
sv.vehicle_make, 
sv.vehicle_model

ti.vehicle_make, 
ti.vehicle_year, 
ti.vehicle_model, 
ti.vehicle_VIN

生成的列名称不包括表别名。

答案 1 :(得分:0)

问题是因为sv.vehicle_VIN在您的查询中出现两次,其他列也是如此。见下文。你需要相应地对它们进行别名

**sv.vehicle_VIN**, **sv.vehicle_year**, **sv.vehicle_make**,  **sv.vehicle_model**, 

     **ti.vehicle_make**, **ti.vehicle_year**, **ti.vehicle_model**, **ti.vehicle_VIN**,