假设我有两个表,payment_received和payments_processed。我想在PL / SQL中声明一个变量,将数据从一个表复制到另一个表。两个表都有一个保持相同值的字段,比如payor_name。我用哪个表来定义%TYPE,“from”或“to”?
PROCEDURE some_proc AS
-- value coming FROM payments_received
-- value going TO payments_processed
v_payor_name payments_received.payor_name%TYPE;
-- OR
v_payor_name payments_processed.payor_name%TYPE;
BEGIN
SELECT payor_name INTO v_payor_name
FROM payments_received
WHERE payment_id = some_payment_id;
UPDATE payments_processed
SET payor_name = v_payor_name
WHERE processed_id = some_processed_id;
END some_proc;
我四处寻找最佳实践指导,但没有找到任何真正涵盖%TYPE /%ROWTYPE使用方面的内容。
答案 0 :(得分:3)
一般无所谓。据推测,payor_name
列在两个表中声明相同。
就个人而言,我可能会使用源表,因为如果您现在或将来需要选择多个列(或整个行),这将是一致的。但是如果有人想使用目标表作为锚类型,我不会反对。
如果有一些原因,如果声明中存在某些差异,最好在SELECT
声明或UPDATE
声明中输出错误,这可能会导致您更喜欢在另一个。但这种情况非常罕见,因此通常是偏好和一致性问题。
答案 1 :(得分:1)
如果有人想对此非常严格,他们可以同时拥有:
PROCEDURE some_proc AS
-- value coming FROM payments_received
-- value going TO payments_processed
v_payor_name_source payments_received.payor_name%TYPE;
v_payor_name_target payments_processed.payor_name%TYPE;
BEGIN
SELECT payor_name INTO v_payor_name_source
FROM payments_received
WHERE payment_id = some_payment_id;
v_payor_name_target := v_payor_name_source;
UPDATE payments_processed
SET payor_name = v_payor_name_target
WHERE processed_id = some_processed_id;
END some_proc;
但我认为这有点矫枉过正。