簡體   English   中英

Oracle (+) 聯接轉換為 SQL Server

[英]Oracle (+) Joins converting to SQL Server

我有一個使用舊式 Oracle 連接語法的查詢。 我正在嘗試將其轉換為 T-SQL 以反映數據存儲更改。

我的查詢是:

SELECT
    client_name1,
    personal_business_ind,
    birth_date,
    sex
FROM 
    client ce,
    exclusions_endorsements ee
WHERE 
    ce.client_reference = :clientid
    AND ee.fourth_insert(+) = ce.client_reference
    AND ee.identification_code(+) = 'OCCP1'
ORDER BY
    ee.run_date_last_trans DESC;

有人可以幫忙嗎?

我嘗試了以下查詢,但它似乎沒有在 SQL Server 上產生正確的輸出:

SELECT
    client_name1,
    personal_business_ind,
    birth_date,
    sex
FROM 
    client ce,
RIGHT OUTER JOIN 
    [CLOAS].[EE_ExclusionsEndorsements] ee ON ee.fourth_insert = ce.client_reference AND ee.identification_code = 'OCCP1'
WHERE 
    ce.client_reference = @clientid
ORDER BY
    ee.run_date_last_trans DESC;

那是“舊”的 Oracle 外連接運算符。 例如:

SQL> select d.deptno, max(e.ename) ename
  2  from dept d, emp e
  3  where e.deptno (+) = d.deptno
  4  group by d.deptno
  5  order by d.deptno;

    DEPTNO ENAME
---------- ----------
        10 MILLER
        20 SMITH
        30 WARD
        40

你 - 相反 - 使用

SQL> select d.deptno, max(e.ename) ename
  2  from dept d left join emp e on e.deptno = d.deptno
  3  group by d.deptno
  4  order by d.deptno;

    DEPTNO ENAME
---------- ----------
        10 MILLER
        20 SMITH
        30 WARD
        40

SQL>

或者,就你而言,

SELECT
    client_name1,
    personal_business_ind,
    birth_date,
    sex
FROM exclusions_endorsements ee left join client ce on ee.fourth_insert = ce.client_reference
                                                   AND ee.identification_code = 'OCCP1'
WHERE 
    ce.client_reference = :clientid   
ORDER BY
    ee.run_date_last_trans DESC;

你很近。 但是外部連接表是exclusions_endorsements ,而不是client ,所以你需要一個LEFT OUTER JOIN 在您的查詢中FROM client ce之后有一個逗號太多。

還有一點:Oracle 在降序時先對空值進行排序,SQL Server 將它們排在最后。 並且 SQL Server 不支持標准 SQL 語法NULLS FIRST 因此,我們需要一個 case 表達式。

SELECT
    client_name1,
    personal_business_ind,
    birth_date,
    sex
FROM client ce
LEFT OUTER JOIN exclusions_endorsements ee
                  ON ee.fourth_insert = ce.client_reference
                 AND ee.identification_code = 'OCCP1'
WHERE 
    ce.client_reference = @clientid
ORDER BY
    CASE WHEN ee.run_date_last_trans IS NULL THEN 1 ELSE 2 END,
    ee.run_date_last_trans DESC;

我應該補充一點,查詢看起來會更好,您是否對所有列進行了限定。 例如ce.client_name1 按您未選擇的列排序會使結果看起來完全無序。 好吧,也許它是一個不可為空的列,在這種情況下,它只會在結果中將外部連接的行與內部連接的行分開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM