1

我有以下代码:

CREATE OR REPLACE FUNCTION repeatable_rand_text(ftype    IN VARCHAR2
                                              , in_val   IN VARCHAR2)
    RETURN VARCHAR2 IS
    workval         VARCHAR2(64);
    insert_needed   BOOLEAN := FALSE;
BEGIN
    BEGIN
        SELECT new_name
        INTO   workval
        FROM   ps_dt_mixnames_preserve
        WHERE  name_type = ftype AND old_name = in_val;

    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            workval := rand_text(ftype);
            insert_needed := TRUE;
    END;

    IF insert_needed THEN
        INSERT INTO ps_dt_mixnames_preserve(name_type, old_name, new_name)
        VALUES      (ftype, in_val, workval);
    END IF;

    RETURN workval;
END repeatable_rand_text;

此例程的目的是在数据库中屏蔽名称,作为从生产环境创建开发数据库的准备工作的一部分。

我们想要屏蔽名称,但我们希望它们是可重复的,以便我们的结果类似于:(左侧输入;右侧输出)

JOHN JONES  -> STEEL POTATO
SAM JONES   -> LARGE POTATO
MARY JONES  -> WHITE POTATO
SUE SMITH   -> LARGE CARROT
FRED JONES  -> RED POTATO
JOHN SMITH  -> GREEN CARROT

您可能明白了:姓氏更改为随机值,但再次遇到时会重复。给定的名称只是随机的。这里关心的是姓氏。

数组和其他非永久性解决方案对我们来说效果不佳,因为这实际上将作为一大系列 UPDATE 语句在同一个会话中执行。GTT 似乎非常适合这种类型的东西。

最终,我们将要执行类似于以下内容的更新:

UPDATE MY_TABLE
SET ORIG_NAME = repeatable_rand_text('last', ORIG_NAME)

但为了“证明这个例程的结果,我们执行以下 SQL:

SELECT ORIG_NAME, repeatable_rand_text('last',ORIG_NAME)
FROM MY_TABLE

现在,显然该函数中的 INSERT 将作为 SELECT 的结果执行,这是一个禁忌。(如果无法做到这一点,我们不愿意尝试自然跟随的更新。据我们所知,它可能会失败并显示类似的消息。)问题是,有没有合理的方法来解决这种情况?

4

3 回答 3

1

我终于使用了一个自治块。这些很少是一个好主意,但在这种情况下,我认为这只是门票。这是它的外观:

function repeatable_rand_text(ftype IN VARCHAR2, in_val IN VARCHAR2)
    -- The idea is to assign random values to an entity (such as family name),
    -- and to repeat that value each time that entity is seen again.  In this
    -- manner, we can assign random values without losing associations such as
    -- (again) family NAMES.
    --     JONES  -> POTATO
    --     SMITH  -> CARROT
    --     DOE    -> ONION
    --     JONES  -> POTATO
    --
    -- Parameters:
    --    field-type  ('ADJ'  'NOUN'   'BOTH')
    --    field-value
    --
    RETURN VARCHAR2
    DETERMINISTIC IS
    PRAGMA AUTONOMOUS_TRANSACTION;
    workval         VARCHAR2(64);
    insert_needed   BOOLEAN := FALSE;
BEGIN
    BEGIN
        SELECT new_name
        INTO   workval
        FROM   ps_dt_mixnames_preserve
        WHERE  name_type = UPPER(ftype) AND old_name = UPPER(in_val);
    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            workval := rand_text(ftype);
            insert_needed := TRUE;
    END;

    IF insert_needed THEN
        INSERT
        INTO   ps_dt_mixnames_preserve(name_type, old_name, new_name)
        VALUES (UPPER(ftype), UPPER(in_val), UPPER(workval));

        COMMIT;
    END IF;

    RETURN workval;
END repeatable_rand_text;
于 2012-09-25T17:55:44.937 回答
0

Not particularly pretty, but you could use an anonymous block:

set serveroutput on
declare
    cursor c is
        SELECT ORIG_NAME
        FROM MY_TABLE;
begin
    for r in c loop
        dbms_output.put_line(r.orig_name || ' -> ' ||
            repeatable_rand_text('last', r.orig_name));
    end loop;
end;
/

Depending on the data volume this may not be practical. And you could pad the values to make them align I suppose.

Shouldn't you be calling rand_text(in_val) rather than rand_text(ftype)? This will always get a randomised version of 'last', though to be fair it will still be different for each value of in_val.

于 2012-09-17T16:21:24.667 回答
0

您可以尝试使用 md5 哈希进行屏蔽。这样,表就包含了不能转换为原始(实际)名称的哈希值。我还假设您的查询通常会通过 emp_id 或类似键加入,而不是实际名称。

因此,为所有名称更新一次您的开发表:

update my_table
set lastname = rawtohex(dbms_crypto.hash(utl_i18n.string_to_raw(lastname,'AL32UTF8'),2));

commit;

请注意,这对姓氏区分大小写,但如果您愿意,可以使用大写(姓氏)。希望有帮助。

于 2012-09-17T17:29:09.467 回答