22,210
社区成员
发帖
与我相关
我的任务
分享
--测试数据
if not object_id(N'Tempdb..#tb1') is null
drop table #tb1
Go
Create table #tb1([RecordID] int,[ToDoUserID] nvarchar(27))
Insert #tb1
select 1,N'1,2' union all
select 2,null union all
select 3,N'1,2,3,4' union all
select 4,N'2,4'
GO
if not object_id(N'Tempdb..#tbUser') is null
drop table #tbUser
Go
Create table #tbUser([UserID] int,[sName] nvarchar(22))
Insert #tbUser
select 1,N'小张' union all
select 2,N'小王' union all
select 3,N'小李' union all
select 4,N'小赵'
Go
--测试数据结束
SELECT RecordID ,
STUFF(( SELECT ',' + sName
FROM #tbUser
WHERE CHARINDEX(',' + RTRIM(UserID) + ',',
',' + ToDoUserID + ',') > 0
FOR
XML PATH('')
), 1, 1, '') AS ToDoUser
FROM #tb1