2011-07-11 3 views
1

J'ai déjà posé une question sur le même schéma. Cette fois, mon exigence est de prendre la sortie de la table. Pour le schéma, s'il vous plaît consulter ce questionAffichage des valeurs séparées par des virgules de la table

Maintenant ce que je veux interroger la société Id de la table de carte et afficher toutes les sous-catégories d'une catégorie comme Comma Separated Value. Le scénario est quelque chose comme tht. Si je suis connecté en tant que société ID 1. et j'ai 3 catégories différentes. Ensuite, dans ma grille, il affiche:

**Category**         **Subcategory** 

Category1    SubCat1, Subcat2, Subcat3 and so on 
Category2    SubCat4, Subcat62, Subcat6 and so on 

Ma requête existante est la suivante:

SELECT tbl_Category.Name , dbo.fnGetCSValues(@CompanyId) AS Subcategory 
     FROM tbl_Company_Category_Map 
    INNER JOIN tbl_Category ON tbl_Company_Category_Map.CategoryId = tbl_Category.Id 
    INNER JOIN tbl_SubCategory ON tbl_Company_Category_Map.SubCategoryId = tbl_SubCategory.Id 
    WHERE tbl_Company_Category_Map.CompanyId = @CompanyId 
    GROUP BY tbl_Category.Name 

Avec une UDF

ALTER FUNCTION [dbo].[fnGetCSValues] (@Id INT) 

RETURNS VARCHAR(8000) 
AS 
BEGIN 

DECLARE @List VARCHAR(8000) 
SELECT @List =(SELECT tbl_SubCategory.Name  
       FROM  tbl_Company_Category_Map INNER JOIN 
          tbl_Category ON tbl_Company_Category_Map.CategoryId = tbl_Category.Id INNER JOIN 
          tbl_SubCategory ON tbl_Company_Category_Map.SubCategoryId = tbl_SubCategory.Id 
       WHERE tbl_Company_Category_Map.CompanyId = @Id 
       ORDER by tbl_SubCategory.Name 

       FOR XML PATH('')) 
RETURN LEFT(@List,(LEN(@List) -1)) 
END 
+1

Voir: http: //archive.msdn .microsoft.com/SQLExamples/Wiki/View.aspx? title = createacommadelimitedlist – Magnus

+0

Alors, quelle est la question? Est-ce que l'UDF ne vous obtient pas les bons résultats, ou autre chose ...? –

+0

Je reçois la même ligne pour les deux ... –

Répondre

1
Declare @Table Table 
(
    Category varchar(100),Subcategory varchar(MAX) 
) 
insert into @Table 
Select 'Category1','SubCat1,Subcat2,Subcat3' UNION ALL 
Select 'Category2','SubCat4,Subcat62,Subcat6' 



--1st option to use master..sptvalue 

Select Category,SUBSTRING(',' + Subcategory + ',', n.Number + 1, CHARINDEX(',', ',' + Subcategory + ',', n.Number + 1) - n.Number - 1) 
From @Table As T 
Cross Join master..spt_values As n 
WHERE n.Type = 'p' 
AND n.Number > 0 
AND n.Number < LEN(',' + Subcategory + ',') 
and SUBSTRING(',' + Subcategory + ',', n.Number, 1)=',' 

--2nd option to use split function 
CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1))  
returns @temptable TABLE (items varchar(8000))  
as  
begin  
    declare @idx int  
    declare @slice varchar(8000)  

    select @idx = 1  
     if len(@String)<1 or @String is null return  

    while @idx!= 0  
    begin  
     set @idx = charindex(@Delimiter,@String)  
     if @idx!=0  
      set @slice = left(@String,@idx - 1)  
     else  
      set @slice = @String  

     if(len(@slice)>0) 
      insert into @temptable(Items) values(@slice)  

     set @String = right(@String,len(@String) - @idx)  
     if len(@String) = 0 break  
    end 
return  
end 

--call query here 
Select Category,items as Subcategory 
From @Table As T 
Cross Apply dbo.Split(Subcategory,',') 
Questions connexes