0

我有一个很长的 SQL 脚本。非常丑陋的是,我不得不多次复制和粘贴现有代码。我更愿意参考现有的代码。

我的目标是每个代码块只写一次。如果我需要第二次,我会参考它。没有意见这可能吗?

SELECT batch
FROM (SELECT    'DISCONNECT ent_user  FROM job_code WITH user_id ="'
             || a.user_id
             || '" , jc_name = "'
             || b.jc_name
             || '";'
                AS batch,
             1 AS my_order
        FROM ent_user a, user_jc b
       WHERE a.user_id IN (SELECT user_id
                           FROM (SELECT REPLACE (overview.entity_key,
                                                 'user_id=',
                                                 '')
                                           AS USER_ID,
                                        details.old_value_str,
                                        SUBSTR (
                                           details.old_value_str,
                                           1,
                                           INSTR (details.old_value_str,
                                                  '-',
                                                  1,
                                                  1)
                                           - 1)
                                           AS OLD_PREFIX,
                                        details.new_value_str,
                                        SUBSTR (
                                           details.new_value_str,
                                           1,
                                           INSTR (details.new_value_str,
                                                  '-',
                                                  1,
                                                  1)
                                           - 1)
                                           AS NEW_PREFIX,
                                        overview.from_event_time,
                                        CASE
                                           WHEN SUBSTR (
                                                   details.old_value_str,
                                                   1,
                                                   INSTR (
                                                      details.old_value_str,
                                                      '-',
                                                      1,
                                                      1)
                                                   - 1) =
                                                   SUBSTR (
                                                      details.new_value_str,
                                                      1,
                                                      INSTR (
                                                         details.
                                                          new_value_str,
                                                         '-',
                                                         1,
                                                         1)
                                                      - 1)
                                           THEN
                                              'OK'
                                           ELSE
                                              'DELETE'
                                        END
                                           AS COMPARE
                                   FROM    audit_trail overview
                                        INNER JOIN
                                           audit_info details
                                        ON overview.serial = details.serial
                                  WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                           TO_CHAR (
                                              overview.from_event_time,
                                              'IW-YYYY')
                                        AND details.field_name =
                                               '__99__Department'
                                        AND overview.action = 'Update'
                                        AND CASE
                                               WHEN SUBSTR (
                                                       details.
                                                        old_value_str,
                                                       1,
                                                       INSTR (
                                                          details.
                                                           old_value_str,
                                                          '-',
                                                          1,
                                                          1)
                                                       - 1) =
                                                       SUBSTR (
                                                          details.
                                                           new_value_str,
                                                          1,
                                                          INSTR (
                                                             details.
                                                              new_value_str,
                                                             '-',
                                                             1,
                                                             1)
                                                          - 1)
                                               THEN
                                                  'OK'
                                               ELSE
                                                  'DELETE'
                                            END = 'DELETE'
                                        AND REPLACE (overview.entity_key,
                                                     'user_id=',
                                                     '') NOT LIKE
                                               'EXT%'
                                        AND details.old_value_str LIKE
                                               'INT%'
                                        AND details.new_value_str NOT LIKE
                                               'INT%'))
             AND a.user_id = b.user_id
      UNION
      SELECT 'DISCONNECT rss_user  FROM user_group WITH rss_user_name = "'
             || b.rss_user_name
             || '"  rss_name = "'
             || b.rss_name
             || '" rss_type = "'
             || b.rss_type
             || '" , ug_name = "'
             || b.ug_name
             || '";'
                AS batch,
             2 AS my_order
        FROM ent_user a, ru_ug b
       WHERE a.user_id IN (SELECT user_id
                           FROM (SELECT REPLACE (overview.entity_key,
                                                 'user_id=',
                                                 '')
                                           AS USER_ID,
                                        details.old_value_str,
                                        SUBSTR (
                                           details.old_value_str,
                                           1,
                                           INSTR (
                                              details.old_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS OLD_PREFIX,
                                        details.new_value_str,
                                        SUBSTR (
                                           details.new_value_str,
                                           1,
                                           INSTR (
                                              details.new_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS NEW_PREFIX,
                                        overview.from_event_time,
                                        CASE
                                           WHEN SUBSTR (
                                                   details.
                                                    old_value_str,
                                                   1,
                                                   INSTR (
                                                      details.
                                                       old_value_str,
                                                      '-',
                                                      1,
                                                      1)
                                                   - 1) =
                                                   SUBSTR (
                                                      details.
                                                       new_value_str,
                                                      1,
                                                      INSTR (
                                                         details.
                                                          new_value_str,
                                                         '-',
                                                         1,
                                                         1)
                                                      - 1)
                                           THEN
                                              'OK'
                                           ELSE
                                              'DELETE'
                                        END
                                           AS COMPARE
                                   FROM    audit_trail overview
                                        INNER JOIN
                                           audit_info details
                                        ON overview.serial =
                                              details.serial
                                  WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                           TO_CHAR (
                                              overview.from_event_time,
                                              'IW-YYYY')
                                        AND details.field_name =
                                               '__99__Department'
                                        AND overview.action = 'Update'
                                        AND CASE
                                               WHEN SUBSTR (
                                                       details.
                                                        old_value_str,
                                                       1,
                                                       INSTR (
                                                          details.
                                                           old_value_str,
                                                          '-',
                                                          1,
                                                          1)
                                                       - 1) =
                                                       SUBSTR (
                                                          details.
                                                           new_value_str,
                                                          1,
                                                          INSTR (
                                                             details.
                                                              new_value_str,
                                                             '-',
                                                             1,
                                                             1)
                                                          - 1)
                                               THEN
                                                  'OK'
                                               ELSE
                                                  'DELETE'
                                            END = 'DELETE'
                                        AND REPLACE (
                                               overview.entity_key,
                                               'user_id=',
                                               '') NOT LIKE
                                               'EXT%'
                                        AND details.old_value_str LIKE
                                               'INT%'
                                        AND details.new_value_str NOT LIKE
                                               'INT%'))
             AND a.user_id = b.user_id
             AND (   b.rss_type = 'TEST14'
                  OR b.rss_type = 'TTTR'
                  OR b.rss_type = 'OFFI2021'
                  OR b.rss_type = 'TEST27'
                  OR b.rss_type = 'TEST22'
                  OR b.rss_type = 'TEST26'
                  OR b.rss_type = 'TEST23'
                  OR b.rss_type = 'TEST39'
                  OR b.rss_type = 'TEST19'
                  OR b.rss_type = 'TEST12'
                  OR b.rss_type = 'TEST16'
                  OR b.rss_type = 'TEST05'
                  OR b.rss_type = 'TEST09'
                  OR b.rss_type = 'HSSO'
                  OR b.rss_type = 'TEST37'
                  OR b.rss_type = 'TEST34'
                  OR b.rss_type = 'TEST1')
      UNION
      SELECT    'DELETE rss_user WITH rss_user_name = "'
             || b.rss_user_name
             || '"  rss_name = "'
             || b.rss_name
             || '" rss_type = "'
             || b.rss_type
             || '";'
                AS batch,
             3 AS my_order
        FROM ent_user a, rss_user b
       WHERE a.user_id IN (SELECT user_id
                           FROM (SELECT REPLACE (overview.entity_key,
                                                 'user_id=',
                                                 '')
                                           AS USER_ID,
                                        details.old_value_str,
                                        SUBSTR (
                                           details.old_value_str,
                                           1,
                                           INSTR (
                                              details.old_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS OLD_PREFIX,
                                        details.new_value_str,
                                        SUBSTR (
                                           details.new_value_str,
                                           1,
                                           INSTR (
                                              details.new_value_str,
                                              '-',
                                              1,
                                              1)
                                           - 1)
                                           AS NEW_PREFIX,
                                        overview.from_event_time,
                                        CASE
                                           WHEN SUBSTR (
                                                   details.
                                                    old_value_str,
                                                   1,
                                                   INSTR (
                                                      details.
                                                       old_value_str,
                                                      '-',
                                                      1,
                                                      1)
                                                   - 1) =
                                                   SUBSTR (
                                                      details.
                                                       new_value_str,
                                                      1,
                                                      INSTR (
                                                         details.
                                                          new_value_str,
                                                         '-',
                                                         1,
                                                         1)
                                                      - 1)
                                           THEN
                                              'OK'
                                           ELSE
                                              'DELETE'
                                        END
                                           AS COMPARE
                                   FROM    audit_trail overview
                                        INNER JOIN
                                           audit_info details
                                        ON overview.serial =
                                              details.serial
                                  WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                           TO_CHAR (
                                              overview.from_event_time,
                                              'IW-YYYY')
                                        AND details.field_name =
                                               '__99__Department'
                                        AND overview.action = 'Update'
                                        AND CASE
                                               WHEN SUBSTR (
                                                       details.
                                                        old_value_str,
                                                       1,
                                                       INSTR (
                                                          details.
                                                           old_value_str,
                                                          '-',
                                                          1,
                                                          1)
                                                       - 1) =
                                                       SUBSTR (
                                                          details.
                                                           new_value_str,
                                                          1,
                                                          INSTR (
                                                             details.
                                                              new_value_str,
                                                             '-',
                                                             1,
                                                             1)
                                                          - 1)
                                               THEN
                                                  'OK'
                                               ELSE
                                                  'DELETE'
                                            END = 'DELETE'
                                        AND REPLACE (
                                               overview.entity_key,
                                               'user_id=',
                                               '') NOT LIKE
                                               'EXT%'
                                        AND details.old_value_str LIKE
                                               'INT%'
                                        AND details.new_value_str NOT LIKE
                                               'INT%'))
             AND a.user_id = b.user_id
             AND (   b.rss_type = 'TEST14'
                  OR b.rss_type = 'TEST27'
                  OR b.rss_type = 'TEST22'
                  OR b.rss_type = 'TEST26'
                  OR b.rss_type = 'TEST23'
                  OR b.rss_type = 'TEST39'
                  OR b.rss_type = 'TEST19'
                  OR b.rss_type = 'TEST12'
                  OR b.rss_type = 'TEST16'
                  OR b.rss_type = 'TEST09'
                  OR b.rss_type = 'TEST05'
                  OR b.rss_type = 'TEST37'
                  OR b.rss_type = 'TEST34'
                  OR b.rss_type = 'TEST1')
          ORDER BY my_order;

您可以在没有重复代码的情况下重写查询吗?

4

1 回答 1

1

创建一个像

create view details
as
select d.*, 
       substr(d.old_value_str, 1, instr(details.old_value_str, '-', 1, 1) - 1) as old_prefix, 
       substr(d.new_value_str, 1, instr(details.new_value_str, '-', 1, 1) - 1) as new_prefix,
       case
         when substr (d.old_value_str, 1, instr (d.old_value_str, '-', 1, 1) - 1)
               = substr (d.new_value_str, 1, instr (d.new_value_str, '-', 1, 1) - 1)
         then
           'OK'
         else
           'DELETE'
       end as compare
  from audit_info d;

(或在 awith clause而不是视图中使用它)

然后只是参考

   WHERE a.user_id IN (SELECT user_id
                       FROM (SELECT REPLACE (overview.entity_key,
                                             'user_id=',
                                             '')
                                       AS USER_ID,
                                    details.old_value_str,
                                    OLD_PREFIX,
                                    details.new_value_str,
                                    NEW_PREFIX,
                                    overview.from_event_time,
                                    COMPARE
                               FROM    audit_trail overview
                                    INNER JOIN  details
                                    ON overview.serial = details.serial
                              WHERE TO_CHAR (SYSDATE, 'IW-YYYY') =
                                       TO_CHAR (
                                          overview.from_event_time,
                                          'IW-YYYY')
                                    AND details.field_name =
                                           '__99__Department'
                                    AND overview.action = 'Update'
                                    AND CASE
                                           WHEN old_prefix =
                                                   new_prefix
                                           THEN
                                              'OK'
                                           ELSE

ETC..

于 2013-02-25T11:33:30.707 回答