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

相关推荐
掘根3 分钟前
【MySQL】Ubuntu环境下MySQL的安装与卸载
数据库·mysql·centos
知识分享小能手1 小时前
mysql学习教程,从入门到精通,SQL 修改表(ALTER TABLE 语句)(29)
大数据·开发语言·数据库·sql·学习·mysql·数据分析
暮毅1 小时前
Django对接支付宝沙箱环境(2024年9月新测有效)
数据库·django
fat house cat_1 小时前
mysql-索引笔记
数据库·mysql
He guolin1 小时前
【MySQL】数据库基础知识
数据库·mysql
AitTech2 小时前
深入理解C#中的TimeSpan结构体:创建、访问、计算与格式化
开发语言·数据库·c#
我焦虑的编程日记2 小时前
【RabbitMQ】RabbitMQ学习
java·数据库·java-ee
晨曦启明6342 小时前
MySQL表操作(进阶)
数据库
攻城狮numberOne2 小时前
MongoDB集群模式详解及应用实战
数据库·mongodb