27,579
社区成员
发帖
与我相关
我的任务
分享
需要建立”人員基本信息表”和“人員職稱信息關系表,再通過關系就可以得到你想要的數據!
人员基本信息表
人员职称关系表
结果
-------------------------
zc xm
会计师 bb,aa
人力资源管理师 bb
审计师 cc,bb
统计师 dd
create table #temp
(
xm varchar(20),
zc varchar(100)
)
declare aaa cursor
for select * from bb where charindex('、',zc)>0
open aaa
declare @xm varchar(100),@zc varchar(100)
fetch next from aaa into @xm,@zc
while @@fetch_status=0
begin
insert into #temp select @xm,textstr from dbo.split(@zc,'、')
fetch next from aaa into @xm,@zc
end
close aaa
deallocate aaa
insert into #temp
select * from bb where xm not in (select xm from #temp)
select * into #temp2 from #temp order by zc
declare @xm varchar(100),@zc varchar(100)
select @xm='',@zc=''
update #temp2 set @xm=xm=case when charindex(zc,@zc)>0 then @xm+','+xm else xm end,@zc=zc
IF OBJECT_ID('split', 'tf') IS NOT NULL
Drop function split
GO
create function split
(
@str varchar(2000),
@spl varchar(2)=','
)
returns @table table
(
id int identity(1,1),
textstr varchar(200)
)
as
begin
declare @dang int
declare @next int
declare @text varchar(200)
set @dang=1
while @dang<=len(@str)
begin
set @next=charindex(@spl,@str,@dang)
if @next=0 or @next is null
set @next=len(@str)+1
set @text=substring(@str,@dang,@next-@dang)
insert into @table values(@text)
set @dang=@next+1
end
return
end
go