编辑 SQL 视图以使用 noexpand 添加更多字段

Edit a SQL View to add more fields WIth noexpand

我继承了一个带有视图和存储过程的遗留数据库。我对SQL的了解有限。

我有一个名为 PropertyCosts 的视图,我想在其中添加其他字段。PropertyCosts 视图使用 (noexpand) 调用另一个名为 PropertyCostsIntermediateA 的视图。

当我编辑 propertycosts 视图以添加两个新字段时出现错误

'Hint 'noexpand' on object 'dbo.PropertyCostsIntermediateA' is invalid.'

如果我用 noexpand 注释掉,错误就会消失。如何在不删除 noexpand 的情况下编辑 PropertyCosts 视图?

    ALTER VIEW [dbo].[PropertyCostsIntermediateA]
WITH SCHEMABINDING
AS
     SELECT uc.OrganisationID,
            unioned.PropertyID,
            unioned.ClonedFromID,
            unioned.Discriminator,
            uc.QuestionID UnitCostQuestionID,
            abaq.Answer_ID AnswerID,
            uc.Amount UnitCost,
            unioned.ShareOfCost TenantsShare,
            unioned.ComputedNumberOfProperties NumberOfProperties,
            uc.RepeatPeriod,
            unioned.SHQSElement_ID UnitCostSHQSElementID,
            unioned.answerdate UnitCostAnswerDate,
            c.UnitsQuestionID,
            c.QuestionID ReplacementYearQuestionID,
            uc.Fees,
            uc.Prelims
     FROM dbo.UnitCostsA uc
          INNER JOIN dbo.CostsA c ON c.UnitCostQuestionID = uc.QuestionID
                                    AND c.isdeleted = 0
                                    AND c.OrganisationID = uc.OrganisationID
          INNER JOIN [dbo].[PropertyCostsUnionedTableA] unioned ON unioned.QuestionID = uc.QuestionID
     AND unioned.OrganisationID = uc.OrganisationID
    INNER JOIN dbo.AnswerBaseAnsweredQuestionsA abaq ON abaq.BaseAnsweredQuestion_ID = unioned.BaseAnsweredQuestionID
     AND abaq.Answer_ID = uc.AnswerID
     WHERE uc.Amount > 0
           AND uc.IsDeleted = 0;
GO

ALTER VIEW [dbo].[PropertyCostsA]
AS
WITH unioned AS (SELECT        OrganisationID, PropertyID, QuestionID, BaseAnsweredQuestionID, Discriminator, AnswerDate, ShareOfCost, ClonedFromID, SHQSElement_ID, IntegerAnswer, ComputedNumberOfProperties
                                        FROM            dbo.PropertyCostsUnionedTableA)
    SELECT        i.OrganisationID, i.PropertyID, i.ClonedFromID, i.Discriminator, i.UnitCostQuestionID, ISNULL(i.UnitCostSHQSElementID, r.SHQSElement_ID) AS SHQSElement_ID, i.AnswerID, r.QuestionID AS ReplacementYearQuestionID, 
                              u.QuestionID AS UnitsQuestionID, i.UnitCost, ISNULL(u.IntegerAnswer, 1) AS UnitsA, i.TenantsShare, i.NumberOfProperties, ISNULL(DATEADD(year, 
                              CASE WHEN r.integeranswer > 100 THEN 100 WHEN R.IntegerAnswer < - 100 THEN - 100 ELSE r.IntegerAnswer END, r.AnswerDate), i.UnitCostAnswerDate) AS AnswerDate, i.RepeatPeriod, a.AbeyanceReason, 
                              e.ExemptionReason, i.UnitCostSHQSElementID, i.Fees, i.prelims,  dbo.udf_FunctionForComputedColumn(unitcost,i.prelims,i.Fees) as CalculatedCost
     FROM            dbo.PropertyCostsIntermediateA AS i 
     WITH (NOEXPAND) 
     LEFT OUTER JOIN
                              unioned AS u ON u.QuestionID = i.UnitsQuestionID AND u.PropertyID = i.PropertyID LEFT OUTER JOIN
                              unioned AS r ON r.QuestionID = i.ReplacementYearQuestionID AND r.PropertyID = i.PropertyID LEFT OUTER JOIN
                              dbo.SHQSExemptions AS e ON i.PropertyID = e.PropertyID AND i.UnitCostSHQSElementID = e.SHQSElementID AND e.IsDeleted = 0 LEFT OUTER JOIN
                              dbo.SHQSAbeyances AS a ON i.UnitCostSHQSElementID = a.SHQSElementID AND i.PropertyID = a.PropertyID AND a.IsDeleted = 0
     WHERE        (i.ReplacementYearQuestionID IS NULL) AND (ISNULL(u.IntegerAnswer, 1) > 0) OR
                              (ISNULL(u.IntegerAnswer, 1) > 0) AND (r.QuestionID IS NOT NULL);
GO

(NOEXPAND) 提示,仅适用于索引视图,否则您会收到您收到的错误消息。

所以,如果 PropertyCostsIntermediateA 不是索引视图,只需删除提示或在视图上添加所需的索引。

无论哪种方式,如果您处于 azure 状态,则永远不需要指定该提示。因为:

Microsoft Documents:

Azure SQL Database supports automatic use of indexed views without specifying the NOEXPAND hint.