协助替换oracle(+)连接到ANSI连接

时间:2015-08-13 13:26:27

标签: oracle select join

我已经回顾了其他帖子以及有关使用ANSI格式联接替换旧样式(+)联接的Oracle文档。我很难将其转换为我认为是两个左外连接操作。我的挂断是select语句中的两个,以及应该放置连接的位置。

这是我的问题:

SELECT
  AUP.USERNAME,
  AUP.MENU_STRING MODULE,
  NVL(UGA.PERMISSION,AUP.DEFAULT_PERMISSION) PERMISSION
FROM
  (SELECT
    DU.USERNAME,
    A.PROGRAM_ID,
    A.MENU_STRING,
    'Y' DEFAULT_PERMISSION
  FROM
    APPLICATION A,
    DBA_USERS DU
  WHERE 
    A.PROGRAM_ID NOT IN ('.SEPARATOR')
    AND DU.USERNAME NOT LIKE '%#') AUP,
  (SELECT
    USER_ID,
    PROGRAM_ID,
    PERMISSION
  FROM
    USER_PGM_AUTHORITY
  WHERE
    PROGRAM_COMPONENT='PROGRAM') UGA
WHERE
  AUP.USERNAME=UGA.USER_ID(+)
  AND AUP.PROGRAM_ID=UGA.PROGRAM_ID(+)
  AND aup.menu_string = 'Vendor Maintenance'
ORDER BY
  AUP.USERNAME,
  AUP.MENU_STRING;

3 个答案:

答案 0 :(得分:0)

我认为这样做会:

SELECT
  AUP.USERNAME,
  AUP.MENU_STRING MODULE,
  NVL(UGA.PERMISSION,AUP.DEFAULT_PERMISSION) PERMISSION
FROM
  (SELECT
    DU.USERNAME,
    A.PROGRAM_ID,
    A.MENU_STRING,
    'Y' DEFAULT_PERMISSION
  FROM
    APPLICATION A,
    DBA_USERS DU
  WHERE 
    A.PROGRAM_ID NOT IN ('.SEPARATOR')
    AND DU.USERNAME NOT LIKE '%#') AUP
LEFT JOIN
  (SELECT
    USER_ID,
    PROGRAM_ID,
    PERMISSION
  FROM
    USER_PGM_AUTHORITY
  WHERE
    PROGRAM_COMPONENT='PROGRAM') UGA
ON
  AUP.USERNAME=UGA.USER_ID
  AND AUP.PROGRAM_ID=UGA.PROGRAM_ID
WHERE
  aup.menu_string = 'Vendor Maintenance'
ORDER BY
  AUP.USERNAME,
  AUP.MENU_STRING;

答案 1 :(得分:0)

联接介于AUPUGA之间:

SELECT
  AUP.USERNAME,
  AUP.MENU_STRING MODULE,
  NVL(UGA.PERMISSION,AUP.DEFAULT_PERMISSION) PERMISSION
FROM
  (SELECT
    DU.USERNAME,
    A.PROGRAM_ID,
    A.MENU_STRING,
    'Y' DEFAULT_PERMISSION
  FROM
    APPLICATION A,
    DBA_USERS DU
  WHERE 
    A.PROGRAM_ID NOT IN ('.SEPARATOR')
    AND DU.USERNAME NOT LIKE '%#') AUP
LEFT JOIN
  (SELECT
    USER_ID,
    PROGRAM_ID,
    PERMISSION
  FROM
    USER_PGM_AUTHORITY
  WHERE
    PROGRAM_COMPONENT='PROGRAM') UGA
ON AUP.USERNAME=UGA.USER_ID
AND AUP.PROGRAM_ID=UGA.PROGRAM_ID
WHERE aup.menu_string = 'Vendor Maintenance'
ORDER BY
  AUP.USERNAME,
  AUP.MENU_STRING;

答案 2 :(得分:0)

您可以使用命名子查询对其进行重构,以使其更具可读性:

WITH AUP AS (                                -- Moved to a named sub-query
  SELECT
    DU.USERNAME,
    A.PROGRAM_ID,
    A.MENU_STRING,
    'Y' DEFAULT_PERMISSION
  FROM
    APPLICATION A
    CROSS JOIN                               -- No apparent join condition so used an explicit CROSS JOIN.
    DBA_USERS DU
  WHERE 
        A.PROGRAM_ID  NOT IN ('.SEPARATOR')
    AND DU.USERNAME   NOT LIKE '%#'
    AND A.MENU_STRING = 'Vendor Maintenance' -- Moved to the appropriate sub-query
),
UGA AS (                                     -- Moved to a named sub-query
  SELECT
    USER_ID,
    PROGRAM_ID,
    PERMISSION
  FROM
    USER_PGM_AUTHORITY
  WHERE
    PROGRAM_COMPONENT='PROGRAM'
)
SELECT
  AUP.USERNAME,
  AUP.MENU_STRING MODULE,
  NVL(UGA.PERMISSION,AUP.DEFAULT_PERMISSION) PERMISSION
FROM
  AUP
  LEFT OUTER JOIN
  UGA
  ON (    AUP.USERNAME  =UGA.USER_ID
      AND AUP.PROGRAM_ID=UGA.PROGRAM_ID )
ORDER BY
  AUP.USERNAME,
  AUP.MENU_STRING;