SQL循环更新
所以这就是交易。我得到了我们所说的“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-SEG
:0174- 的记录将更新 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不确定您到底想通过
ProjectIsPhase
实现什么目标,但我怀疑这是您问题的微不足道的部分。给定
ProjectID
,您可以按如下方式派生ParentProjectID
:(可能需要一些 +-1 调整来说明分隔符。)然后插入不支持的父项目不存在:
您可以运行上述操作,直到不再有父母失踪。您还可以使用
WHILE @@ROWCOUNT > 0 循环。
插入所有父项目后,您可以同时更新所有项目的 ParentProjectID。
编辑
阅读评论后,您似乎想针对特定项目执行此操作。
然后调整上述内容并使用局部变量应该可以解决问题:
您还可以选择确保允许在存储过程中进行递归调用,并让存储过程使用当前 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 theParentProjectID
as follows: (might need a few +-1 tweaks to account for the separator.)So then insert parent projects that don't exist:
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.
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:
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.