pgsql获取两个同结构表的不同数据条,结果以表格输出

使用pgsql通过游标和临时表对比两个同结构表格的数据,不同之处通过row_n标记,值为2表示数据不一致。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:声明两个游标遍历表格中的数据,然后进行交叉对比,为了定位不同的数据,在临时表中新增一列row_n作为标记(默认值为1),如果数据不同,则更新row_n值为2。

create or replace function get_two_tables_difference
(in schem_n_1 text,in schem_n_2 text,in table_n_1 text,in table_n_2 text)
returns void
language plpgsql
as $$
declare curs1 refcursor;--游标1用于遍历第一个表
		curs2 refcursor;--游标2用于遍历第二个表
		record1 record;
		record2 record;
		m int=1;--用于计数进行循环
		n int=1;--用于计数进行循环
		i int=1;
		j int=1;
		result_hh record;--用于记录结果
		flag boolean=false;
		temp_n int;---用于记录哪些行满足条件
		temp_n_1 int;
begin
	drop table if exists temp_table_1;
	drop table if exists temp_table_2;
	drop table if exists temp_table_3;
	execute format('create table temp_table_1 as select * from %I.%I',schem_n_1,table_n_1);
	execute format('create table temp_table_2 as select * from %I.%I',schem_n_2,table_n_2);
	select count(*)into m from temp_table_1;
	select count(*)into n from temp_table_2;
	alter table temp_table_1 add column if not exists row_n int d
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值