Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Thursday, March 13, 2014

SQL Server Get all rows records in one value

Use for xml PATH ('')

_______________________________________________
Select
replace(replace((
Select Name from dbo.[Fn_GetTableFunction](ISNULL(SuppTeamMemAct,''))
for xml PATH ('')
),'<Name>',''),'</Name>',', ')

_______________________________________________

CREATE FUNCTION [dbo].[Fn_GetTableFunction]    
              (@CompositeValue varchar(1000))    
RETURNS @tblTmp TABLE(  
ID          INT,  
Name           VARCHAR(MAX)  
)  
AS    
BEGIN  
   
  insert into @tblTmp  
  Select ID,Name from TestTable
  where ID in  
  (  
 Select Value  from dbo.[SplitDelimited](@CompositeValue,'#')  
  )  
 return;  
   
END    
 
_______________________________________________

CREATE FUNCTION [dbo].[SplitDelimited]      
(      
 @List NVARCHAR(MAX),      
 @SplitOn nvarchar(10)      
)      
RETURNS @RtnValue table (      
 Id int identity(1,1),      
 Value NVARCHAR(MAX)      
)      
AS      
BEGIN      
 While (Charindex(@SplitOn,@List)>0)      
 Begin      
  Insert Into @RtnValue (value)      
  Select      
   Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))      
  Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))      
 End      
       
 Insert Into @RtnValue (Value)      
    Select Value = ltrim(rtrim(@List))      
     
    Return      
END