Solved

Recursive function not working correctly Sql Server

Posted on 2008-11-03
3
218 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
Comment Utility
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
Comment Utility
also consider using a recursive CTE...might get some performance gains out of it.
0
 

Author Comment

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

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now