• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 231
  • Last Modified:

Recursive function not working correctly Sql Server

I have a table I have attached a small part of that has demand ID's and supply ids, what I need is to give a demand ID and have the function look for all of the rows that have that demand ID and give me the supply ID on that line, then take all those supply id's and check to see if they are in the demand ID column also and if they are to give me the supply ID for those and so on.  I have the function below, but if I manually do work it out there should only be 14 results for the demand I put in but it returns 60 and some of them are not related, any help would be appreciated.

USE [fede]
GO
/****** Object:  UserDefinedFunction [dbo].[fn_GetSupply]    Script Date: 11/03/2008 09:46:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[fn_GetSupply]
(
        -- Input parameters
        @demand varchar(20)
)
RETURNS
@TBL TABLE
(
        -- Returned table layout
        supply varchar(20),
                  demand varchar(20)
)

AS
BEGIN

      DECLARE @currentID varchar(20)  -- item currently being processed
                  
        -- grab first child record
        SELECT @currentID = MIN(supply_base_id)
        FROM dbo.DEMAND_SUPPLY_LINK WHERE demand_base_id = @demand and Demand_Type = 'RQ' and Supply_Type = 'WO'      
 
       

WHILE @currentID IS NOT NULL
                BEGIN
                        -- explode downward
                     INSERT INTO @TBL
            SELECT supply, @Demand
           FROM dbo.fn_GetSupply(@currentID)
                       
-- insert id matching input
        INSERT INTO @TBL
        SELECT supply_base_id, demand_base_id
        FROM dbo.DEMAND_SUPPLY_LINK WHERE supply_base_id = @currentID and Demand_Type = 'RQ' and Supply_Type = 'WO'
 
                        -- grab the next record
                        SELECT @currentID = MIN(supply_base_id)
        FROM dbo.DEMAND_SUPPLY_LINK WHERE demand_base_id = @demand AND supply_base_id > @currentID and Demand_Type = 'RQ' and Supply_Type = 'WO'
                END
        RETURN
END
demand-supply.xls
0
jeremyj54
Asked:
jeremyj54
1 Solution
 
Kevin CrossChief Technology OfficerCommented:
This recursive function works well when the supply_base_id (ID being looked up) is unique in its column -- the demand can appear in multiple supplies but once you find that supply the id can be used as a key.  If that is not the case, then this section of code:

-- insert id matching input
        INSERT INTO @TBL
        SELECT supply_base_id, demand_base_id
        FROM dbo.DEMAND_SUPPLY_LINK WHERE supply_base_id = @currentID and Demand_Type = 'RQ' and Supply_Type = 'WO'

Will have to be changed to this:

-- insert id matching input
        INSERT INTO @TBL
        SELECT supply_base_id, demand_base_id
        FROM dbo.DEMAND_SUPPLY_LINK WHERE supply_base_id = @currentID and  demand_base_id = @demand AND Demand_Type = 'RQ' and Supply_Type = 'WO'

That should fix the issues.
0
 
chapmandewCommented:
also consider using a recursive CTE...might get some performance gains out of it.
0
 
jeremyj54Author Commented:
That worked great.  How would this be done as CTE?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now