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
Voir: http: //archive.msdn .microsoft.com/SQLExamples/Wiki/View.aspx? title = createacommadelimitedlist – Magnus
Alors, quelle est la question? Est-ce que l'UDF ne vous obtient pas les bons résultats, ou autre chose ...? –
Je reçois la même ligne pour les deux ... –