如何在 SQL Server 2000 中编写递归查询

发布于 2024-08-11 23:36:10 字数 1048 浏览 3 评论 0原文

我有一个表,其中有一个看起来像这样的列表

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)

查询应该在记录中逐一进行,并根据引用生成一个值,选择第一个,即报告名称 A ,第 1.1 行,现在引用为 (B, 24.1),这意味着我们需要找到报表名称 B,第 24.1 行并选择其值。在同一个表 R.Name B 和 Line No B, 24.1 被 (B, 23.1) 引用,所以现在我们需要找到 Report name B, Line No 23.1,我们继续迭代,除非我们找不到引用同一张表,这意味着我们找不到的最后一个表在另一个表中有值。 (此表中仅找到没有值的引用)...看下表

表:GenerateValues

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 引用B,24.1 哪些引用B,23.1 哪些引用A,1.2 以及自从A , 1.2 不存在于引用表中,另一个查询运行并从生成值表中获取该数字。在本例中为 5632,因此 A,1.1 = 5632。

像这样,我们逐条浏览每条记录。

我的问题是我不知道如何编写递归查询来实现这一点。

侯赛因

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铁轨上的流浪者 2024-08-18 23:36:10

让我们来分解一下。

首先,一个 UDF 来获取下一个“值”

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

接下来一个 UDF 来查找每个条目的最终值:

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

最后,我们可以运行 SELECT

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

我希望这一切都清楚,并且我没有犯任何语法错误。使用 UDF 可能意味着此查询不会像您希望的那样运行得那么快,但我认为 UDF 在任何情况下都会对您有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文