达梦数据库比较两个表的数据结构是否相同,并找出差异
2021/7/19 19:07:56
本文主要是介绍达梦数据库比较两个表的数据结构是否相同,并找出差异,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
应用场景:数据库里有两个数据结构一样的表,在其中一个主表中设计了一个触发器,当删除该表中的一条数据时,同时把这条记录插入另一个表,有时候会突然报错,一直提示表结构不一样不能删除,这个时候就需要比较这两个表的结构。
1、创建测试表
create table TA(
id VARCHAR2(36) not null,
name VARCHAR2(100),
age NUMBER,
sex VARCHAR2(2)
);
insert into TA (id, name, age, sex)
values ('1', '1', 1, '1');
insert into TA (id, name, age, sex)
values ('2', '2', 2, '2');
insert into TA (id, name, age, sex)
values ('3', '3', 3, '3');
commit;
create table TB(
id VARCHAR2(36) not null,
name VARCHAR2(10), --字段类型和TA不同
age NUMBER,
clazz VARCHAR2(36) --字段名称和类型都和TA不同
);
insert into TB (id, name, age, clazz)
values ('1', '1', 1, '1');
insert into TB (id, name, age, clazz)
values ('2', '2', 1, '3'); --第四个值和TA不同
insert into TB (id, name, age, clazz)
values ('3', '3', 3, '3');
commit;
2、sql方式比较
select 'TA' table_name,* from (
select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = 'TA'
minus select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = 'TB'
)
union
select 'TB' table_name,* from (
select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = 'TB'
minus
select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = 'TA'
)
3、存储过程方式比较
CREATE or replace PROCEDURE proc_check_table(table1 varchar(100) , table2 varchar(100)) AS
TYPE Rd IS RECORD(column_name VARCHAR(128), data_type VARCHAR(128));
v_row1 Rd;
v_row2 Rd;
cur cursor;
BEGIN
open cur for
select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = table1 minus
select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = table2;
LOOP
FETCH cur INTO v_row1;
EXIT WHEN cur%NOTFOUND;
print table1||'--'||v_row1.column_name ||'--'||v_row1.data_type;
END LOOP;
CLOSE cur;
open cur for
select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = table2 minus
select column_name,data_type||'('||data_length||')' data_type from user_tab_columns where table_name = table1;
LOOP
FETCH cur INTO v_row2;
EXIT WHEN cur%NOTFOUND;
print table2||'--'||v_row2.column_name ||'--'||v_row2.data_type;
END LOOP;
CLOSE cur;
END;
/
call proc_check_table('TA','TB');
1、 比较两个表中的数据是否相同,并找出差异数据。
(
select * from TA
minus
select * from TB
)
union
(
select * from TB
minus
select * from TA
)
这篇关于达梦数据库比较两个表的数据结构是否相同,并找出差异的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?
- 2024-05-09企业src漏洞挖掘-有意思的命令执行
- 2024-05-08阿里云域名注册流程,分享给第一次购买域名的新手站长!