我想在dbo.ret_udfCalculateDynamicReportHybridColumn
子句中使用WHERE
函数,但是我必须避免在SELECT
和WHERE
子句中都重复该函数,如何解决问题?
这是我的存储过程:
ALTER PROCEDURE [dbo].[ret_FinancialVoucherArticles_GetArticles]
(@CalculationGuid UNIQUEIDENTIFIER,
@FinancialVoucherTypeGuid UNIQUEIDENTIFIER)
AS
SELECT
salaried.CostCenterGuid AS CostCenterEntityGuid,
salaried.CostCenterCode,
salaried.PersonGuid AS PersonEntityGuid,
salaried.PersonCode,
salaried.[Guid] AS SalariedEntityGuid,
salaried.FinancialIdentifierCode,
financial_VoucherArticleTemplates.ArticleNature,
financial_VoucherArticleTemplates.[Description],
financial_VoucherArticleTemplates.AccountCodePatternGuid,
financial_VoucherArticleTemplates.AccountCodePattern,
financial_VoucherTemplates.TargetDBGuid,
financial_VoucherTemplates.FiscalYear,
financial_VoucherTemplates.GroupingIdenticalArticle,
calcs.[Guid] AS CalculationGuid,
dbo.ret_udfCalculateDynamicReportHybridColumn(financial_VoucherArticleTemplates.ReferenceGuid) AS Amount
FROM
[ret_payrollCalculations] calcs
INNER JOIN
[ret_PayrollCalculationInfo] info ON info.PayrollCalculationGuid = calcs.[Guid]
INNER JOIN
[ret_PayrollCalculationCommands] commands ON commands.[Guid] = calcs.[CalculationCommandGuid]
INNER JOIN
[ret_PayrollItemPatterns] patterns ON patterns.[Guid] = commands.[ItemPattern]
INNER JOIN
[ret_FinancialVoucherTemplateGroups] financialVoucherTemplateGroups ON financialVoucherTemplateGroups.[Guid] = patterns.[FinancialVoucherTemplateGroupGuid]
INNER JOIN
[ret_FinancialVoucherTemplateGroupsDetails] financialVoucherTemplateGroupsDetails ON financialVoucherTemplateGroupsDetails.[FinancialVoucherTemplateGroupGuid] = financialVoucherTemplateGroups.[Guid] AND financialVoucherTemplateGroupsDetails.[FinancialVoucherTypeGuid] = @FinancialVoucherTypeGuid
INNER JOIN
[gtw_Financial_VoucherTemplates] financial_VoucherTemplates ON financial_VoucherTemplates.[Guid] = financialVoucherTemplateGroupsDetails.[FinancialVoucherTemplateGuid]
INNER JOIN
[gtw_Financial_VoucherArticleTemplates] financial_VoucherArticleTemplates ON financial_VoucherArticleTemplates.[VoucherTemplateGuid] = financial_VoucherTemplates.[Guid]
INNER JOIN
[ret_vwSalariedFull] salaried ON salaried.[Guid] = calcs.[SalariedGuid]
WHERE
calcs.[Guid] = @CalculationGuid
AND dbo.ret_udfCalculateDynamicReportHybridColumn(financial_VoucherArticleTemplates.ReferenceGuid) <> 0
答案 0 :(得分:3)
使用CTE或子查询一次对其进行计算,并将结果列作为WHERE
过滤器和SELECT
结果列引用:
;WITH Data AS
(
SELECT
salaried.CostCenterGuid AS CostCenterEntityGuid,
salaried.CostCenterCode,
salaried.PersonGuid AS PersonEntityGuid,
salaried.PersonCode,
salaried.[Guid] AS SalariedEntityGuid,
salaried.FinancialIdentifierCode,
financial_VoucherArticleTemplates.ArticleNature,
financial_VoucherArticleTemplates.[Description],
financial_VoucherArticleTemplates.AccountCodePatternGuid,
financial_VoucherArticleTemplates.AccountCodePattern,
financial_VoucherTemplates.TargetDBGuid,
financial_VoucherTemplates.FiscalYear,
financial_VoucherTemplates.GroupingIdenticalArticle,
calcs.[Guid] AS CalculationGuid,
dbo.ret_udfCalculateDynamicReportHybridColumn(financial_VoucherArticleTemplates.ReferenceGuid) AS Amount
FROM
[ret_payrollCalculations] calcs INNER JOIN
[ret_PayrollCalculationInfo] info ON info.PayrollCalculationGuid = calcs.[Guid] INNER JOIN
[ret_PayrollCalculationCommands] commands ON commands.[Guid] = calcs.[CalculationCommandGuid] INNER JOIN
[ret_PayrollItemPatterns] patterns ON patterns.[Guid] = commands.[ItemPattern] INNER JOIN
[ret_FinancialVoucherTemplateGroups] financialVoucherTemplateGroups ON financialVoucherTemplateGroups.[Guid] = patterns.[FinancialVoucherTemplateGroupGuid] INNER JOIN
[ret_FinancialVoucherTemplateGroupsDetails] financialVoucherTemplateGroupsDetails ON financialVoucherTemplateGroupsDetails.[FinancialVoucherTemplateGroupGuid] = financialVoucherTemplateGroups.[Guid] AND financialVoucherTemplateGroupsDetails.[FinancialVoucherTypeGuid] = @FinancialVoucherTypeGuid INNER JOIN
[gtw_Financial_VoucherTemplates] financial_VoucherTemplates ON financial_VoucherTemplates.[Guid] = financialVoucherTemplateGroupsDetails.[FinancialVoucherTemplateGuid] INNER JOIN
[gtw_Financial_VoucherArticleTemplates] financial_VoucherArticleTemplates ON financial_VoucherArticleTemplates.[VoucherTemplateGuid] = financial_VoucherTemplates.[Guid] INNER JOIN
[ret_vwSalariedFull] salaried ON salaried.[Guid] = calcs.[SalariedGuid]
WHERE
calcs.[Guid] = @CalculationGuid
)
SELECT
D.CostCenterEntityGuid,
D.CostCenterCode,
D.PersonEntityGuid,
D.PersonCode,
D.SalariedEntityGuid,
D.FinancialIdentifierCode,
D.ArticleNature,
D.Description,
D.AccountCodePatternGuid,
D.AccountCodePattern,
D.TargetDBGuid,
D.FiscalYear,
D.GroupingIdenticalArticle,
D.CalculationGuid,
D.Amount
FROM
Data AS D
WHERE
D.Amount <> 0
确保检查执行计划,以查看它是否仅被计算一次。