簡體   English   中英

將SQL Server動態游標轉換為Oracle

[英]Converting SQL Server Dynamic Cursor to Oracle

我有一個表,其中包含兩個不同表中的行之間的匹配結果。 在表a中,行由export_invoice_id列唯一標識,在表b中,記錄由import_invoice_id列唯一標識。 除了這些識別列外,兩個表都有一組包含發票抬頭數據的列。 我需要匹配這兩個表中的行

有一項工作要遍歷表a和b中的記錄,並根據表a中的任何數據行與表b中的任何數據行匹配的程度來創建匹配分數。 結果存儲在表匹配項中。 該表包含表a中的export_invoice_id,表b中的import_invoice_id和match_score,指示記錄的相等性。 match_score越高,兩個表中的記錄越相等。

匹配表中的數據示例:

 IMPORT_INVOICE_ID EXPORT_INVOICE_ID MATCH_SCORE SORT_ID 0 5117095 17 1 0 5117096 9 2 0 5117097 9 3 1 5117096 17 4 1 5117097 17 5 1 5117095 9 6 2 5117097 17 7 2 5117096 10 8 2 5117095 9 9 

列sort_id給出排序順序(SORT BY import_invoice_id,match_score DESC,export_invoice_id)

我想從此匹配表中刪除行,以便只剩下表a中的export_invoice id與表b中的import_invoice_id的最佳匹配。 每個import_invoice_id和export_invoice_id值只能使用一次。

 IMPORT_INVOICE_ID EXPORT_INVOICE_ID MATCH_SCORE SORT_ID 0 5117095 17 1 1 5117096 17 4 2 5117097 17 7 

要在SQL Server中做到這一點,我需要執行以下操作(請注意CURSOR DYNAMIC):

DECLARE @ExportInvoiceId AS int;
DECLARE @ImportInvoiceID AS int;
DECLARE @SortId AS bigint; 
DECLARE Match_Cursor CURSOR DYNAMIC TYPE_WARNING FOR 
SELECT export_invoice_id, import_invoice_id, sort_id FROM matches ORDER BY sort_id
OPEN Match_Cursor
FETCH NEXT FROM Match_Cursor INTO @ExportInvoiceId, @ImportInvoiceID, @SortId;
WHILE @@FETCH_STATUS = 0 
    BEGIN 
        DELETE FROM matches WHERE sort_id > @SortId AND(export_invoice_id = @ExportInvoiceId OR import_invoice_id = @ImportInvoiceId)
        FETCH NEXT FROM Match_Cursor INTO @ExportInvoiceId, @ImportInvoiceID, @SortId; 
    END; 
CLOSE Match_Cursor; 
DEALLOCATE Match_Cursor; 

在Oracle中如何做到這一點?

比我想象的要難的聖牛

問題是oracle讀取一致性的方式與sql server的方式不同

很難在游標中執行此操作,因為如果您打開游標,則結果集不會刷新,除非您在刪除后重新打開它

所以-試試這個,這只是一個直接刪除,沒有光標

 delete
 from matches a
 where (a.match_score,a.sort_id) not in (select max(b.match_score), min(sort_id)
                        from matches b
                        where (a.import_invoice_id=b.import_invoice_id
                        or a.export_invoice_id=b.import_invoice_id))

暫無
暫無

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

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