单表更新
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函数返回一个字符串,描述了导致异常的上下文(例如,哪个函数或过程抛出了异常,以及它在哪一行)。
3、rollback
如果发生异常,则回滚当前事务,以确保数据库的一致性。
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