Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Multiple values are coming through cursor

Author  Topic 

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2013-04-12 : 05:41:16
hello all,

in my table

[Code]
Cust_Id RelationID
144 39,40,283
2 39,40,41,283


so 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,YoungerBrother

if i made a single select in front application it need to show

ElderBrother

but it giving all

ElderBrother,Father,Mother,Self,YoungerBrother

here 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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-04-12 : 05:48:41
why do you need a cursor? if you want to split values why not use either of below approaches?

http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html

http://visakhm.blogspot.in/2013/01/delimited-string-split-xml-parsing.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -