如何在表之间翻转 Oracle 同义词
How to flip Oracle Synonyms between tables
我有以下两个table。
A_DETAILS
B_DETAILS
我在上面 tables
上创建了以下两个 SYNONYM
A_DETAILS 指向 --> L_DETAILS
B_DETAILS 指向 --> R_DETAILS
我想在某个时间点翻转同义词,例如
A_DETAILS 指向 --> R_DETAILS
B_DETAILS 指向 --> L_DETAILS
我尝试了下面的代码,但它创建了单个 table 的同义词并从另一个 table.
中删除
BEGIN
FOR reg IN
(SELECT SYNONYM_NAME, TABLE_NAME, OWNER
FROM ALL_SYNONYMS
WHERE OWNER IN ('MY_SCHEMA') AND SUBSTR (TABLE_NAME, 1, 2) = 'A_')
LOOP
v_Statement := 'CREATE OR REPLACE SYNONYM '|| reg.owner|| '.'|| reg.SYNONYM_NAME|| ' FOR '|| reg.owner|| '.'|| 'B_'|| SUBSTR (reg.TABLE_NAME, 3);
EXECUTE IMMEDIATE v_Statement;
END LOOP;
FOR reg
IN (SELECT SYNONYM_NAME, TABLE_NAME, OWNER
FROM ALL_SYNONYMS
WHERE OWNER IN ('MY_SCHEMA') AND SUBSTR (TABLE_NAME, 1, 2) = 'B_')
LOOP
v_Statement :='CREATE OR REPLACE SYNONYM '|| reg.owner|| '.'|| reg.SYNONYM_NAME|| ' FOR '|| reg.owner|| '.'|| 'A_'|| SUBSTR (reg.TABLE_NAME, 3);
EXECUTE IMMEDIATE v_Statement;
END LOOP;
END;
在一个循环而不是两个循环中完成。当您进入第二个循环时,第一个同义词已更新为指向 B_,因此第二个循环正在更新两个同义词以指向 A_。
BEGIN
FOR reg IN (
SELECT SYNONYM_NAME, TABLE_NAME, OWNER
, case SUBSTR (TABLE_NAME, 1, 2)
when 'A_' then 'B_'
when 'B_' then 'A_'
end || SUBSTR (TABLE_NAME, 3) NEW_TABLE
FROM ALL_SYNONYMS
WHERE OWNER IN ('MY_SCHEMA') AND SUBSTR (TABLE_NAME, 1, 2) in ('A_','B_'))
LOOP
v_Statement := 'CREATE OR REPLACE SYNONYM '|| reg.owner|| '.'|| reg.SYNONYM_NAME
|| ' FOR '|| reg.owner|| '.'|| reg.NEW_TABLE;
EXECUTE IMMEDIATE v_Statement;
END LOOP;
END;
我有以下两个table。
A_DETAILS
B_DETAILS
我在上面 tables
上创建了以下两个 SYNONYMA_DETAILS 指向 --> L_DETAILS
B_DETAILS 指向 --> R_DETAILS
我想在某个时间点翻转同义词,例如
A_DETAILS 指向 --> R_DETAILS
B_DETAILS 指向 --> L_DETAILS
我尝试了下面的代码,但它创建了单个 table 的同义词并从另一个 table.
中删除BEGIN
FOR reg IN
(SELECT SYNONYM_NAME, TABLE_NAME, OWNER
FROM ALL_SYNONYMS
WHERE OWNER IN ('MY_SCHEMA') AND SUBSTR (TABLE_NAME, 1, 2) = 'A_')
LOOP
v_Statement := 'CREATE OR REPLACE SYNONYM '|| reg.owner|| '.'|| reg.SYNONYM_NAME|| ' FOR '|| reg.owner|| '.'|| 'B_'|| SUBSTR (reg.TABLE_NAME, 3);
EXECUTE IMMEDIATE v_Statement;
END LOOP;
FOR reg
IN (SELECT SYNONYM_NAME, TABLE_NAME, OWNER
FROM ALL_SYNONYMS
WHERE OWNER IN ('MY_SCHEMA') AND SUBSTR (TABLE_NAME, 1, 2) = 'B_')
LOOP
v_Statement :='CREATE OR REPLACE SYNONYM '|| reg.owner|| '.'|| reg.SYNONYM_NAME|| ' FOR '|| reg.owner|| '.'|| 'A_'|| SUBSTR (reg.TABLE_NAME, 3);
EXECUTE IMMEDIATE v_Statement;
END LOOP;
END;
在一个循环而不是两个循环中完成。当您进入第二个循环时,第一个同义词已更新为指向 B_,因此第二个循环正在更新两个同义词以指向 A_。
BEGIN
FOR reg IN (
SELECT SYNONYM_NAME, TABLE_NAME, OWNER
, case SUBSTR (TABLE_NAME, 1, 2)
when 'A_' then 'B_'
when 'B_' then 'A_'
end || SUBSTR (TABLE_NAME, 3) NEW_TABLE
FROM ALL_SYNONYMS
WHERE OWNER IN ('MY_SCHEMA') AND SUBSTR (TABLE_NAME, 1, 2) in ('A_','B_'))
LOOP
v_Statement := 'CREATE OR REPLACE SYNONYM '|| reg.owner|| '.'|| reg.SYNONYM_NAME
|| ' FOR '|| reg.owner|| '.'|| reg.NEW_TABLE;
EXECUTE IMMEDIATE v_Statement;
END LOOP;
END;