hello all,in my table [Code]Cust_Id RelationID144 39,40,2832 39,40,41,283so i have written cursor to split the values after the result is coming now i have basing on the ID i need to bring relations ElderBrother,Father,Mother,Self,YoungerBrotherif i made a single select in front application it need to show ElderBrotherbut it giving all ElderBrother,Father,Mother,Self,YoungerBrotherhere is my cursor code CREATE TABLE #CustCategory ( Cust_ID INT , RelationShipID INT --CategoryID INT ) DECLARE @i_Cust_ID INT , @vc_RelationShipID VARCHAR(50) --@vc_CategoryID VARCHAR(50) DECLARE CUR_Category CURSOR FOR SELECT Cust_ID , RelationShipID --CategoryID FROM Cust_DailyVistedCustomers WHERE Cust_Id is not null OPEN CUR_Category FETCH NEXT FROM CUR_Category INTO @i_Cust_ID,@vc_RelationShipID--,@vc_CategoryID WHILE @@FETCH_STATUS = 0 BEGIN INSERT INTO #CustCategory ( Cust_ID , RelationShipID --CategoryID ) SELECT DISTINCT @i_Cust_ID , DT1.RelationShipID --DT2.CategoryID FROM (SELECT DISTINCT @i_Cust_ID AS Cust_ID, KeyValue AS RelationShipID FROM dbo.udf_SplitStringToTable(@vc_RelationShipID,',')) DT1 --CROSS JOIN --(SELECT -- @i_Cust_ID AS Cust_ID, -- KeyValue AS CategoryID --FROM dbo.udf_SplitStringToTable(@vc_CategoryID,','))DT2 WHERE NOT EXISTS (SELECT DISTINCT 1 FROM #CustCategory TCC WHERE TCC.Cust_ID = DT1.Cust_ID --AND TCC.CategoryID = DT2.CategoryID AND TCC.RelationShipID = DT1.RelationShipID) FETCH NEXT FROM CUR_Category INTO @i_Cust_ID, @vc_RelationShipID --@vc_CategoryID END CLOSE CUR_Category DEALLOCATE CUR_Category and my select query : SELECT DISTINCT L.ProfileID , B.FirstName + B.LastName AS NAME, STUFF(( SELECT DISTINCT '','' + CDV.MetaValueDescription FROM Cust_DailyVistedCustomers p INNER JOIN #CustCategory ds ON p.Cust_ID = ds.Cust_ID INNER JOIN Mst_Meta_Values CDV ON CDV.Meta_Value_ID= ds.RelationShipID WHERE ds.Cust_ID = CC.Cust_ID FOR XML PATH('''')) , 1 , 1 , '''') AS Relation, CDV.RelationsName, E.FirstName + E.LastName AS AttendedBy FROM #CustCategory CC INNER JOIN Cust_BasicInfo B ON B.Cust_ID = CC.Cust_ID INNER JOIN Cust_Login L ON L.Cust_ID = B.Cust_ID [/code]P.V.P.MOhan |