DECLARE @temp VARCHAR(20) --申明变量, 也就是输入参数
SET @temp='a,b' --为变量赋值
WITH aa(a,b,c,d) AS --处理数据库中数据
(
SELECT benji,ASCII(shangji),1,shangji FROM [dengji]
UNION ALL
SELECT benji,b+ASCII(SUBSTRING(shangji,c+1,1)),c+1,shangji FROM aa ,dengji WHERE dengji.[benji]=aa.a AND dengji.[shangji]=aa.d AND c<len(shangji)
),
bb AS
(
SELECT a,d,MAX(b)AS b FROM aa GROUP BY a,d
),
cc AS --得到处理结果
(
SELECT a,d,SUM(b) OVER (PARTITION BY a) AS b FROM bb
),
dd(b,a) AS --处理传入参数
(
SELECT ASCII(@temp),1
UNION ALL
SELECT b+ASCII(SUBSTRING(REPLACE(@temp,',',''),a+1,1)),a+1 FROM dd WHERE a<len(REPLACE(@temp,',',''))
),
ff AS --匹配数据
(
SELECT a,b,d FROM cc WHERE cc.b=(SELECT MAX(dd.b) FROM dd)
),
ee AS --验证数据正确行,二次过滤
(
SELECT *,COUNT(a) OVER (PARTITION BY a) AS c FROM ff WHERE @temp LIKE '%'+d+'%'
)
SELECT a,d FROM ee --得到结果
只需要用,号将要包含的字段内容隔开就可以了。拓展性极高