问一个sql解决解析的问题[希望是通过纯sql语句解决]
有2个表表A------------------------字段名/AUTH/AUTHNAME/ 12 /前锋/ 7 /守门/ 244/教练------------------------表B------------------------字段名/AUTHG/AUTHGNAME/ 1 /12,7/ 2 /244/ 3 /12,7,244------------------------希望得到的查询结果1/前锋,守门2/教练3/前锋,守门,教练等于是把数字解析成文字,谢谢
参考答案:先建立一个和表B一样结构的表C,然后用游标实现
declare @source varchar(20)
declare @desc varchar(20)
declare @lstr varchar(20)
declare @rstr varchar(20)
declare @memo varchar(20)
declare @id int
declare @k int
declare cs1 cursor for
select AUTHG,AUTHGNAME from 表B
truncate table 表C
open cs1
fetch next from cs1 into @id,@source
while @@FETCH_STATUS = 0
begin
set @source=@source+','
set @desc=''
set @rstr=@source
set @k=charindex(',',@rstr)
while @k>0
begin
set @lstr=substring(@rstr,1,charindex(',',@rstr)-1)
set @rstr=substring(@rstr,charindex(',',@rstr)+1,len(@rstr)-charindex(',',@rstr))
select @memo=AUTHNAME from 表A where AUTH=@lstr
set @desc=@desc+@memo+','
set @k=charindex(',',@rstr)
end
insert into 表C values(@id,left(@desc,len(@desc)-1))
fetch next from cs1 into @id,@source
end
close cs1
deallocate cs1
select * from 表C