SQL循环更新

发布于 2024-10-17 15:00:48 字数 1124 浏览 0 评论 0原文

所以这就是交易。我得到了我们所说的“ProjectIDs”,这是我们数据库中的一个字段(主键),我们有 2 个字段需要根据项目 ID 中的信息进行更新。例如:

我可能有一个项目 ID

00068:

这个项目 ID 不会有我们所说的“阶段”...但我可能有另一个具有“阶段”的项目,该项目将有多个记录(即所有单独的作业),但通过差异字段。

0174:
0174-A-SEG-64:
0174-A-SEG-86:
0174-A-DEF-64:
0174-A-DEF-86:

例如:0174-A-SEG-64 将绑定到 0174-A-SEG0174- 的记录将更新 2 个字段A-SEG-64

ProjectIsPhase = -1 and ParentProjectID = 0174-A-SEG:

所以棘手的部分开始发挥作用。一些“主要”工作,如 0174-A-SEG:可能不存在。因此它们必须动态创建。理想情况下,正如您从给出的示例项目中看到的那样,0174-A:不存在,0174-A-SEG:或0174-A-DEF:也不存在,所以最终项目结构需要如下所示:

ProjectID           ProjectIsPhase               Parent ProjectID
0174: 
0174-A:             -1                           0174:
0174-A-SEG:         -1                           0174-A:
0174-A-DEF:         -1                           0174-A:
0174-A-SEG-64:      -1                           0174-A-SEG:

所以当它结束时在树视图中,它看起来如下所示

0174:
  0174-A:
    0174-A-SEG:
       0174-A-SEG-64:
    0174-A-DEF:

这一切都需要通过 SQL 或 T-SQL 完成

So here is the deal. I got what we call "ProjectIDs" which is a field in our database (Primary Key) we have 2 fields that need to be updated based on information that is within the project IDs. For example:

I might have a project ID

00068:

This projectID will not have what we call "Phases"... but I might have another Project that has "phases" which would have multiple records (i.e all separate jobs) but tied together by a diff fields.

0174:
0174-A-SEG-64:
0174-A-SEG-86:
0174-A-DEF-64:
0174-A-DEF-86:

So for example: 0174-A-SEG-64, would be tied to 0174-A-SEG: 2 fields would get updated for the record of 0174-A-SEG-64.

ProjectIsPhase = -1 and ParentProjectID = 0174-A-SEG:

So the tricky part comes into play. Some of the "main" jobs like 0174-A-SEG: might not exist. So they would have to be created on the fly. Ideally as you can see from the example projects given 0174-A: does not exist neither does 0174-A-SEG: or 0174-A-DEF: So in the end the project structure needs to look as the following:

ProjectID           ProjectIsPhase               Parent ProjectID
0174: 
0174-A:             -1                           0174:
0174-A-SEG:         -1                           0174-A:
0174-A-DEF:         -1                           0174-A:
0174-A-SEG-64:      -1                           0174-A-SEG:

So when it ends up in a tree view it looks like the following

0174:
  0174-A:
    0174-A-SEG:
       0174-A-SEG-64:
    0174-A-DEF:

This all needs to be done via SQL or T-SQL

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

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

发布评论

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

评论(1

野稚 2024-10-24 15:00:48

我不确定您到底想通过 ProjectIsPhase 实现什么目标,但我怀疑这是您问题的微不足道的部分。

给定 ProjectID,您可以按如下方式派生 ParentProjectID:(可能需要一些 +-1 调整来说明分隔符。)

LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID))

然后插入不支持的父项目不存在:

INSERT INTO Projects(ProjectID)
SELECT  LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID))
FROM    Projects p
WHERE   LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID)) 
        NOT IN (
        SELECT  ProjectID
        FROM    Projects
        )
-- You might need to double-check the best condition to not insert a "parent" for a root project
AND     LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID)) <> ''

您可以运行上述操作,直到不再有父母失踪。您还可以使用 WHILE @@ROWCOUNT > 0 循环。

插入所有父项目后,您可以同时更新所有项目的 ParentProjectID。

UPDATE  Projects
SET ParentProjectID = LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID))

编辑
阅读评论后,您似乎想针对特定项目执行此操作。
然后调整上述内容并使用局部变量应该可以解决问题:

DECLARE @CurrentProjectID varchar, 
        @ParentProjectID varchar
SET @CurrentProjectID = @InputProjectID
SET @ParentProjectID = LEFT(@CurrentProjectID, LEN(@CurrentProjectID) - CHARINDEX('-', REVERSE(@CurrentProjectID))

WHILE @ParentProjectID <> ''
BEGIN
  IF NOT EXISTS (
    SELECT  *
    FROM    Projects
    WHERE   ProjectID = @ParentProjectID
  )
  BEGIN
    INSERT INTO Projects (ProjectID)
    VALUES (@ParentProjectID)
  END
  UPDATE  Projects
  SET     ParentProjectID = @ParentProjectID
  WHERE   ProjectID = @CurrentProjectID
    SET @CurrentProjectID = @ParentProjectID
    SET @ParentProjectID = LEFT(@CurrentProjectID, LEN(@CurrentProjectID) - CHARINDEX('-', REVERSE(@CurrentProjectID))
END

您还可以选择确保允许在存储过程中进行递归调用,并让存储过程使用当前 ProjectID 的父级调用本身。

I'm not sure exactly what you want to achieve with ProjectIsPhase, but I suspect that's the trivial part of your problem.

Given a ProjectID, you can derive the ParentProjectID as follows: (might need a few +-1 tweaks to account for the separator.)

LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID))

So then insert parent projects that don't exist:

INSERT INTO Projects(ProjectID)
SELECT  LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID))
FROM    Projects p
WHERE   LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID)) 
        NOT IN (
        SELECT  ProjectID
        FROM    Projects
        )
-- You might need to double-check the best condition to not insert a "parent" for a root project
AND     LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID)) <> ''

You can run the above until no more parents are missing. You could also use a WHILE @@ROWCOUNT > 0 loop.

Once all parent projects have been inserted, you can update the ParentProjectID of all projects simultaneously.

UPDATE  Projects
SET ParentProjectID = LEFT(ProjectID, LEN(ProjectID) - CHARINDEX('-', REVERSE(ProjectID))

EDIT
After reading comments, it looks like you want to do this for a particular project.
Then tweaking the above and using local variables should do the trick:

DECLARE @CurrentProjectID varchar, 
        @ParentProjectID varchar
SET @CurrentProjectID = @InputProjectID
SET @ParentProjectID = LEFT(@CurrentProjectID, LEN(@CurrentProjectID) - CHARINDEX('-', REVERSE(@CurrentProjectID))

WHILE @ParentProjectID <> ''
BEGIN
  IF NOT EXISTS (
    SELECT  *
    FROM    Projects
    WHERE   ProjectID = @ParentProjectID
  )
  BEGIN
    INSERT INTO Projects (ProjectID)
    VALUES (@ParentProjectID)
  END
  UPDATE  Projects
  SET     ParentProjectID = @ParentProjectID
  WHERE   ProjectID = @CurrentProjectID
    SET @CurrentProjectID = @ParentProjectID
    SET @ParentProjectID = LEFT(@CurrentProjectID, LEN(@CurrentProjectID) - CHARINDEX('-', REVERSE(@CurrentProjectID))
END

You also have the option to ensure you're allowing recursive calls in your stored procs, and have the stored proc call itself with the current ProjectID's parent.

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