junwebhead
Programmer
Hi everyone I have a problem in creating a link records with a certain level of downlines.
What I mean is I want to get all the downlines of a customers n levels deep.
I have a customers table that has contain two fields that define their own downlines. first field is the customercode the second is the sponsorcode.
for example I want to get the downlines of customercode 3242
what I will do is search for all other customers with sponsorcode is 3242. That set of records is the customers 1st level downlines. then I will look up the 2nd level downlines by searching for other customers with sponsorcode using the 1st level customercode.
here is my solution so far but this takes 5 minutes to execute on a table with more than 200,000 records. what I want is the best and fastest solution for my problem. Am I using the cursor correctly? What am I missing here.
here is my first stored procedure:
Here is the spInsertToLevel that was called above inside the loop
Thanks I advance!
Jun
What I mean is I want to get all the downlines of a customers n levels deep.
I have a customers table that has contain two fields that define their own downlines. first field is the customercode the second is the sponsorcode.
for example I want to get the downlines of customercode 3242
what I will do is search for all other customers with sponsorcode is 3242. That set of records is the customers 1st level downlines. then I will look up the 2nd level downlines by searching for other customers with sponsorcode using the 1st level customercode.
here is my solution so far but this takes 5 minutes to execute on a table with more than 200,000 records. what I want is the best and fastest solution for my problem. Am I using the cursor correctly? What am I missing here.
here is my first stored procedure:
Code:
CREATE PROCEDURE dbo.spCustomerLevels
(
@CustCode int, @MaxLevel int = 1
)
AS
SET NOCOUNT ON
DELETE FROM tbltmpLevel
DECLARE @Level int, @Counter int, @CurrCode int, @CustomerCode int, @SponsorCode int
SET @Counter = 0
WHILE @Counter <> @MaxLevel
BEGIN
IF @Counter = 0
BEGIN /* get first level downlines */
SET @Level = @Counter + 1
EXECUTE spInsertToLevel @CustCode, @CustCode, @Level
END
ELSE
BEGIN /* get downlines from second level to maxlevel*/
SET @Level = @Counter
DECLARE Customer_Cursors CURSOR FAST_FORWARD FOR
SELECT CustomerCode, SponsorCode FROM tbltmpLevel
WHERE TopLevelCode = @CustCode AND CustomerLevel = @Level
OPEN Customer_Cursors
FETCH NEXT FROM Customer_Cursors INTO
@CustomerCode, @SponsorCode
WHILE @@FETCH_STATUS = 0
BEGIN
SET @Level = @Counter + 1
EXECUTE spInsertToLevel @CustomerCode, @CustCode, @Level
FETCH NEXT FROM Customer_Cursors INTO
@CustomerCode, @SponsorCode
END
CLOSE Customer_Cursors
DEALLOCATE Customer_Cursors
END
SET @Counter = @Counter + 1
END
RETURN
GO
Here is the spInsertToLevel that was called above inside the loop
Code:
CREATE PROCEDURE dbo.spInsertToLevel
(
@CustCode int, @TopLevelCode int, @Level int = 1
)
AS
SET NOCOUNT ON
INSERT INTO tbltmpLevel(CustomerCode, SponsorCode, CustomerLevel, TopLevelCode)
SELECT CustomerCode, SponsorCode, @Level, @TopLevelCode
FROM Customers WHERE SponsorCode = @CustCode AND CustLevel = 'DC'
RETURN
GO
Thanks I advance!
Jun