存储过程、函数 单表更新、连表更新、获取六个月的最后一天。获取当月的最后一天

单表更新
update plant_psi_info
	set production = sales,
		production_amount = sales_amount,
		modify_by = p_user_id,
		modify_dt = hand_sys_now()	 	 	
	where
		org_id = p_org_id
		and psi_period = p_psi_period
		and del_flag = '0';			



 连另一张表更新
 update plant_psi_info
	set production = b.sales,
		production_amount = b.sales_amount,
		modify_by = p_user_id,
		modify_dt = hand_sys_now()
	from
		plant_psi_info b
	where
		b.org_id = p_org_id
		and b.psi_period = p_psi_period
		and b.del_flag = '0';			
		
		and plant_psi_info.org_id=b.org_id
		and plant_psi_info.uid=b.uid
		and plant_psi_info.user_id=b.user_id
		and plant_psi_info.row_num=b.row_num
		and plant_psi_info.inf_type=b.inf_type
		and plant_psi_info.m_code=b.m_code
		and plant_psi_info.psi_period =b.psi_period;

获取日期

select to_char(date_trunc('month',to_date((
select psi_period_st from plant_m_psi_period_info where org_id = 1 and psi_period = '2024年1月No.1'
),'yyyy-MM'::text) + ((seq_no-1)::varchar || ' month') ::interval) + interval'1 month - 1 day',
'yyyy-MM-dd') as shipping_dt from m_seq t2 where seq_no <= 6
		

执行结果
在这里插入图片描述
蓝色部分执行结果
在这里插入图片描述

获取当月的最后一天

select to_char((date_trunc('month',to_date('2024-05','yyyy-mm-dd')) + interval'1 month - 1 day'),'yyyy-mm-dd')

在这里插入图片描述
存储过程含义讲解

    exception when others then
    get stacked diagnostics p_err_detail = pg_exception_context;
	rollback;
	p_err_flag := '-2' || ':' || p_err_detail || '->' || sqlerrm;
	update t_procedure_result set proce_result = substr(p_err_flag,0,200), 
	last_update_time = hand_sys_now() where proce_uid = p_uid and 
	proce_user_id = p_user_id and proce_type = 'pkg_apollo_plant_psi_modify_m';
	return;

1、当在PL/pgSQL块中发生任何未被特定捕获的异常时,when others then部分将被执行。
2、这行代码使用GET STACKED DIAGNOSTICS来获取异常的上下文信息,并将其存储在变量p_err_detail中。pg_exception_context函数返回一个字符串,描述了导致异常的上下文(例如,哪个函数或过程抛出了异常,以及它在哪一行)。
3rollback 如果发生异常,则回滚当前事务,以确保数据库的一致性。
4、这里,它创建了一个错误标志p_err_flag。这个标志由几个部分组成:
-2:可能是一个自定义的错误代码。
p_err_detail:从pg_exception_context获取的异常上下文。
sqlerrm:SQL错误消息,即描述发生了什么的实际消息。
5、return 退出PL/pgSQL过程或函数。
总的来说,这段代码在PL/pgSQL过程或函数中捕获任何异常,记录有关该异常的详细信息,并将该信息存储在t_procedure_result表中,然后退出过程或函数。
6、如果 p_err_detail 包含 “function foo() at line 10” 并且 sqlerrm 包含 “division by zero”,那么 p_err_flag 将会是:

-2:function foo() at line 10->division by zero
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值