Manager Master table
经理主表
And user table
和用户表
I want comma separated manager name using master table .
我想用逗号表分隔逗号分隔的管理器名称。
i want result in this form
我想要这种形式的结果
My query is like this
我的查询是这样的
declare @ststring varchar(max)= select manager
from customer
CREATE TABLE #tblUserCustomer ( inCustomerID iNT)
Insert into #tblUserCustomer
select stvalue from dbo.getlistfrmcommaseprated_int('1,2,4')
declare @stcscont varchar(1000)
select @stcscont = (select Um.Name + ',' from master um
join #tblUserCustomer T on t.inCustomerID = um.inuserid
FOR XML PATH('')
)
print @stcscont
drop table #tblUserCustomer
this query can be help full for single data but i have multiple row
这个查询可以帮助完整的单个数据,但我有多行
1 个解决方案
#1
1
I don't have your parse function, so I substituted mine. [dbo].[udf-Str-Parse]
which returns RETVAL
我没有你的解析功能,所以我替换了我的。 [dbo]。[udf-Str-Parse]返回RETVAL
Some Sample Data
一些示例数据
Declare @Manager table (ID int,Name varchar(25))
Insert Into @Manager values
(1,'Ajay'),
(2,'Sarita'),
(3,'Akash'),
(4,'Niti')
Declare @User table (ID int,Name varchar(25),Manager varchar(25))
Insert Into @User values
(1,'Javed','1,3'),
(2,'Ruma' ,'1,4')
Actual Query
实际查询
Select A.*
,ManagerName = B.String
From @User A
Cross Apply (
Select String = Stuff((Select ',' +Name
From (
Select M.Name,ML.RetSeq
From @Manager M
Join [dbo].[udf-Str-Parse](A.Manager,',') ML
on M.ID=ML.RetVal
) X
Order By RetSeq
For XML Path ('')),1,1,'')
) B
Returns
返回
ID Name Manager ManagerName
1 Javed 1,3 Ajay,Akash
2 Ruma 1,4 Ajay,Niti
EDIT - Added my Parse Function
编辑 - 添加了我的解析功能
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as X
Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[udf-Str-Parse]('this,is,<test>,for,< & >',',')
--Performance On a 5,000 random sample -8K 77.8ms, -1M 79ms (+1.16), -- 91.66ms (+13.8)
#1
1
I don't have your parse function, so I substituted mine. [dbo].[udf-Str-Parse]
which returns RETVAL
我没有你的解析功能,所以我替换了我的。 [dbo]。[udf-Str-Parse]返回RETVAL
Some Sample Data
一些示例数据
Declare @Manager table (ID int,Name varchar(25))
Insert Into @Manager values
(1,'Ajay'),
(2,'Sarita'),
(3,'Akash'),
(4,'Niti')
Declare @User table (ID int,Name varchar(25),Manager varchar(25))
Insert Into @User values
(1,'Javed','1,3'),
(2,'Ruma' ,'1,4')
Actual Query
实际查询
Select A.*
,ManagerName = B.String
From @User A
Cross Apply (
Select String = Stuff((Select ',' +Name
From (
Select M.Name,ML.RetSeq
From @Manager M
Join [dbo].[udf-Str-Parse](A.Manager,',') ML
on M.ID=ML.RetVal
) X
Order By RetSeq
For XML Path ('')),1,1,'')
) B
Returns
返回
ID Name Manager ManagerName
1 Javed 1,3 Ajay,Akash
2 Ruma 1,4 Ajay,Niti
EDIT - Added my Parse Function
编辑 - 添加了我的解析功能
CREATE FUNCTION [dbo].[udf-Str-Parse] (@String varchar(max),@Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(@String,@Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as X
Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
--Select * from [dbo].[udf-Str-Parse]('this,is,<test>,for,< & >',',')
--Performance On a 5,000 random sample -8K 77.8ms, -1M 79ms (+1.16), -- 91.66ms (+13.8)