2

假设我有两个表,payments_received 和 payment_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 使用方面的内容。

4

2 回答 2

3

一般没关系。据推测,该payor_name列在两个表中的声明相同。

就个人而言,我可能会使用源表,因为如果您现在或将来需要选择多个列(或整行),那将是一致的。但如果有人想将目标表用作锚类型,我不会反对。

如果出于某种原因,最好在SELECT声明中出错,或者UPDATE如果声明中有一些差异,则可能会导致您更喜欢其中一个。不过,这种情况很少见,因此通常是偏好和一致性问题。

于 2013-04-04T01:48:22.750 回答
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;

但我认为这是矫枉过正。

于 2013-04-04T02:58:27.940 回答