太能了,参考下吧,下面配套的是SqlServer2018
WHERE 1=1 AND DeleteFlag = 0
AND USER_CODE = $fine_username
${if(len(USER_CODE) = 0,""," and [YearMonth] = '" + yearmonth + "'")}
${if(len(yearmonth) = 0,""," and [YearMonth] = '" + yearmonth + "'")}
${if(len(attributiontype) == 0 || attributiontype=0," ","and attributiontype in (SELECT VALUE FROM STRING_SPLIT('"+ attributiontype +"',','))")}
${if(len(regiontreeid) = 0,""," and [MasterDeptTreeId] = '" + regiontreeid + "'")}
${if(len(region) = 0,""," and [MasterDeptName] = '" + region + "'")}