SQL Server、达梦数据库 存储过程:两个表(一对多)数据合并到一个表

使用存储过程和游标处理两个表(一对多)的数据到一个表中,涉及到存储过程调用另一个存储过程技术点,记录SQL Server、达梦两种数据库的实现。

测试表

复制代码
--创建测试表 person
create table person
(
	id int not null,
	name varchar(20),
	primary key(id)
);

--创建测试表 telephone
create table telephone
(
	id int not null,
	toolname varchar(20),
	number varchar(20)
);

--创建测试表 person_telephone
create table person_telephone
(
	id int not null,
	name varchar(20),
	tel varchar(200),
	primary key(id)
);

--插入测试数据
insert into person(id,name) values(1,'李小龙');
insert into person(id,name) values(2,'张大富');
insert into telephone(id,toolname,number) values(1,'电话','010-66666666');
insert into telephone(id,toolname,number) values(1,'手机','13866666666');
insert into telephone(id,toolname,number) values(1,'小灵通','5879325');
insert into telephone(id,toolname,number) values(2,'电话','010-55555555');

go

存储过程脚本

创建两个存储过程:

  • proc1:提取 telephone 的联系数据
  • proc2:调用 proc1 并向person_telephone插入数据

完整阅读:
https://www.laobingbiji.com/page/202404030842010000000010691999.html

相关推荐
布局呆星20 分钟前
SQLite数据库的介绍与使用
数据库·python
2401_8384725121 分钟前
用Python和Twilio构建短信通知系统
jvm·数据库·python
石头wang27 分钟前
oracle jdbc 依赖以及对dbeaver的性能影响,如何选择oracle驱动, oracle jdbc 驱动
数据库·oracle
weixin_4521595528 分钟前
如何从Python初学者进阶为专家?
jvm·数据库·python
2301_790300961 小时前
用Python读取和处理NASA公开API数据
jvm·数据库·python
万象.1 小时前
redis持久化:AOF和RDB
数据库·redis·缓存
tod1132 小时前
力扣高频 SQL 50 题阶段总结(四)
开发语言·数据库·sql·算法·leetcode
!chen2 小时前
Redis快速实现布隆过滤器
数据库·redis·缓存
2301_790300962 小时前
数据分析与科学计算
jvm·数据库·python
-XWB-2 小时前
【Oracle】Oracle诊断系列(2/6):锁问题与阻塞分析——解决“卡住”的会话
数据库·oracle