繁体   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