我试图摆脱我所有的动态 SQL 代码。这个存储过程似乎需要一些带有 WHERE 子句的嵌套 CASE 语句。这按预期工作。但是,我担心性能。如果父 CASE 语句不正确,是否会执行嵌套的 ( GuarantorNumber not in ( select... ) ) 语句?
WHERE 1 = CASE WHEN ( @method='ADDED_RECORDS' ) THEN
CASE WHEN
( pt.[GuarantorNumber] not in (select cpg.GuarantorNumber from [CorepointProtectedGuarantor] cpg) ) THEN
1
ELSE 0 END -- NEW_RECORDS
WHEN ( @method='DELETED_RECORDS' ) THEN
CASE WHEN
( pt.[GuarantorNumber] not in (select mpg.GuarantorNumber from [MeditechProtectedGuarantor] mpg) ) THEN
1
ELSE 0 END -- DELETED_RECORDS
WHEN ( @method='UPDATED_RECORDS' ) THEN
CASE WHEN
( pt.[GuarantorNumber] in
(SELECT mpg.GuarantorNumber FROM
[MeditechProtectedGuarantor] mpg,
[CorepointProtectedGuarantor] cpg
WHERE
mpg.GuarantorNumber = cpg.GuarantorNumber and
(mpg.[LastName] <> cpg.[LastName] OR
mpg.[FirstName] <> cpg.[FirstName] OR
mpg.[MiddleName] <> cpg.[MiddleName] OR
mpg.[Address] <> cpg.[Address] OR
mpg.[AddressLine2] <> cpg.[AddressLine2] OR
mpg.[City] <> cpg.[City] OR
mpg.[State] <> cpg.[State] OR
mpg.[ZIP] <> cpg.[ZIP] OR
mpg.[Phone] <> cpg.[Phone])
) -- end of SELECT clause
) -- end of WHEN clause
THEN
1
ELSE 0 END -- UPDATED_RECORDS