sql父子表结构,常用脚本
在实际运用中经常会创建这样的结构表Category(Id, ParentId, Name),
特别是用于树形结构时(菜单树,权限树..),
这种表设计自然而然地会用到递归,
若是在程序中进行递归(虽然在程序中递归真的更方便一些),
无论是通过ADO.NET简单sql查找还是ORM属性关联都会执行多次sql语句,
难免会造成一些性能上的损耗,所以干脆使用sql的函数来解决这个问题,用函数返回我们最终需要的结果。
数据准备
CREATE TABLE Region
(
Id INT IDENTITY PRIMARY KEY,
Name NVARCHAR(20),
ParentId INT
);
GO insert into Region(Name,ParentId) values('广东',NULL)
insert into Region(Name,ParentId) values('深圳',1)
insert into Region(Name,ParentId) values('惠州',2)
insert into Region(Name,ParentId) values('罗湖区',2)
insert into Region(Name,ParentId) values('福田区',2)
insert into Region(Name,ParentId) values('龙岗区',2)
insert into Region(Name,ParentId) values('惠阳区',3)
insert into Region(Name,ParentId) values('龙门县',3)
insert into Region(Name,ParentId) values('华强北',5)
insert into Region(Name,ParentId) values('体育馆',5) SELECT * FROM dbo.Region AS R
1.查询父节点的所有子节点
/*
* summary:递归获取所有子节点
*/
CREATE function GetRecursiveChildren
(
@Id int
)
returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int)
begin
declare @i int
set @i = 1
--根节点,Level = 0
insert into @t select @Id,@id,(select Name from Region where Id = @id),0
--直属子节点,Level = 1
insert into @t select Id,ParentId,Name,@i from Region where ParentId = @Id --如果没有新的值插入,循环结束
while @@rowcount<>0
begin
set @i = @i + 1;
insert into @t
select
a.Id,a.ParentId,a.Name,@i
from
Region a, @t b
where
a.ParentId = b.Id and b.Level = @i - 1
end
return
end
go
--调用函数
select * from GetRecursiveChildren(3)

--CTE(公用表表达式)实现
declare @id int
set @id = 3
;with t as--如果CTE前面有语句,需要用分号隔断
(
select Id, ParentId, Name
from Region
where Id = @id
union all
select r1.Id,r1.ParentId,r1.Name
from Region r1 join t as r2 on r1.ParentId = r2.Id
)
select * from t order by Id

2.根据子节点追溯根节点
create function GetRecursiveParent
(
@Id int
)
returns @t table(Id int,ParentId int,Name nvarchar(20), [Level] int)
as
begin
declare @i int
set @i = 1
--插入末节点,Level = 0
insert into @t select @Id,@id,(select Name from Region where Id = @id),0
--插入末节点的父节点,Level = 1
insert into @t select Id,ParentId,Name,@i from Region
where Id = (select ParentId from Region where Id = @Id)
--如果没有新的值插入,循环结束
while @@rowcount<>0
begin
set @i = @i + 1;
insert into @t
select
a.Id,a.ParentId,a.Name,@i
from
Region a, @t b
where
a.Id = b.ParentId and b.Level = @i - 1
end
return
end
go
--调用函数
select * from GetRecursiveParent(8)
go

3.根据导航数据查询节点
create function GetLevel
(
@Id int
)
returns @level table(IdLevel varchar(100),NameLevel nvarchar(200))
as
begin
declare @IdLevel varchar(100),@NameLevel nvarchar(200),@Name nvarchar(50)
select @IdLevel = cast(@Id as varchar(10))
select @NameLevel = (select Name from Region where Id = @Id) while(exists(select Id,ParentId from Region where Id = (select ParentId from Region where Id = @Id)))
begin
select @Id = Id,@Name = Name from Region where Id = (select ParentId from Region where Id = @Id)
select @IdLevel = cast(@Id as varchar(10)) + '>' + @IdLevel
select @NameLevel = @Name + '>' + @NameLevel
end
insert into @level select @IdLevel,@NameLevel
return
end
go
--调用函数
select * from GetLevel(10)
go

