重写动态SQL存储过程

时间:2017-03-28 19:00:58

标签: sql sql-server stored-procedures dynamic-sql

我正在尝试重写动态SQL存储过程以避免将SQL语句视为字符串

原始存储过程

        SELECT
            AM.AUTHNO as Auth_No,
            AST.DESCR as Descr,
            AM.MEMBID_DISP as Memb_Id,
            CONVERT(varchar(25), ISNULL(AM.MEMBNAME,'')) as Member_Name,
            ISNULL(MM.SEX,'') as Member_Sex,
            ISNULL(MM.BIRTH,'') as Member_Dob,
            CONVERT(varchar(25), ISNULL(HC.HPNAME,'')) as Member_Hp,
            CONVERT(varchar(25), ISNULL(PM.LASTNAME,'') + ISNULL(', ' + PM.FIRSTNAME, '')) as Provider_Name,
            PM.PROVID_DISP as Prov_Id,
            '1' as SORT_ORDER,
            AM.DBKEY as Db_Key,
            (SELECT DESCR FROM DB WHERE [KEY] = AM.DBKEY) as Ipa,
            AM.AUTHDATE,
            AM.REQDATE              
        FROM
            Z_AUTH_MASTERS AM,
            Z_AUTH_STATUS AST,
            Z_PROV_MASTERS PM, 
            Z_MEMB_MASTERS MM, 
            Z_HP_CONTRACTS HC
        WHERE
            AM.DBKEY IN ( DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY, @GUID))
            AND AM.DBKEY = PM.DBKEY
            AND AM.AUTHPCP = PM.PROVID
            AND AM.DBKEY = AST.DBKEY
            AND AM.STATUS = AST.CODE
            AND AM.DBKEY = MM.DBKEY
            AND AM.MEMBID = MM.MEMBID
            AND AM.DBKEY = HC.DBKEY
            AND AM.HPCODE = HC.HPCODE
            AND (@STATUS IS NULL OR (AM.STATUS = @STATUS))
            AND (@REFPROV IS NULL OR (AM.AUTHPCP_DISP = @REFPROV))
            AND (@AUTHNO IS NULL OR (AM.AUTHNO like @AUTHNO))
            AND (@REQPROV_FIRST IS NULL OR (PM.FIRSTNAME like @REQPROV_FIRST))
            AND (@REQPROV_LAST IS NULL OR (PM.LASTNAME like @REQPROV_LAST))
            AND (@MEMB_FIRST IS NULL OR (MM.FIRSTNM like @MEMB_FIRST))
            AND (@MEMB_LAST IS NULL OR (MM.LASTNM like @MEMB_LAST))
            AND (@MEMB_ID IS NULL OR (AM.MEMBID_DISP like @MEMB_ID))

