【高海東】SQL語句對錶中父子節點正向和反向取所有節點
CREATE TABLE [dbo].[temptb](
[id] [int] IDENTITY(1,1) NOT NULL,
[pid] [int] NULL,
[name1] [varchar](20) ,
[name] [nvarchar](50) ,
[parentid] [int] NULL,
CONSTRAINT [PK_temptb] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/**//* 建立函式 根據節點id找出其所有父節點*/
create function f_pid(@id int)
returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.pid,@l
from temptb a,@re b
where a.id=b.id
and b.level=@l-1
and a.pid<>0
end
update @re set level=@l-level
return
end
go
/**//* */
select a.*,b.level
from temptb a,f_pid(7) b
where a.id=b.id
order by b.level
go
/**//* 建立函式 根據節點id 找出所有子節點*/
create function c_tree(@initid int)/**//*定義函式c_tree,輸入引數為初始節點id*/
returns @t table(id int,name varchar(100),parentid int,lev INT,byid int)/**//*定義表t用來存放取出的資料*/
begin
declare @i int/**//*標誌遞迴級別*/
set @i=1
insert @t select id,name,parentid,@i ,byid=@initid from temptb where id=@initid
while @@rowcount<>0
begin
set @i=@i+1
insert @t select a.id,a.name,a.parentid,@i,@initid from temptb as a,@t as b
where b.id=a.parentid and b.lev=@i-1
end
return
END
/**//*在上面的函式中由於表變數使用了兩次,效能很差 ,下面的效能要高些*/
create function [dbo].[UF_GetOwnerSKUNumber]()
RETURNS @b table(id int,byid int)
BEGIN
DECLARE @t table(id int,lev INT,byid int)
declare @i int/**//*標誌遞迴級別*/
set @i=1
insert @t select c.id,@i ,c.byid
from [temptb] c WITH (NOLOCK)
WHERE [pid]=0 OR [parentid] IS NULL
OR parentid NOT IN (SELECT id FROM [temptb]WHERE id=c.id)
while @@rowcount<>0
begin
set @i=@i+1
insert @b SELECT a.id,b.byid from
[temptb] as a WITH (NOLOCK) ,@t as b
where b.id=a.parentid and b.lev=@i-1
end
RETURN
END
select * from c_tree( )
/**//* 把所有行轉換為一個字串 */
DECLARE @FileClassName nvarchar(max)
SET @FileClassName=''
SELECT @FileClassName =+ @FileClassName+CONVERT(varchar(20),id)+',' FROM [temptb] a WHERE pid=0
SELECT @FileClassName AS a
[id] [int] IDENTITY(1,1) NOT NULL,
[pid] [int] NULL,
[name1] [varchar](20) ,
[name] [nvarchar](50) ,
[parentid] [int] NULL,
CONSTRAINT [PK_temptb] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/**//* 建立函式 根據節點id找出其所有父節點*/
create function f_pid(@id int)
returns @re table(id int,level int)
as
begin
declare @l int
set @l=0
insert @re select @id,@l
while @@rowcount>0
begin
set @l=@l+1
insert @re select a.pid,@l
from temptb a,@re b
where a.id=b.id
and b.level=@l-1
and a.pid<>0
end
update @re set level=@l-level
return
end
go
/**//* */
select a.*,b.level
from temptb a,f_pid(7) b
where a.id=b.id
order by b.level
go
/**//* 建立函式 根據節點id 找出所有子節點*/
create function c_tree(@initid int)/**//*定義函式c_tree,輸入引數為初始節點id*/
returns @t table(id int,name varchar(100),parentid int,lev INT,byid int)/**//*定義表t用來存放取出的資料*/
begin
declare @i int/**//*標誌遞迴級別*/
set @i=1
insert @t select id,name,parentid,@i ,byid=@initid from temptb where id=@initid
while @@rowcount<>0
begin
set @i=@i+1
insert @t select a.id,a.name,a.parentid,@i,@initid from temptb as a,@t as b
where b.id=a.parentid and b.lev=@i-1
end
return
END
/**//*在上面的函式中由於表變數使用了兩次,效能很差 ,下面的效能要高些*/
create function [dbo].[UF_GetOwnerSKUNumber]()
RETURNS @b table(id int,byid int)
BEGIN
DECLARE @t table(id int,lev INT,byid int)
declare @i int/**//*標誌遞迴級別*/
set @i=1
insert @t select c.id,@i ,c.byid
from [temptb] c WITH (NOLOCK)
WHERE [pid]=0 OR [parentid] IS NULL
OR parentid NOT IN (SELECT id FROM [temptb]WHERE id=c.id)
while @@rowcount<>0
begin
set @i=@i+1
insert @b SELECT a.id,b.byid from
[temptb] as a WITH (NOLOCK) ,@t as b
where b.id=a.parentid and b.lev=@i-1
end
RETURN
END
select * from c_tree( )
/**//* 把所有行轉換為一個字串 */
DECLARE @FileClassName nvarchar(max)
SET @FileClassName=''
SELECT @FileClassName =+ @FileClassName+CONVERT(varchar(20),id)+',' FROM [temptb] a WHERE pid=0
SELECT @FileClassName AS a
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-281057/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql server 樹狀結構表中,獲取指定節點的所有父節點路徑SQLServer
- Ext實現點選節點,父子節點反選
- 父子節點資料統計
- jQuery如何獲取元素父節點和子節點jQuery
- JavaScript 獲取所有後代元素節點JavaScript
- mysql根據節點查詢所有葉節點MySql
- 資料庫——查詢樹形結構某節點的所有子節點、所有父節點資料庫
- oracle rac中讓sql語句在指定的節點執行的方法OracleSQL
- 計算節點會對收到的SQL 語句做解析、優化、執行並SQL優化
- json 陣列已知父節點,求所有子節點JSON陣列
- Java中的條件判斷語句-動力節點Java
- JQuery2:節點選取與節點插入jQuery
- 替歸演算法獲取Treeview所有節點演算法View
- Neo4j 查詢某標籤節點個數語句 刪除某標籤全部節點語句
- Postgrsql 從節點當機,主節點執行DML語句出現等待情況SQL
- 獲取頁面中所有元素的節點型別型別
- 節點快取的優缺點快取
- 1.19 JQuery2:節點插入與節點選取jQuery
- jquery獲取指定節點的第一級文字節點jQuery
- dom4j 根據xml節點路徑查詢節點,找到對應的目標節點下的子節點,對節點Text值進行修改XML
- jquery獲取元素節點jQuery
- JavaScript學習之DOM(節點、節點層級、節點操作)JavaScript
- DDL語句在儲存過程中使用的細節和要點儲存過程
- 社群 - 所有節點,進入節點頁面返回中間頁顯示蒙層
- 女神節熱點海報設計指南!一分鐘製作女神節精美海報
- C#不使用DataSet操作XML,XmlDocument讀寫xml所有節點及讀取xml節點的資料總結C#XML
- dom節點和vue中template淺談Vue
- RAC 雙節點 轉單節點流程
- Linq to xml 查詢所有的子節點XML
- Java中在二叉搜尋樹中查詢節點的父節點Java
- 正向代理和反向代理
- consul 多節點/單節點叢集搭建
- RAC中跨節點並行並行
- 二叉樹父子節點下標位置關係證明二叉樹
- java處理流 和節點流(在位元組流和字元流中,又分為處理流和節點流)Java字元
- 獲取BT節點資訊bittorrent-discovery
- JavaScript獲取父元素下子元素節點JavaScript
- k8s系列--node(k8s節點介紹,新增節點,移除節點)K8S