我有一個 12 列的表:
表格1:
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 美國廣播公司 | 1 | 000 | 啊啊啊啊 | 贊 | 2 | 234 | 面向物件 | 00001 | 01 | 123 | 214 |
| 定義 | 2 | 023 | bbb | 年年 | 4 | 345 | 購買力平價 | 00002 | 02 | 133 | 224 |
| 吉 | 3 | 011 | 抄送 | xxx | 6 | 456 | 00003 | 03 | 143 | 234 | |
| jkl | 4 | 112 | 滴滴 | 萬維網 | 8 | 567 | 存款準備金率 | 00004 | 04 | 153 | 244 |
我想在回圈中使用第三列資料并從另一個表中獲取“最佳匹配”資料。
表2:
| 1 | 2 | 3 | 4 |
|---|---|---|---|
| 0 | 777 | 676 | 美國 |
| 00 | 888 | 878 | 英國 |
| 01 | 999 | 989 | 法國 |
| 02 | 666 | 656 | 德國 |
將在回圈中修剪第三列資料,直到獲取 table2 中的匹配項。
first row:
iter 1: table1 row1 col3=000 -- no match in table
iter 2: table1 row1 col3=00 -- return england, replace table1 row1 col12=214 with 'england'
updated row: abc,1,000,aaa,zzz,2,234,OOO,00001,01,123,england
second row:
iter 1: table1 row2 col3=023 -- no match in table
iter 2: table1 row2 col3=02 -- return germany, replace table1 row1 col12=224 with 'germany'
updated row: def,2,023,bbb,yyy,4,345,PPP,00002,02,133,germany
uj5u.com熱心網友回復:
您需要做的是創建一個程序,然后在該程序中宣告一個游標以及一個variable_c_row cursor_name%ROWTYPE.
在程式中,這將是以下內容:
OPEN cursor_name
FETCH cursor_name INTO variable_c_row;
WHILE cursor_name%FOUND LOOP
-- Declare a number variable (i)
i := 0;
-- Declare a varchar variable (match)
match := variable_c_row.col3
WHILE length(match) > 0 LOOP OR l_countryname IS NULL
begin
-- Declare a yourrow%ROWTYPE variable (l_countryname)
SELECT col4 FROM table2 INTO l_countryname WHERE col1 = match;
UPDATE table1 SET col12 = l_countryname;
exception when no_data_found then null;
end;
i := i 1;
match := substr(variable_c_row.cow3, 0, length(match)-i);
END LOOP;
FETCH cursor_name INTO variable_c_row;
END LOOP;
CLOSE cursor_name;
由于該問題沒有 DDL 或 DML,因此我最多只能提供一個廣泛的答案,尚未經過測驗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/349016.html
上一篇:如何對多列進行計數(不同)
