Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
btree
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
IF OBJECT_ID('[dbo].[btree]', 'U') IS NOT NULL DROP TABLE [dbo].[btree]; GO CREATE TABLE [dbo].[btree] ( id INT PRIMARY KEY , parent_id INT REFERENCES [dbo].[btree] ([id]) , name NVARCHAR(20) ); GO INSERT INTO [btree] VALUES (1, null, '1 Root'); INSERT INTO [btree] VALUES (2, 1, '1.1 Group'); INSERT INTO [btree] VALUES (3, 1, '1.2 Group'); INSERT INTO [btree] VALUES (4, 2, '1.1.1 Group'); INSERT INTO [btree] VALUES (5, 2, '1.1.2 Group'); INSERT INTO [btree] VALUES (6, 3, '1.2.1 Group'); INSERT INTO [btree] VALUES (7, 3, '1.2.2 Group'); INSERT INTO [btree] VALUES (8, 4, '1.1.1.1 Items'); INSERT INTO [btree] VALUES (9, 4, '1.1.1.2 Items'); INSERT INTO [btree] VALUES (10, 5, '1.1.2.1 Items'); INSERT INTO [btree] VALUES (11, 5, '1.1.2.2 Items'); INSERT INTO [btree] VALUES (12, 6, '1.2.1.1 Items'); INSERT INTO [btree] VALUES (13, 6, '1.2.1.2 Items'); INSERT INTO [btree] VALUES (14, 7, '1.2.2.1 Items'); ;WITH tree AS ( SELECT c1.id, c1.parent_id, c1.name, [level] = 1 FROM dbo.[btree] c1 WHERE c1.parent_id IS NULL UNION ALL SELECT c2.id, c2.parent_id, c2.name, [level] = tree.[level] + 1 FROM dbo.[btree] c2 INNER JOIN tree ON tree.id = c2.parent_id ) SELECT tree.level, tree.id, parent_id, REPLICATE(' ', tree.level - 1) + tree.name AS description FROM tree OPTION (MAXRECURSION 0) ;
View schema
Execution time: 0,02 sec, rows selected: 14, rows affected: 14, absolute service time: 0,17 sec
edit mode
|
history
level
id
parent_id
description
1
1
1
NULL
1 Root
2
2
2
1
1.1 Group
3
2
3
1
1.2 Group
4
3
6
3
1.2.1 Group
5
3
7
3
1.2.2 Group
6
4
14
7
1.2.2.1 Items
7
4
12
6
1.2.1.1 Items
8
4
13
6
1.2.1.2 Items
9
3
4
2
1.1.1 Group
10
3
5
2
1.1.2 Group
11
4
10
5
1.1.2.1 Items
12
4
11
5
1.1.2.2 Items
13
4
8
4
1.1.1.1 Items
14
4
9
4
1.1.1.2 Items