我的存储过程的Staff列并不总是生成信息

时间:2016-06-29 20:08:24

标签: sql-server stored-procedures

因此,我的存储过程有一个staff列,应该从我的Employee表生成一个人员名称。工作人员姓名仅显示某些行。任何人都可以看看我的错误在哪里:

BEGIN
    IF EXISTS (SELECT 1 FROM [user] (NOLOCK) 
               WHERE [user].ID = @UserID 
                 AND [user].BrandID IS NULL AND [user].SpaID IS NULL)
BEGIN  
    DECLARE @OrderStatusID_Completed int, @OrderStatusID_Shipped int

    SET @OrderStatusID_Shipped = 4
    SET @OrderStatusID_Completed = 2

    SELECT 
        CAST('' AS varchar(50)) AS ErrMsg
        , o.OrderNumber
        , Customer.GUID AS CustomerGUID
        , OrderItem_View.DateCreated AS ItemDate
        , COALESCE(MasterProductVariant.SKU, ProductVariant.SKU, Treatment.SKU) AS SKU
        , DynamicPrice.FinalPrice AS Price
        --, COALESCE(MasterProductVariant.OriginalPrice, ProductVariant.OriginalPrice, Treatment.Price) AS Price
        , ISNULL(Employee.FirstName,'') + ' ' + ISNULL(Employee.LastName,'') AS Staff
        , COALESCE(Product.Name, Treatment.Name) AS Item
        , NULL AS Note
    FROM
        [Order] o (Nolock)
    LEFT JOIN 
        Customer (Nolock) ON o.CustomerID = Customer.ID
    INNER JOIN 
        OrderItem_View (nolock) ON OrderItem_View.OrderID = o.ID
    LEFT JOIN 
        DynamicPrice (nolock) ON OrderItem_View.DynamicPriceID = DynamicPrice.ID
    LEFT JOIN 
        AppointmentTreatment WITH (NOLOCK) ON AppointmentTreatment.ID = OrderItem_View.AppointmentTreatmentID
    LEFT JOIN 
        Employee (NOLOCK) ON Employee.ID = COALESCE(OrderItem_View.EmployeeID, OrderItem_View.Employee2ID, AppointmentTreatment.EmployeeID)
    LEFT JOIN 
        Treatment_View Treatment (nolock) ON Treatment.BillableItemID = OrderItem_View.BillableItemID
    LEFT JOIN 
        ProductVariant (NOLOCK) 
    LEFT JOIN 
        Product (NOLOCK) ON Product.ID = ProductVariant.ProductID
            ON ProductVariant.BillableItemID = OrderItem_View.BillableItemID
    LEFT JOIN 
        ProductVariant MasterProductVariant (NOLOCK) ON ProductVariant.MasterRecordID = MasterProductVariant.ID
    WHERE 
        o.SpaID = @SpaID
        AND o.IsDeleted = 0
        AND o.DateCompleted >= CONVERT(DATETIME,0)
        AND o.DateCompleted < GetDate() 
        AND o.StatusID IN (@OrderStatusID_Completed,@OrderStatusID_Shipped)
END
ELSE
    SELECT CAST('Insufficient rights.' AS VARCHAR(50)) AS ErrMsg
END

1 个答案:

答案 0 :(得分:2)

它必须处于合并功能中。介于这三者之间 - 你没有获得价值

OrderItem_View.EmployeeID OrderItem_View.Employee2ID AppointmentTreatment.EmployeeID

因此,单独运行OrderItem_View并查看是否存在EmployeeID或Employee2ID永远为null的实例。如果是这样,那么尝试确定哪些员工失踪。如果员工丢失,他们是否也在AppointmentTreatment表中丢失?如果是这样,那就存在问题。