这是我到目前为止所拥有的

        if @REQDATE_OP = 'E'
            select @sql = @sql + 'and AM.REQDATE = ''' + @REQDATE_BEGIN + ''' '
        if @REQDATE_OP = 'B'
            select @sql = @sql + 'and (AM.REQDATE >= ''' + @REQDATE_BEGIN + ''' and AM.REQDATE <= ''' + @REQDATE_END + ''') '
        if @AUTDATE_OP = 'E'
            select @sql = @sql + 'and AM.AUTHDATE = ''' + @AUTDATE_BEGIN + ''' '
        if @AUTDATE_OP = 'B'
            select @sql = @sql + 'and (AM.AUTHDATE >= ''' + @AUTDATE_BEGIN + ''' and AM.AUTHDATE <= ''' + @AUTDATE_END + ''') '
        if @EXPDATE_OP = 'E'
            select @sql = @sql + 'and AM.EXPRDATE = ''' + @EXPDATE_BEGIN + ''' '
        if @EXPDATE_OP = 'B'
            select @sql = @sql + 'and (AM.EXPRDATE >= ''' + @EXPDATE_BEGIN + ''' and AM.EXPRDATE <= ''' + @EXPDATE_END + ''') '
        if @FILTER <> '%'
            select @sql = @sql + 'and ' + @FILTER + ' '

我很难转换下面的代码片段

 WHERE
    CASE
        WHEN @REQDATE_OP = 'E' THEN AM.REQDATE = @REQDATE_BEGIN
        WHEN @REQDATE_OP = 'B' THEN (AM.REQDATE >= @REQDATE_BEGIN AND AM.REQDATE <= @REQDATE_END)
        ....
        ....

    AM.DBKEY IN ( DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY, @GUID))
    AND AM.DBKEY = PM.DBKEY
    AND AM.AUTHPCP = PM.PROVID

我尝试过实现CASE语句,但语法不正确

import java.util.stream.LongStream;

/*
the goal is to find the least number that is greater than n and the digit sum of that number can be fully divided by 11.
 */
public class M1 {
  private static long nextCRC(int n) {
    return LongStream.iterate(n + 1, l -> ++l)
      .filter(l -> sumOfBase10Digits(l) % 11 == 0)
      .findFirst().getAsLong();
  }

  private static long sumOfBase10Digits(final long num) {
    if (num == 0) {
      return 0;
    } else {
      return num % 10 + sumOfBase10Digits(num / 10);
    }
  }

  public static void main(String... args) {
    System.out.println(nextCRC(20));
    System.out.println(nextCRC(200));
    System.out.println(nextCRC(98765));
    System.out.println(nextCRC(92));
  }
}
  

CASE声明是正确的方法吗?如果是这样,CASE应该怎么做   声明要重写吗?还有其他替代方法吗?

1 个答案:

答案 0 :(得分:1)

您可以改进此代码的构造,但是使用@FILTER条件,您不可能在不重新定义整个过程的情况下摆脱动态代码。以下是单个SELECT中的整个块:

select @sql = 'select AM.AUTHNO, 
    AST.DESCR, 
    AM.MEMBID_DISP as ''MEMBID'', 
    convert(varchar(25),isnull(AM.MEMBNAME,'''')) as member_name, 
    isnull(MM.SEX,'''') as member_sex, 
    isnull(MM.BIRTH,'''') as member_birth, 
    convert(varchar(25),isnull(HC.HPNAME,'''')) as member_healthplan, 
    convert(varchar(25),isnull(PM.lastname,'''') + isnull('', '' + PM.firstname,'''')) as provider_name, 
    PM.PROVID_DISP as ''PROVID'', 
    ''1'' as ''SORT_ORDER'', AM.MEMBID as ''MEMB_KEYID'', AM.AUTHPCP as ''PROV_KEYID'' 
    , AM.DBKEY as ''DBKEY'' 
    , (select DESCR from DB where [KEY] = AM.DBKEY) as ''IPA'' 
    , AM.AUTHDATE, AM.REQDATE 
    from Z_AUTH_MASTERS AM
    INNER JOIN Z_AUTH_STATUS AST
        ON AM.DBKEY = AST.DBKEY  
        and AM.STATUS = AST.CODE 
    INNER JOIN Z_PROV_MASTERS PM
        ON AM.DBKEY = PM.DBKEY  
        and AM.AUTHPCP = PM.PROVID 
    INNER JOIN Z_MEMB_MASTERS MM
        ON AM.DBKEY = MM.DBKEY  
        and AM.MEMBID = MM.MEMBID 
    INNER JOIN Z_HP_CONTRACTS HC 
        ON AM.DBKEY = HC.DBKEY  
        and AM.HPCODE = HC.HPCODE   
    where AM.DBKEY in (' + DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY,@GUID) + ') ' +   -- **** CUSTOM
    CASE
        WHEN @REFPROV <> '%'
            THEN 'and AM.AUTHPCP_DISP = ''' + @REFPROV + ''' ' 
        ELSE ''
    END + CASE
        WHEN @AUTHNO <> '%'
            THEN 'and AM.AUTHNO like ''' + @AUTHNO + ''' '
        ELSE ''
    END + CASE
        WHEN @STATUS <> '%'
            THEN 'and AM.STATUS = ''' + @STATUS + ''' '
        ELSE ''
    END + CASE
        WHEN @REQPROV_FIRST <> '%'
            THEN 'and PM.FIRSTNAME like ''' + @REQPROV_FIRST + ''' '
        ELSE ''
    END + CASE
        WHEN @REQPROV_LAST <> '%'
            THEN 'and PM.LASTNAME like ''' + @REQPROV_LAST + ''' '
        ELSE ''
    END + CASE
        WHEN @MEMB_FIRST <> '%'
            THEN 'and MM.FIRSTNM like ''' + @MEMB_FIRST + ''' '
        ELSE ''
    END + CASE
        WHEN @MEMB_LAST <> '%'
            THEN 'and MM.LASTNM like ''' + @MEMB_LAST + ''' '
        ELSE ''
    END + CASE
        WHEN @MEMB_ID <> '%'
            THEN 'and AM.MEMBID_DISP like ''' + @MEMB_ID + ''' '
        ELSE ''
    END + CASE @REQDATE_OP
        WHEN 'E'
            THEN 'and AM.REQDATE = ''' + @REQDATE_BEGIN + ''' '
        WHEN 'B'
            THEN 'and (AM.REQDATE >= ''' + @REQDATE_BEGIN + ''' and AM.REQDATE <= ''' + @REQDATE_END + ''') '
    END + CASE @AUTDATE_OP 
        WHEN 'E'
            THEN 'and AM.AUTHDATE = ''' + @AUTDATE_BEGIN + ''' '
        WHEN 'B'
            THEN 'and (AM.AUTHDATE >= ''' + @AUTDATE_BEGIN + ''' and AM.AUTHDATE <= ''' + @AUTDATE_END + ''') '
    END + CASE @EXPDATE_OP 
        WHEN 'E'
            THEN 'and AM.EXPRDATE = ''' + @EXPDATE_BEGIN + ''' '
        WHEN 'B'
            THEN 'and (AM.EXPRDATE >= ''' + @EXPDATE_BEGIN + ''' and AM.EXPRDATE <= ''' + @EXPDATE_END + ''') '
    END + CASE
        WHEN @FILTER <> '%'
            THEN 'and ' + @FILTER + ' '
        ELSE ''
    END + CASE        -- only show auths where the Authpcp or reqprov are providers that are linked to this user
        WHEN @IS_SUPER=0
            THEN 'and ((AM.AUTHPCP in (SELECT PROVID from USER_PROVIDERS where USER_ID = ' + convert(varchar(10),@USER_ID) + ' and DBKEY in (' + DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY,@GUID) + '))) 
        or (AM.REQPROV in (SELECT PROVID from USER_PROVIDERS where USER_ID = ' + convert(varchar(10),@USER_ID) + ' and DBKEY in (' + DBO.C_SP_AUTH_SEARCH_DBKEY(@DB_KEY,@GUID) + ')))) '
    END + 'order by SORT_ORDER asc' +
    CASE @SORT_COLUMN 
        WHEN 1
            THEN ', AM.AUTHNO desc'
        WHEN 2
            THEN ', AM.STATUS asc'
        WHEN 3
            THEN ', AM.MEMBID_DISP asc'
        WHEN 4
            THEN ', MM.LASTNM asc'
        WHEN 5
            THEN ', MM.SEX asc'
        WHEN 6
            THEN ', MM.BIRTH asc'
        WHEN 7
            THEN ', HC.HPNAME asc'
        WHEN 8
            THEN ', PM.LASTNAME asc'
        WHEN 9
            THEN ', ''IPA'' asc'
    END

exec sp_executesql @sql

此外,还有一个注意事项:为什么常量上有一个排序? (WHEN 9条件)。只需删除它,因为它根本不会改变排序顺序。