设计中的计算列可以在sql server中引用另一个吗?

时间:2013-01-02 10:27:28

标签: database sql-server-2008 tsql calculated-columns

我有2个计算列,[StartTime][EndTime]

使用[StartTime][EndTime]列,使用公式计算[Week][Year]

现在我需要另一个使用前两个计算的计算列[Status]。但是当我尝试在[Status]

的公式中使用其中一个时,它给出了公式中的错误

我真的需要这个工作,因为我别无选择。但这有可能吗?

在这里你先走-1:

(case when [IsVOR]=(1) then 'VOR' 
      when [MarkedAsCompleteOn] IS NULL AND [Year]<datepart(year,getdate()) then 'Overdue' 
      when [MarkedAsCompleteOn] IS NULL AND [Year]>datepart(year,getdate()) then 'Not Due' 
      when [MarkedAsCompleteOn] IS NULL AND [Year]=datepart(year,getdate()) AND [Week]<datepart(iso_week,getdate()) then 'Overdue' 
      when [MarkedAsCompleteOn] IS NULL AND [Year]=datepart(year,getdate()) AND [Week]=datepart(iso_week,getdate()) then 'Due' 
      when [MarkedAsCompleteOn] IS NULL AND [Year]=datepart(year,getdate()) AND [Week]>datepart(iso_week,getdate()) then 'Not Due' 
      when [MarkedAsCompleteOn] IS NOT NULL AND [Year]<datepart(year,[MarkedAsCompleteOn]) then 'Late' 
      when [MarkedAsCompleteOn] IS NOT NULL AND [Year]>datepart(year,[MarkedAsCompleteOn]) then 'Early' 
      when [MarkedAsCompleteOn] IS NOT NULL AND [Year]=datepart(year,[MarkedAsCompleteOn]) AND [Week]<datepart(iso_week,[MarkedAsCompleteOn]) then 'Late' 
      when [MarkedAsCompleteOn] IS NOT NULL AND [Year]=datepart(year,[MarkedAsCompleteOn]) AND [Week]=datepart(iso_week,[MarkedAsCompleteOn]) then 'On Time' 
      when [MarkedAsCompleteOn] IS NOT NULL AND [MarkedAsCompleteOn]<[AllocatedTimeStart] then 'Early'  end)

它的最后一部分导致错误:

[MarkedAsCompleteOn]<[AllocatedTimeStart] then 'Early'

错误是通用的:

- Error validating the formula for column 'Status'.

2 个答案:

答案 0 :(得分:0)

没有。一个计算列不能基于不同的计算列。这有一个特定的错误(1759):

  

计算栏&#39;%s&#39;在表格&#39;%s&#39;不允许在另一个计算列定义中使用。

您可以根据表创建视图,并在视图定义中执行第二轮计算。然后,您是否针对视图执行所有活动(如果需要,添加触发器),或仅将其用于查找是您需要做出的设计决策。

当然,您添加了使用视图的想法,您可以使用公用表表达式构建多个(而不仅仅是2个)计算层。

你确实失去了使计算列保持不变的能力,除非视图有资格成为索引视图 - 在这种情况下这并不重要,因为它似乎是基于日期计算,所以可能无论如何都不会持久化。

答案 1 :(得分:0)

我最终通过使用function找到了自己的解决方案,这使我的生产应用无需任何更改。

USE [DBNAME]
GO
/****** Object:  UserDefinedFunction [dbo].[GetStatus]    Script Date: 01/02/2013 11:08:29 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[GetStatus](@CompletedOn DATETIME, @Year INT, @Week INT)
RETURNS NVARCHAR(MAX)
AS BEGIN
    DECLARE @CurrentDate DATETIME
    DECLARE @StartTime DATETIME
    DECLARE @EndTime DATETIME
    DECLARE @Status NVARCHAR(MAX)

    SET @CurrentDate = GETDATE()
    SET @StartTime = (dateadd(week,@Week-(1),dateadd(day,(-1),dateadd(week,datediff(week,(0),CONVERT([varchar](4),@Year,(0))+'-01-01'),(1)))))
    SET @EndTime = (dateadd(week,@Week-(1),dateadd(day,(-1),dateadd(week,datediff(week,(0),CONVERT([varchar](4),@Year,(0))+'-01-01'),(8)))))

    SET @Status = '-'
    IF(@CompletedOn IS NULL)
        BEGIN
            IF(@CurrentDate < @StartTime)
                SET @Status = 'Not Due'
            IF(@CurrentDate > @StartTime AND @CurrentDate < @EndTime)
                SET @Status = 'Due'
            IF(@CurrentDate > @EndTime)
                SET @Status = 'Overdue'
        END
    ELSE
        BEGIN
            IF(@CompletedOn < @StartTime)
                SET @Status = 'Early'
            IF(@CompletedOn > @EndTime)
                SET @Status = 'Late'
            ELSE
                SET @Status = 'On Time'
        END

    RETURN @Status
END

status字段中我放了公式:

([dbo].[GetStatus]([MarkedAsCompleteOn],[Year],[Week]))

这是我function中的第一个sql server,因此可能不是最佳状态。