Solved

Recursive function not working correctly Sql Server

Posted on 2008-11-03
3
222 Views
Last Modified: 2012-06-21
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
Comment
Question by:jeremyj54
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 250 total points
ID: 22868108
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
 
LVL 60

Expert Comment

by:chapmandew
ID: 22868141
also consider using a recursive CTE...might get some performance gains out of it.
0
 

Author Comment

by:jeremyj54
ID: 22868475
That worked great.  How would this be done as CTE?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question