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

相关推荐
听闻风很好吃7 分钟前
DAY9:Oracle数据库安全管理深度解析
oracle
LucianaiB42 分钟前
【金仓数据库征文】_AI 赋能数据库运维:金仓KES的智能化未来
运维·数据库·人工智能·金仓数据库 2025 征文·数据库平替用金仓
时序数据说1 小时前
时序数据库IoTDB在航空航天领域的解决方案
大数据·数据库·时序数据库·iotdb
.生产的驴1 小时前
SpringBoot 封装统一API返回格式对象 标准化开发 请求封装 统一格式处理
java·数据库·spring boot·后端·spring·eclipse·maven
AnsenZhu1 小时前
2025年Redis分片存储性能优化指南
数据库·redis·性能优化·分片
Johny_Zhao2 小时前
MySQL 高可用集群搭建部署
linux·人工智能·mysql·信息安全·云计算·shell·yum源·系统运维·itsm
oydcm2 小时前
MySQL数据库概述
数据库·mysql
oioihoii2 小时前
C++23中if consteval / if not consteval (P1938R3) 详解
java·数据库·c++23
带娃的IT创业者2 小时前
《AI大模型趣味实战》基于RAG向量数据库的知识库AI问答助手设计与实现
数据库·人工智能
husterlichf3 小时前
MYSQL 常用数值函数 和 条件函数 详解
数据库·sql·mysql