?
Solved

Recursive function not working correctly Sql Server

Posted on 2008-11-03
3
Medium Priority
?
226 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 60

Accepted Solution

by:
Kevin Cross earned 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

765 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