--创建test表 createtable test(oid int,name char(20),lead char(10)) --往里面插入几行数据 insertinto test values(1,'测试公司','11') insertinto test values(1,'测试公司','12') insertinto test values(1,'测试公司','13') insertinto test values(1,'测试公司','14,15,16') --拆分字符串函数 alterfunction select_Dempart_Manager ( @oidint, @leadchar(10) ) returns@temptable(oid int,lead char(10)) as begin whilecharindex(',',@lead)>1 begin insertinto@tempvalues(@oid,left(@lead,charindex(',',@lead)-1)) set@lead=stuff(@lead,1,charindex(',',@lead),'') end insertinto@tempvalues(@oid,@lead) return end --使用apply函数调用 select a.oid,name,ST.lead from test a outer apply select_Dempart_Manager(a.oid,a.lead) as ST --结果 --oid name lead --1 测试公司 11 --1 测试公司 12 --1 测试公司 13 --1 测试公司 14 --1 测试公司 15 --1 测试公司 16