English 中文(简体)
服务器 • 让所有成长的儿童参与到许多与男性的关系中?
原标题:SQL Server - Get all children of a row in many-to-many relationship?

I m试图在服务器上写一个休养的问讯,基本上将父母与子女之间的等级从某个父母那里划分。 父母可以有多个子女,子女可以归属多个父母,因此,父母关系中有许多。

我从另一个有点关联的问题修改了以下询问,然而,这并没有完全走到树上,只是选择了第一层次的儿童。

DECLARE @ObjectId uniqueidentifier
SET @ObjectId =  1A213431-F83D-49E3-B5E2-42AA6EB419F1 ;

WITH Tree AS
(
   SELECT A.*
   FROM Objects_In_Objects A
   WHERE A.ParentObjectId = @ObjectId

   UNION ALL

   SELECT B.*
   FROM Tree A
   JOIN Objects_In_Objects B
   ON A.ParentObjectId = B.ObjectId
)
SELECT *
FROM Tree
INNER JOIN Objects ar on tree.ObjectId = ar.ObjectId

是否有任何人知道如何改变 que子,以走下树的一切道路? 还是无法使用上述建筑?

<>光>

栏目:<条码>

<>Objects_In_Objects

栏目:<条码> Id

www.un.org/Depts/DGACM/index_spanish.htm 抽样数据:

Objects

ObjectId                             | Name
1A213431-F83D-49E3-B5E2-42AA6EB419F1 | Main container  
63BD908B-54B7-4D62-BE13-B888277B7365 | Sub container  
71526E15-F713-4F03-B707-3F5529D6B25E | Sub container 2  
ADA9A487-7256-46AD-8574-0CE9475315E4 | Object in multiple containers

Objects In Object

ObjectId                             | ParentObjectId                    
ADA9A487-7256-46AD-8574-0CE9475315E4 | 71526E15-F713-4F03-B707-3F5529D6B25E
ADA9A487-7256-46AD-8574-0CE9475315E4 | 63BD908B-54B7-4D62-BE13-B888277B7365
63BD908B-54B7-4D62-BE13-B888277B7365 | 1A213431-F83D-49E3-B5E2-42AA6EB419F1
71526E15-F713-4F03-B707-3F5529D6B25E | 1A213431-F83D-49E3-B5E2-42AA6EB419F1
最佳回答

参看

为此:

;WITH Tree AS
(
   SELECT A.ObjectID, A.ObjectName, o.ParentObjectID, 1 AS  Level 
   FROM dbo.Objects A
   INNER JOIN dbo.Objects_In_Objects o ON A.ObjectID = o.ParentObjectID
   WHERE A.ObjectId = @ObjectId           -- use the A.ObjectId here

   UNION ALL

   SELECT A2.ObjectID, A2.ObjectName, B.ParentObjectID, t.Level + 1 AS  Level 
   FROM Tree t 
   INNER JOIN dbo.Objects_In_Objects B ON B.ParentObjectID = t.ObjectID
   INNER JOIN dbo.Objects A2 ON A2.ObjectId = B.ObjectId        
)
SELECT *
FROM Tree
INNER JOIN dbo.Objects ar on tree.ObjectId = ar.ObjectId

如果你改变这种状况,现在你的工作是不是? (我添加了一个<代码> 水平栏——通常有助于了解每一行的等级中的“深度”

我似乎在我的服务器上取得了适当的产出,至少......

问题回答
declare @Objects_In_Objects table
(
  ObjectID uniqueidentifier, 
  ParentObjectId uniqueidentifier
)

declare @Objects table
(
  ObjectId uniqueidentifier, 
  Name varchar(50)
)

insert into @Objects values
( 1A213431-F83D-49E3-B5E2-42AA6EB419F1 ,  Main container ),  
( 63BD908B-54B7-4D62-BE13-B888277B7365 ,  Sub container ),  
( 71526E15-F713-4F03-B707-3F5529D6B25E ,  Sub container 2 ),  
( ADA9A487-7256-46AD-8574-0CE9475315E4 ,  Object in multiple containers )

insert into @Objects_In_Objects values
( ADA9A487-7256-46AD-8574-0CE9475315E4 ,  71526E15-F713-4F03-B707-3F5529D6B25E ),
( ADA9A487-7256-46AD-8574-0CE9475315E4 ,  63BD908B-54B7-4D62-BE13-B888277B7365 ),
( 63BD908B-54B7-4D62-BE13-B888277B7365 ,  1A213431-F83D-49E3-B5E2-42AA6EB419F1 ),
( 71526E15-F713-4F03-B707-3F5529D6B25E ,  1A213431-F83D-49E3-B5E2-42AA6EB419F1 )


DECLARE @ObjectId uniqueidentifier
SET @ObjectId =  1A213431-F83D-49E3-B5E2-42AA6EB419F1 ;

WITH Tree AS
(
   SELECT A.ObjectID,
          A.ParentObjectId
   FROM @Objects_In_Objects A
   WHERE A.ParentObjectId = @ObjectId

   UNION ALL

   SELECT B.ObjectID,
          B.ParentObjectId
   FROM Tree A
   JOIN @Objects_In_Objects B
   ON B.ParentObjectId = A.ObjectId
)
SELECT *
FROM Tree
INNER JOIN @Objects ar on tree.ObjectId = ar.ObjectId;

https://data.stackchange.com/stackoverflow/q/111357/“rel=”nofollow noretinger”https://data.stackchange.com/stackoverflow/q/111357/





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签