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 可以获 ...
随机推荐
- spring启动容器加载成功后执行调用方法
需求: 由于在微服务架构中各服务之间都是通过接口调用来进行交互的,像很多的基础服务,类似字典信息其实并不需每次需要的时候再去请求接口.所以我的想法是每次启动项目的时候,容器初始化完成,就去调用一下基础 ...
- python第十四天
今日内容 1. 带参装饰器 | wrapper 2. 迭代器 3. 可迭代对象 4.迭代器对象 5.for 迭代器 6.枚举对象 带参装饰器 是指装饰器为被装饰的函数添加新功能,需要外界的参数 - ...
- Druid.jar包
首先了解一下,什么是Druid. Druid是Java语言中最好的数据库连接池,它能够提供强大的监控和扩展功能. Druid是一个JDBC组件,它包括三部分: 1)DruidDriver 代理Driv ...
- head里两个重要标签base和meta
base标签 <base href="../"> 我们并不常用的一个标签,但是一旦用得不当会带来灾难性的影响. 它会影响到所有页面上的href和src属性相对路劲的定位 ...
- 2018-2019-2 20165232《网络对抗技术》Exp1 缓冲区溢出实验
2018-2019-2 20165232<网络对抗技术>Exp1 缓冲区溢出实验 实验点1:逆向及Bof基础实践 实践任务 用一个pwn1文件. 该程序正常执行流程是:main调用foo函 ...
- MD5算法 —— C语言实现(字符串的加密)
转 https://blog.csdn.net/weixin_42167759/article/details/81209320 网上找到的实现md5函数代码,包括一个头文件md5.h和一个源文件md ...
- Python 中使用 matplotlib 绘图中文字符显示异常的问题
最近在使用 Python matplotlib 绘制图表时发现中文字符不能正确显示:比如在绘制折线图时,中文全部显示成▢▢▢的格式,虽然将数据改成英文就没什么问题,但是所有数据都这么做时不可行的,于是 ...
- 试用VS2019正式版
1.下载地址https://visualstudio.microsoft.com/zh-hans/downloads/2.安装,安装过程比较简单,直接下一步,下一步即可,现在的网速一般下载+安装要2个 ...
- JS通用模块模式 UMD
历史 JS诞生之初面向简单页面开发, 没有模块的概念. 后来页面逐渐复杂, 人类构造到 IIFE 立即执行函数来模拟 模块: 之前也有雅虎的实践,使用命名空间 作为模块名. 最后衍生出 面向各种使用场 ...
- spring整合mybatis,批量扫描mapper接口出现异常
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component c ...