Solved

Recursive function not working correctly Sql Server

Posted on 2008-11-03
3
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

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.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

733 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