sql父子表结构,常用脚本的更多相关文章
- SQL server 表结构转Oracle SQL脚本
SQL server 表结构转Oracle SQL脚本 /****** Object: StoredProcedure [dbo].[getOracle] Script Date: 2019/7/25 ...
- MS SQL 日常维护管理常用脚本(二)
监控数据库运行 下面是整理.收集监控数据库运行的一些常用脚本,也是MS SQL 日常维护管理常用脚本(一)的续集,欢迎大家补充.提意见. 查看数据库登录名信息 Code Snippet SELEC ...
- SQL Server 一句Sql把表结构全部查询出来
--一句Sql把表结构全部查询出来 SELECT 表名 = Case When A.colorder=1 Then D.name Else '' End, 表说明 = Case When A.colo ...
- sql复制表结构,复制表内容语句
sql复制表结构,复制表内容语句 select * into b from a where 1<>1 select top 0 * into b from a insert into a ...
- 用户中心mysql数据库表结构的脚本
/* Navicat MySQL Data Transfer Source Server : rm-m5e3xn7k26i026e75o.mysql.rds.aliyuncs.com Source S ...
- DB2表结构DDL脚本导出
db2look是导出DDL语句脚本的命令,以下是对db2look的一个简单介绍. 语法:db2look -d <数据库名> -e -t <表名> -o <文件名>. ...
- sql复制表结构及复制表数据
一.复制表结构 假设我们有一个数据表Person,有Id,FirstName,LastName,Weight,Height5个列,表结构可以参考这一篇.现在我们想创建一个新表叫People,表结构和P ...
- sql 查看表结构
sqlserver 查看表结构 exec sp_help @TableName --得到表信息.字段,索引.constraint. exec sp_pkeys @TableName --得到主键. e ...
- 7.使用EXPLAIN 来分析SQL和表结构_1
explain:查看执行计划 使用explain 关键字可以模拟优化器执行SQL查询语句,从而知道MySQL是如何处理你的SQL语句的 分析你的查询语句或是表结构的性能瓶颈 使用explain 可以获 ...
随机推荐
- git学习02 - log查看&版本回退
1.查看更新记录 git log / git log --pretty=oneline D:\learn\git_test>git log commit a915e7b12076673d778 ...
- JS学习笔记Day23
一.什么是Promise (一)Promise是ES6新增的解决异步(非阻塞)中存在的问题而产生的构造函数 二.Promise中的三种状态 pending(进行中) resoved(成功后) reje ...
- 八.django模型系统(二)之常用查询及表关系的实现
Ⅰ.常用查询 1.几个概念 每一个django模型类,都有一个默认的管理器,objects,查询就是依赖于objects管理器进行的(在创建时就被添加了). QuerySet表示数据库中对象的列表( ...
- Flash设置(各种版本浏览器包括低版本IE)
涉及到的各种版本flash百度下都能下到的,不再说明. Flash的安装比较麻烦,涉及多种浏览器.多种操作系统支持,安装.设置的地方比较多,以下说明基本涉及大部分安装过程中可能遇到的问题,如果安装或视 ...
- codeforces-1136 (div2)
A.读到第i章,就有N - i + 1章还没读. #include <map> #include <set> #include <ctime> #include & ...
- KFold,StratifiedKFold k折交叉切分
python风控评分卡建模和风控常识(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005214003&am ...
- Maven之阿里云镜像仓库配置
方式一:全局配置:修改maven的setting.xml配置 在mirrors节点下面添加子节点: <mirror> <id>nexus-aliyun</id> & ...
- spring-cloud-sleuth 和 分布式链路跟踪系统
==================spring-cloud-sleuth==================spring-cloud-sleuth 可以用来增强 log 的跟踪识别能力, 经常在微服 ...
- 五十九、linux 编程—— I/O 多路复用 fcntl
59.1 介绍 前面介绍的函数如,recv.send.read 和 write 等函数都是阻塞性函数,若资源没有准备好,则调用该函数的进程将进入阻塞状态.我们可以使用 I/O 多路复用来解决此问题(即 ...
- JSP/Serlet 使用fileupload上传文件
需要引用的jar commons-fileupload-1.3.1.jar commons-io-2.2.jar index.jsp <body> <center> <h ...