English 中文(简体)
How to write a recursive query in SQL Server 2000
原标题:

I have a table which has a list which looks like this

References

R. Name    LineNo.       References
  A         1.1           (B,24.1)
  A         6.3           (A, 1.3), (D, 22.1)
  B         23.1          (A. 1.2)
  B         24.1          (B,23.1)
  C         2             (A, 1.1)
  D         3.12          (A, 6.3)

The query should go one by one in the records and generate a value based on the references, pick first one lets say, which is Report Name A, Line No. 1.1, Now the reference is (B, 24.1), which means we need to find Report Name B, line no 24.1 and pick its value. In the same table R.Name B and Line No B, 24.1 is referenced by (B, 23.1), So now we need to find Report name B, Line No 23.1, We go on through the iteration unless we cant find a reference in the same table, which means the last one we cant find has a value in another table. (Only References without values are found in this table) ...Look at the table below

Table: GeneratedValues

R.Name  LineNo.    Values
----------------------------------------
A       1.2          5632
A       1.3          12.5
A       2.1          25
A       2.2          121
A       2.3          8

Now A, 1.1 References B, 24.1 Which references B, 23.1 Which references A, 1.2 And Since A, 1.2 doesnt exist in the reference table another query runs and fetches the number from the Generated Values table. In this case 5632, Hence A, 1.1 = 5632.

Like this we go one by one through each record.

My problem is i dont know how to write a recursive query to implement this.

Hussain

问题回答

Let s break it down.

Firstly, a UDF to get the next value

CREATE FUNCTION dbo.GetNextReference
(
    @CurrentRef varchar(25)
)
RETURNS varchar(25)
AS
BEGIN
    DECLARE @NextRef varchar(25)
    SELECT @NextRef = [References]
    FROM R
    WHERE  (  + [Name] +  ,  + [LineNo] +  )  = @CurrentRef

    RETURN @NextRef
END

Next one to find the final value for each entry :

CREATE FUNCTION dbo.GetFinalReference
(
    @StartRef varchar(25)
)
RETURNS varchar(25)
AS
BEGIN
    DECLARE @NextRef varchar(25), @CurrentRef varchar(25)
    SELECT @NextRef = dbo.GetNextReference(@StartRef), @CurrentRef = @StartRef
    WHILE @NextRef is not null
    BEGIN
        SET @CurrentRef = @NextRef
        SET @NextRef = dbo.GetNextReference(@CurrentRef)
    END

    --at this point @NextRef will be null, so we look in the other table
    DECLARE @FinalValue varchar(25)
    SELECT @FinalValue = [Values]
    FROM GeneratedValues
    WHERE  (  + [Name] +  ,  + [LineNo] +  )  = @CurrentRef

    RETURN @FinalValue
END

Finally, we can run a SELECT

SELECT [Name], [LineNo], dbo.GetFinalReference([References]) AS [Values]
FROM R

I hope this is all clear, and I haven t made any syntactical errors. The use of UDFs may mean this query won t run as quick as you d like, but I think the UDFs will be useful to you in any case.





相关问题
Big Table Advice (SQL Server)

I m experiencing massive slowness when accessing one of my tables and I need some re-factoring advice. Sorry if this is not the correct area for this sort of thing. I m working on a project that aims ...

How do I define a work week in SQL Server 2000?

I need to split a report by work week, and our work week is Saturday through Friday. How would I convert an ISO week from DATEPART(WW, ) into a work week?

SQL Sever Management Studio for SQL Server 2000

We use mostly SQL Server 2005 but have a few SQL Server 2000 servers laying around. Is there a SQL Server Management Studio like application for SQL Server 2000? Thank you.

Sql server 2005 on sql server 2000

I want to ask is there any issues or risks involved in installation of SQL Server 2005 Enterprise Edition on SQL Server 2000 Enterprise Edition in production server? Please tell me the guidelines ...

LINQ, Skip, OrderBy, and SQL Server 2000

I m accessing a data context object that is auto-generated by using LINQ to SQL. The SQL database is a SQL Server 2000 box. The class I m working with is a SQL View. I have a statement that is similar ...

Select Top 5 records of every employee in SQL Server

I have the following issue, I have this query that select the latest 5 records created for an employee: SELECT TOP 5 p.value, p.record_date AS FECHA FROM employee_loan_movements p ...

热门标签