SQL更新目标表中具有相同ID但源表中值不同的多行

时间:2018-12-04 07:44:11

标签: sql-update plsqldeveloper

贷方通知单表

Report1

paymentModeAmount表

id      invoice_id     generated_at(timestamp)            status
10101      111          2018-02-28 14:42:39.247          generated
23982      111          2018-03-30 11:11:11.247          generated 

执行以下查询后,输出是

paymentModeAmount表

id      invoice_id     paid_on(timestamp)     payment_mode
98236      111                                 creditNote
63725      111                                 creditNote

PaidOn列的数据相同,但是我们需要获取两个不同的时间戳

预期

id        invoice_id     paid_on(timestamp)       payment_mode
98236        111         2018-02-28 14:42:39.247   creditNote 
63725        111         2018-02-28 14:42:39.247   creditNote 

paidOn时间戳记应该与预期的不同

PL / SQL函数

id      invoice_id     paid_on(timestamp)       payment_mode
98236      111         2018-02-28 14:42:39.247   creditNote 
63725      111         2018-03-30 11:11:11.247   creditNote 

执行以下功能

CREATE OR REPLACE FUNCTION updatePaidOn()
RETURNS TABLE(
invoiceid BIGINT,
generatedat TIMESTAMP)
AS $$
BEGIN
RETURN QUERY SELECT
generated_invoice_id as invoiceid, generated_at as generatedat
FROM
credit_note
WHERE
status='generated';
END; $$

LANGUAGE 'plpgsql';

0 个答案:

没有答案