SQL Server添加自己的别名并使它们出错

时间:2015-06-19 16:05:34

标签: sql-server sql-server-2012 ssms ssms-2014

我写了以下简短的简单视图:

--DROP VIEW ReportObjects.vStarts;
CREATE VIEW ReportObjects.vStarts AS
SELECT *
FROM ReportObjects.vLotHistory
WHERE
        LotType = 'Some constant value'
    AND CDOName = 'Some constant value'
    AND SpecId = 'Some constant value'

当我执行此脚本时,请点击" Design"在SSMS的观点中,我得到了这个:

SELECT ContainerName, SpecName, ProductName, LotStatus, CDOName, ResourceName AS LotType, EmployeeName AS WorkflowName, LotType AS DieQty, 
                  WorkflowName AS LotQty, DieQty AS TxnDate, LotQty AS TxnDateGMT, TxnDate AS ContainerId, TxnDateGMT AS HistoryMainlineId, ContainerId AS ProductId, 
                  HistoryMainlineId AS SpecId, ProductId AS WorkflowId, SpecId AS WorkflowBaseId, WorkflowId AS WorkflowStepId, WorkflowBaseId, WorkflowStepId, ResourceId, 
                  EmployeeId
FROM     ReportObjects.vLotHistory
WHERE  (LotType = 'Some constant value') AND (CDOName = 'Some constant value') AND (SpecId = 'Some constant value')

看到问题? DieQty AS TxnDate, LotQty AS TxnDateGMT, TxnDate AS ContainerId 它的别名为columnA,名称为columnC

我已尝试多次删除/重新创建视图。 我知道可以说SELECT *是icky,但这不是重点(有时在生产代码中 是必要的。)

上面的视图是从另一个定义为:

的视图中进行选择
CREATE VIEW ReportObjects.vLotHistory AS
SELECT
    lot.ContainerName,
    hist.SpecName,
    hist.ProductName,
    lot.Status LotStatus,
    hist.CDOName,
    hist.ResourceName,
    hist.EmployeeName,
    lot.csiLotType LotType,
    WorkFlowBase.WorkflowName,
    hist.Qty DieQty,
    hist.Qty2 LotQty,
    hist.TxnDate,
    hist.TxnDateGMT,
    lot.ContainerId,
    hist.HistoryMainlineId,
    hist.ProductId,
    hist.SpecId,
    Workflow.WorkflowId,
    Workflow.WorkflowBaseId,
    hist.WorkflowStepId,
    hist.ResourceId,
    hist.EmployeeId
FROM ODS.MES_Schema.Container lot
    LEFT JOIN ODS.MES_Schema.HistoryMainline hist
        ON lot.ContainerId = hist.ContainerId
    LEFT JOIN ODS.MES_Schema.WorkflowStep WS
        ON hist.WorkflowStepId = WS.WorkflowStepId
    LEFT JOIN ODS.MES_Schema.Workflow
        ON WS.WorkflowId = Workflow.WorkflowId
    LEFT JOIN ODS.MES_Schema.WorkflowBase
        ON WorkflowBase.WorkflowBaseId = Workflow.WorkflowBaseId

有谁知道为什么SSMS或SQL Server正在吃我的查询并吐出废话?请注意,我没有使用SSMS工具来创建视图 - 我正在使用CREATE VIEW,如上所示。

0 个答案:

没有答案