Question

I have an SQL query with exact the same code, but two different tables (AUDIT_TRAIL_ARCHIVE and AUDIT_TRAIL). I use "UNION ALL" to have one result.

Good programmers use "Don't repeat yourself" principle. Good programmers avoid WET (write everything twice).

Howto rewrite this code with "Don't repeat yourself" principle?

SELECT REPLACE (ENTITY_KEY, 'rss_user_name=CN=', '')
 FROM    AUDIT_TRAIL_ARCHIVE AU
   INNER JOIN
      (SELECT RSS_USER_NAME
         FROM RSS_USER
        WHERE RSS_NAME = 'rmad'
              AND ADD_INFO_MASTER LIKE '%__47__UPN=%@richemont.com%') FALSCH
   ON REPLACE (AU.ENTITY_KEY, 'rss_user_name=CN=', '') =
         FALSCH.RSS_USER_NAME
WHERE     AU.RSS_NAME = 'rmad'
   AND AU.TABLE_NAME = 'rss_user'
   AND AU.ACTION = 'Insert'
   AND AU.ENTITY_KEY LIKE 'rss_user_name=CN=%'
   AND AU.ORIGIN != 'RSS'
UNION ALL
SELECT REPLACE (ENTITY_KEY, 'rss_user_name=CN=', '')
 FROM    AUDIT_TRAIL AU
   INNER JOIN
      (SELECT RSS_USER_NAME
         FROM RSS_USER
        WHERE RSS_NAME = 'rmad'
              AND ADD_INFO_MASTER LIKE '%__47__UPN=%@richemont.com%') FALSCH
   ON REPLACE (AU.ENTITY_KEY, 'rss_user_name=CN=', '') =
         FALSCH.RSS_USER_NAME
WHERE     AU.RSS_NAME = 'rmad'
   AND AU.TABLE_NAME = 'rss_user'
   AND AU.ACTION = 'Insert'
   AND AU.ENTITY_KEY LIKE 'rss_user_name=CN=%'
   AND AU.ORIGIN != 'RSS'
Was it helpful?

Solution

For example:

SELECT REPLACE (ENTITY_KEY, 'rss_user_name=CN=', '')
 FROM    (select * --or relevant columns
          from AUDIT_TRAIL_ARCHIVE AU
          union all
          select * 
          from AUDIT_TRAIL AU
           ) AU
   INNER JOIN
      (SELECT RSS_USER_NAME
         FROM RSS_USER
        WHERE RSS_NAME = 'rmad'
              AND ADD_INFO_MASTER LIKE '%__47__UPN=%@richemont.com%') FALSCH
   ON REPLACE (AU.ENTITY_KEY, 'rss_user_name=CN=', '') =
         FALSCH.RSS_USER_NAME
WHERE     AU.RSS_NAME = 'rmad'
   AND AU.TABLE_NAME = 'rss_user'
   AND AU.ACTION = 'Insert'
   AND AU.ENTITY_KEY LIKE 'rss_user_name=CN=%'
   AND AU.ORIGIN != 'RSS'

OTHER TIPS

Simply you can not. SQL is compiled language (even if it looks like scripting) and Oracle remembers OBJECT_IDs the query depends. Each half of the query has different dependencies, different "bytecode" and different execution plan.

You can

  • Use tablespace partitioning. Then you would have only one table. Queries against live data could be restricted using "select * from AUDIT_TRAIL partition ACTIVE".

  • Use query factoring like

    WITH AU AS
    (SELECT * from AUDIT_TRAIL union all select * from AUDIT_TRAIL_ARCHIVE)
    SELECT REPLACE (ENTITY_KEY, 'rss_user_name=CN=', '')
    FROM AU JOIN ...
    ...
    

    But I'm not sure whether in this case will Oracle guarantee the same efficiency of execution plan.

Good programmers use "Don't repeat yourself" principle. Good programmers avoid WET (write everything twice).

Heh. I like that. subtle.

also, I'm probably being way too basic but would something like this work:

CREATE [OR REPLACE] PROCEDURE <name_of_procedure> [ (<ENTITY_KEY_variable>) ]
IS
    <ENTITY_KEY=ENTITY_KEY_variable>
BEGIN
    <your code goes here>

    [EXCEPTION
        exception_section]
END [procedure_name];

edit: i see from the first posted answer I fell for a trolling? silly me.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top