SQL 无限极递归查询

--一、示例1
CREATE TABLE [dbo].[tb_Area](
	[ID] [int] IDENTITY(1,1) NOT NULL,
	[Code] [nvarchar](50) NOT NULL,
	[Name] [nvarchar](50) NOT NULL,
	[ParentID] [int] NOT NULL,
	[ParentPath] [nvarchar](200) NOT NULL,
	[AddDate] [datetime] NOT NULL,
	[Level] [int] NOT NULL,
 CONSTRAINT [PK_tb_Area] PRIMARY KEY NONCLUSTERED 
(
	[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

ALTER TABLE [dbo].[tb_Area] ADD  DEFAULT ('') FOR [Code]
GO

ALTER TABLE [dbo].[tb_Area] ADD  DEFAULT ('') FOR [Name]
GO

ALTER TABLE [dbo].[tb_Area] ADD  DEFAULT ((0)) FOR [ParentID]
GO

ALTER TABLE [dbo].[tb_Area] ADD  DEFAULT ('') FOR [ParentPath]
GO

ALTER TABLE [dbo].[tb_Area] ADD  DEFAULT (getdate()) FOR [AddDate]
GO

ALTER TABLE [dbo].[tb_Area] ADD  DEFAULT ((0)) FOR [Level]

GO

insert [dbo].[tb_Area] (Code,Name,ParentID,ParentPath,AddDate,Level) values('河北省','河北省',0,'',GETDATE(),0);
insert [dbo].[tb_Area] (Code,Name,ParentID,ParentPath,AddDate,Level) values('石家庄','石家庄',1,'1',GETDATE(),1);
insert [dbo].[tb_Area] (Code,Name,ParentID,ParentPath,AddDate,Level) values('邯郸','邯郸',1,'1',GETDATE(),1);
insert [dbo].[tb_Area] (Code,Name,ParentID,ParentPath,AddDate,Level) values('武安','武安',3,'1,3',GETDATE(),2);
insert [dbo].[tb_Area] (Code,Name,ParentID,ParentPath,AddDate,Level) values('新华区','新华区',2,'1,2',GETDATE(),2);
insert [dbo].[tb_Area] (Code,Name,ParentID,ParentPath,AddDate,Level) values('东郊街道办','东郊街道办',5,'1,2,5',GETDATE(),3);

GO

select * from tb_Area
GO


--向上递归
WITH temp AS
(
    SELECT * FROM vDepartment WHERE Id = 44456
    UNION ALL
    SELECT d.* FROM vDepartment d
	INNER JOIN temp ON d.Id=temp.Pid
)
SELECT * FROM temp ORDER BY Id ASC
GO

--不包含传递的id值
with temp as
(
   select * from tb_Area where ParentID =1
   union all
   select m.* from tb_Area m
   inner join temp as child on m.ParentID = child.ID
 )
 select * from temp

 GO

 --包含传递的id值(方法1)
 with temp as
(
   select * from tb_Area where ParentID =(select ParentID from tb_Area where ID =1) and tb_Area.ID = 1
   union all
   select m.* from tb_Area m
   inner join temp as child on m.ParentID = child.ID
 )
 select * from temp
 
 GO

 --包含传递的id值(方法2)
 with temp as
(
   select * from tb_Area where ParentID=(select ParentID from tb_Area where ID =1) and tb_Area.ID = 1
   union all
   select m.* from tb_Area m,temp tm where m.ParentID=tm.ID
 )
  select * from temp

--================================================================
--================================================================

--二、示例2
--构造测试数据: 只作演示用  
CREATE TABLE [dbo].[Tim_LinqTable](  
[Id] int PRIMARY KEY IDENTITY(1,1) NOT NULL,  
[Name] [varchar](50) NOT NULL,  
[Parent] int NOT NULL,  
)  
GO  
  
INSERT INTO [Tim_LinqTable]    
SELECT 'A',0 UNION ALL  
SELECT 'A1',1 UNION ALL  
SELECT 'A2',1 UNION ALL  
SELECT 'B1',2 UNION ALL  
SELECT 'B2',3 UNION ALL  
SELECT 'C1',4 UNION ALL  
SELECT 'C2',4 UNION ALL  
SELECT 'D1',5 UNION ALL  
SELECT 'D2',5 UNION ALL  
SELECT 'D3',5   
GO  

select * from [dbo].[Tim_LinqTable]

Go

WITH temp  
AS  
(  
SELECT * FROM [Tim_LinqTable]  WHERE Parent = 1
UNION ALL  
SELECT m.* FROM [Tim_LinqTable]  AS m  
INNER JOIN temp AS child ON m.Parent = child.Id  
)  
SELECT * FROM temp  
GO

你可能感兴趣的:(SQL,Server)