E9流程节点根据明细表的多人力资源字段去寻找矩阵表的经理\总监办理人
- 需求概况
如图,流程里面有明细表多人力资源字段,需要用到这些被选中的人员去部门矩阵里面获取对应的部门经理,部门总监来作为节点办理人。
- 解决思路
一开始,想用外部接口里的自定义SQL来实现,但写出来的SQL提示多人力资源字段是text类型,跟HrmResource的departmentid不符合,所以无法做对应关联查询。
[Microsoft][SQL Server Native Client 11.0][SQL Server]操作数类型冲突: text 与 int 不兼容 (206)
select departmentid,* from HrmResource where id in (select zdzrr from formtable_main_661_dt1 where mainid in (select id from formtable_main_661 where requestid=898498))
发现怎么转换都不成功,遂想用自定义接口实现。
但接口其实也有问题,list里有多个string,但最终只会产生一个办理人,改用2422,440这种格式,也一样。
然后开始百度字符串行转列的方法。
搜索到string_split 函数,但该函数在SqlServer2016版本之后才有,目前E9用的2008版本,需要实现一个替代的函数。
- 最终解决方法
步骤1:实现string_split函数
详见博客https://blog.csdn.net/weixin_43263355/article/details/125893550
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE FUNCTION [dbo].[fn_Split] (@input nvarchar(max), @separater char(1)) RETURNS @result TABLE ( value nvarchar(max) not null ) AS BEGIN declare @tmp nvarchar (max) declare @EndIndex int SET @input = @input + @separater WHILE LEN(@input) > 0 BEGIN SET @EndIndex = CHARINDEX(@separater, @input) SET @tmp = SUBSTRING(@input, 1, @EndIndex - 1); IF LEN(@tmp) > 0 INSERT INTO @result SELECT @tmp SET @input = SUBSTRING(@input, @EndIndex+1, LEN(@input)- @EndIndex) END RETURN END GO
步骤2:编写自定义SQL
根据主导责任人的部门去寻找部门矩阵对应的经理
select bmjl from Matrixtable_2 where id in
( select departmentid from HrmResource where cast(id as nvarchar) in
( select value from fn_Split((select STUFF( (select ','+cast(a.zdzrr as nvarchar) from (select zdzrr from formtable_main_661_dt1 where mainid in
(select id from formtable_main_661 where requestid=$requestid$)) as a for xml Path('') ) ,1,1,'') as userid),',')) )
- 思考
这个问题,用了两天解决,第一天因为没有对应的知识贮备,搜索问题的关键字只能根据思路模糊的去搜一些词语,不是很精准。
第一天尝试解决但没有立马解决,之后便因为其它工作放一边了。
第二天继续解决,因为一直思考这个问题,才开始有了大概思路,搜索到了fn_Split关键字并解决了问题。
变种:
根据查询出来的数据,判断是否含有,逗号,如果有证明是多个人,办理人为创建人,没有则为自身。
SELECT CASE WHEN charindex(',',userid) > 0 THEN '123123' ELSE userid END from ( select STUFF((select ','+CAST(shry AS varchar) from ( select shry from uf_yfbdshgxb where hqbmmc in( select S.selectname from workflow_base B, workflow_billfield F, workflow_SelectItem S where B.Formid = F.Billid and F.Id = S.Fieldid and B.id = 2960 and F.Fieldname = 'psbm' and S.Selectvalue in (99) ) and lx = '变更申请(ECR)' UNION ALL select shry from uf_yfbdshgxb where hqbmmc in( select S.selectname from workflow_base B, workflow_billfield F, workflow_SelectItem S where B.Formid = F.Billid and F.Id = S.Fieldid and B.id = 2960 and F.Fieldname = 'psbmlg' and S.Selectvalue in (99) ) and lx = '变更申请(ECR)' UNION ALL select shry from uf_yfbdshgxb where hqbmmc in( select S.selectname from workflow_base B, workflow_billfield F, workflow_SelectItem S where B.Formid = F.Billid and F.Id = S.Fieldid and B.id = 2960 and F.Fieldname = 'psbmpt' and S.Selectvalue in (99) ) and lx = '变更申请(ECR)' UNION ALL select shry from uf_yfbdshgxb where hqbmmc in( select S.selectname from workflow_base B, workflow_billfield F, workflow_SelectItem S where B.Formid = F.Billid and F.Id = S.Fieldid and B.id = 2960 and F.Fieldname = 'psbmhf' and S.Selectvalue in (0) ) and lx = '变更申请(ECR)' ) AS subquery FOR XML PATH('')),1,1,'') as userid ) a