1

我试图摆脱我所有的动态 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
4

2 回答 2

0
Declare @query nvarchar(max) 

Declare @selectoneCount int

Declare @Temp int

SET @Temp =1

Declare @val nvarchar(max)

SET @val =''

select 
       @query = case @Temp

       when 1 

       then       
          case @val

          when 'NULL'

          then
            ('Select Diameter from BallsProbes where Remark =2') 

          else
            ('Select Diameter from BallsProbes where Remark =3')   
            END 
       when 2 
       then ('Select Diameter from BallsProbes where Remark =6')
       end

EXEC(@query)
于 2013-06-01T12:01:17.617 回答
0

搜索到的 CASE 表达式:

  • 返回计算结果为 TRUE 的第一个 input_expression = when_expression 的 result_expression。

在您的情况下,如果 @method='ADDED_RECORDS' 和 @method='DELETED_RECORDS' 为假,那么将在 SQLServer2008 中执行下一条语句 WHEN(@method='UPDATED_RECORDS') 以通过按 Cntl+L 来分析此行为。

于 2013-04-23T16:47:44.877 回答