我有一个表,名为 FacilityDatabaseConnection,like-so,ID,FacilityID,DatabaseTypeID,ConnectionString
所以给定一个 FacilityID 和 DatabaseTypeID 对,你会得到一个 ConnectionString。Facility 和 DatabaseType 表都有“名称”字段。我想在 FacilityDatabaseConnection 中创建一个“名称”字段来执行以下操作,
SELECT (dbo.Facility.Name+' - '+dbo.DatabaseType.Name) as Name
FROM dbo.FacilityDatabaseConnection
INNER JOIN dbo.Facility
ON dbo.FacilityDatabaseConnection.FacilityID = dbo.Facility.ID
INNER JOIN dbo.DatabaseType
ON dbo.FacilityDatabaseConnection.DatabaseTypeID = dbo.DatabaseType.ID
以便它返回“FacilityName - DatabaseType”
这可以作为查询使用,但是可以将其设为字段吗?
我试过了,
ALTER TABLE dbo.FacilityDatabaseConnection
ADD Name AS (SELECT (dbo.Facility.Name+' - '+dbo.DatabaseType.Name) as Name
FROM dbo.FacilityDatabaseConnection
INNER JOIN dbo.Facility
ON dbo.FacilityDatabaseConnection.FacilityID = dbo.Facility.ID
INNER JOIN dbo.DatabaseType
ON dbo.FacilityDatabaseConnection.DatabaseTypeID = dbo.DatabaseType.ID) PERSISTED
这给了我一个错误“在这种情况下不允许子查询。只允许标量表达式。”
有没有办法实现这一点,或者这样的计算字段是不可能的?