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