gpt4 book ai didi

sql-server - 不允许在另一个计算列定义中使用计算列

转载 作者:行者123 更新时间:2023-12-04 12:06:57 24 4
gpt4 key购买 nike

我得到不允许在另一个计算列中使用计算列
当我尝试在另一个计算域中使用一个计算域时的定义错误消息。有什么解决方法可以实现这一点吗?

计算域1

ALTER TABLE DBO.[ASSy] ADD [CALC STOCK NO] AS (

CASE
WHEN isnull([DIRECTIONAL TREAD],'') ='YES RIGHT' THEN isnull([STOCK NO],'')+'R'
WHEN isnull([DIRECTIONAL TREAD],'') ='YES LEFT' THEN isnull([STOCK NO],'')+'L'
ELSE isnull([STOCK NO],'')
end
)

下面是第二个计算域。当我执行脚本时,我得到

Msg 1759, Level 16, State 0, Line 5 Computed column 'CALC STOCK NO' in table 'ASSy' is not allowed to be used in another computed-column definition.


ALTER TABLE dbo.[ASSy] ADD [PN & DESCRIPTION] AS (ISNULL([CALC STOCK NO],'')+ ', '+ISNULL([TIRE SIZE],'')+', '+ ISNULL([BH SPEC],''))

最佳答案

根据 Computed Columns文档:

A computed column is computed from an expression that can use other columns in the same table. The expression can be a noncomputed column name, constant, function, and any combination of these connected by one or more operators. The expression cannot be a subquery.



一个计算列不能引用另一个计算列。您有以下选择:
  • 创建第二个计算列,其中包含与第一个计算列(无法引用)相同的逻辑。
  • 用普通列替换第一个计算列:

  • ALTER TABLE DBO.[ASSy] DROP COLUMN [CALC STOCK NO]
    ALTER TABLE ADD [CALC STOCK NO] VARCHAR(100) NULL
    UPDATE TABLE t
    SET [CALC STOCK NO] =
    CASE
    WHEN isnull([DIRECTIONAL TREAD],'') ='YES RIGHT' THEN isnull([STOCK NO],'')+'R'
    WHEN isnull([DIRECTIONAL TREAD],'') ='YES LEFT' THEN isnull([STOCK NO],'')+'L'
    ELSE isnull([STOCK NO],'')
    END
    FROM [CALC STOCK NO] t
  • 创建一个 VIEW在表格顶部并在其中实现您的第二列:

  • CREATE VIEW DBO.[vASSy]
    AS
    SELECT t.*
    ,[PN & DESCRIPTION] = (ISNULL([CALC STOCK NO],'')+ ', '+ISNULL([TIRE SIZE],'')+', '+ ISNULL([BH SPEC],''))
    FROM DBO.[ASSy] t

    关于sql-server - 不允许在另一个计算列定义中使用计算列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358013/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com