T-SQL 查询问题
我有一个名为 Summary 的表,表中的数据如下所示:
ID Type Name Parent
1 Act Rent Null
2 Eng E21-01-Rent Rent
3 Prj P01-12-Rent E21-01-Rent
1 Act Fin Null
2 Eng E13-27-Fin Fin
3 Prj P56-35-Fin E13-27-Fin
我正在编写一个 SP,它必须根据类型拉取父级。这里 Act 类型的 ID 始终为 1,Eng 类型的 ID 为 2,Prj 类型的 ID 为 3。ACT
父类型始终为 NULL, 类型 Eng 父级是 Act 并且 type Prj Parent is Eng
现在我有一个名为 Detail 的表。我正在编写一个 SP 将详细表数据插入到汇总表中。
我将 id 作为参数传递:
我与父级有问题。我怎样才能得到它?
我总是可以说,当 ID 为 1 时,父级为 Null,但当 ID 为 2 时,父级是 ID 1 的名称,类似地,当 ID 为 3 时,父级是 ID2 的名称。
我怎样才能得到它?
谁能帮我解决这个问题:
I have table called Summary and the data in the table looks like this:
ID Type Name Parent
1 Act Rent Null
2 Eng E21-01-Rent Rent
3 Prj P01-12-Rent E21-01-Rent
1 Act Fin Null
2 Eng E13-27-Fin Fin
3 Prj P56-35-Fin E13-27-Fin
I am writing a SP which has to pull the parent based on type. Here always the type Act has ID 1, Eng has ID 2 and Prj has ID 3.
The type ACT parent is always NUll,
type Eng parent is Act and
type Prj parent is Eng
Now I have table called Detail.I am writing a SP to insert Detail Table data to the Summary table.
I am passing the id as parameter:
I am having problem with the parent. How do I get that?
I can always say when ID is 1 then parent is Null but when ID is 2 then parent is name of ID 1 similarly when ID is 3 then parent is name of ID2.
How do I get that?
Can anyone help me with this:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议重新访问您的数据模型,但这也许会有所帮助...
只需将 @Parent 插入 Parent 列即可。
I advise re-visiting your data model, but maybe this will help...
Just insert @Parent into the Parent column.
我认为您必须有一个主键(另一个唯一的 ID),因为使用此模型,您将始终拥有该类型的第一行,例如 prj 将始终具有父级“E21-01-Rent”,并且无法到达“ E13-27-鳍”。
最后尝试这个查询
I think you must have a primary key (another ID which is unique) because with this model you will always have the first row of the type for example prj will always have the parent "E21-01-Rent" and no way to reach "E13-27-Fin" .
At last try this query