计算列不允许在另一个计算列定义中使用 - 我如何允许它?

时间:2018-03-15 18:07:40

标签: sql-server

我正在尝试向我的表中添加三个新列 - Scanned,Actions和HowToFix。

但是,我收到的错误是:

  

“不允许在表'TEST_concat'中计算列'已扫描'   用于另一个计算列定义。“

我该怎么办?我需要在其他两列中使用'Scanned'计算列。

以下是我的代码。提前谢谢!

    SELECT TOP (4000) [filer_id]
      ,[filerName]
      ,[ResouceName]
      ,[UniqueIdentity]
      ,[DirID]
      ,[DirsCount]
      ,[PermCount]
      ,[RowID]
      ,[ResourceType]
      ,[ResourceFlags]
      ,[Hresult]
      ,[Details]
      ,[fsid]
      ,[Protocol]
      ,[vExport]
      ,[filerType]
      ,[Error Messages]

  FROM [master].[dbo].[TEST_concat]

ALTER TABLE
    dbo.TEST_concat
ADD
    [Scanned] AS

    CASE WHEN [DirID] > 0 AND [DirsCount] is NULL THEN 'in Shares table, but not SortedDirectoryTree properties'
        WHEN [DirID] > 0 AND  [DirsCount] > 0 THEN 'YES'
        WHEN [DirID] is null and [DirsCount] is null THEN 'NO'
        ELSE ' ' END

ALTER TABLE
    dbo.TEST_concat
ADD
    [Actions] AS
    CASE WHEN [protocol] = 'CIFS' AND [Error Messages] LIKE 'monitor type mismatch' AND [Scanned] = 'Yes' THEN 'Needs action'
         WHEN [protocol] = 'NFS' AND [DirsCount] = 0 AND [Scanned] = 'in Shares table, but not SortedDirectoryTree properties' THEN 'Needs action'
         WHEN [Error Messages] LIKE 'Pruned Different Security Type' or [Error Messages] LIKE 'mismatch' THEN 'Needs action'
         WHEN [Error Messages] LIKE 'access denied' THEN 'Needs action'
         WHEN [protocol] = 'CIFS' AND  [Scanned] = 'in Shares table, but not SortedDirectoryTree properties' THEN 'Needs action'
         WHEN  [Error Messages] LIKE 'the inherited' or [Error Messages] LIKE 'the path' or [Error Messages] LIKE 'missing inheritance' THEN 'Needs action'
         WHEN [Error Messages] LIKE 'Access is Denied. (1)'  AND [DirsCount] < 3 THEN 'Needs action'
         WHEN [protocol] = 'CIFS' AND [protocol] = 'NFS' THEN 'Needs action'
        ELSE ' ' END

ALTER TABLE
    dbo.TEST_concat
ADD
    [HowToFix] AS
    CASE WHEN [protocol] = 'CIFS' AND [Error Messages] LIKE 'monitor type mismatch' AND [Scanned] = 'Yes' THEN 'Backend problems. Security is not visible'
        WHEN [protocol] = 'NFS' AND [DirsCount] = 0 AND [Scanned] = 'in Shares table, but not SortedDirectoryTree properties' THEN 'Backend problems. Security is not visible'
        WHEN [Error Messages] LIKE 'Pruned Different Security Type' or [Error Messages] LIKE 'mismatch' THEN 'Change to NFS'
        WHEN [protocol] = 'CIFS' AND  [Scanned] = 'in Shares table, but not SortedDirectoryTree properties' THEN 'Backend problems. Security is not visible'
        WHEN [Error Messages] LIKE 'access denied' THEN 'Give permission to Varonis'
        WHEN [protocol] = 'CIFS' AND  [Scanned] = 'in Shares table, but not SortedDirectoryTree properties' THEN 'Backend problems. Security is not visible'
        WHEN  [Error Messages] LIKE 'the inherited' or [Error Messages] LIKE 'the path' or [Error Messages] LIKE 'missing inheritance' THEN 'Ignore Error'
        WHEN [Error Messages] LIKE 'Access is Denied. (1)'  AND [DirsCount] < 3 THEN 'Give permission to Varonis'
        ELSE ' ' END ' END

2 个答案:

答案 0 :(得分:4)

呃......你没有,但你的解决方案选项是..

  • 在计算第二个计算列时,重新计算第一个计算列。
  • 创建一个视图,选择您的表加上第一个计算列,并根据第一个计算列创建第二个计算列。
  • 创建一个存储过程,其中第一个计算列位于子查询中,第二个位于主查询中,并返回主查询中的所有行。

顺便看一下你的T-SQL你可能想要考虑一个包含所有这些动作/ howtofix的查找表,在查询中你可以在该查找表上加入并返回正确的动作。

答案 1 :(得分:1)

使用完整表达式而不是[Scanned]。例如,而不是

ADD
    [Actions] AS
    CASE WHEN [protocol] = 'CIFS' AND [Error Messages] LIKE 'monitor type mismatch' AND [Scanned] = 'Yes' THEN 'Needs action'

代码:

ADD
    [Actions] AS
    CASE WHEN [protocol] = 'CIFS' AND [Error Messages] LIKE 'monitor type mismatch' AND CASE WHEN [DirID] > 0 AND [DirsCount] is NULL THEN 'in Shares table, but not SortedDirectoryTree properties'
    WHEN [DirID] > 0 AND  [DirsCount] > 0 THEN 'YES'
    WHEN [DirID] is null and [DirsCount] is null THEN 'NO'
    ELSE ' ' END = 'Yes' THEN 'Needs action'

很方便,但那是SQL。