我正在循環訪問數組並執行插入/更新。在循環中,我檢查是否存在記錄。如果不是,請執行插入。如果是,則執行更新。 STRING_SPLIT_FNC是一個包,它接受一個字符串並將其拆分到分隔符(〜)並將拆分字符串存儲在一個數組中。在數組循環中的Oracle查詢 - 奇怪的結果
DECLARE
service_name VARCHAR(50) := 'Service1';
service_version VARCHAR(10) := '2016';
i INTEGER;
record_count NUMBER;
TYPE T_ARRAY_OF_VARCHAR IS TABLE OF VARCHAR(2000) INDEX BY BINARY_INTEGER;
main_array T_ARRAY_OF_VARCHAR;
split_array STRING_SPLIT_FNC.T_ARRAY;
BEGIN
main_array(1) := '2014|2015|2016|~service.info~25500~NULL~1';
main_array(2) := '2014|2015|2016|2017~service.path~/mypath/myfolder/myfile.zip~0';
main_array(3) := '2014|2015|2016|2017|2018~service.date~Yes~NULL~1';
SELECT COUNT(SERVICE_ID) INTO record_count FROM TEST_SERVICE WHERE SERVICE_DESC = service_name AND SERVICE_VERSION = service_version;
IF record_count = 0 THEN
INSERT INTO TEST_SERVICE(SERVICE_ID, SERVICE_DESC, SERVICE_VERSION) VALUES (SERVICE_SEQ.nextval, service_name, service_version);
END IF;
SELECT SERVICE_ID INTO service_id FROM TEST_SERVICE WHERE SERVICE_DESC = service_name AND SERVICE_VERSION = service_version;
i := main_array.FIRST;
LOOP
record_count := 0;
split_array := STRING_SPLIT_FNC.SPLIT(main_array(i),'~');
IF (INSTR(split_array(1), service_version) > 0) THEN
DBMS_OUTPUT.PUT_LINE('Record count prior: ' || record_count || ' Service Id: ' || service_id || ' Config: ' || split_array(2));
SELECT COUNT(*) INTO record_count FROM TEST_REF_SERVICE_CONFIG WHERE SERVICE_ID = service_id AND CONFIG_NAME = split_array(2);
DBMS_OUTPUT.PUT_LINE('Record count after: ' || record_count || ' Service Id: ' || service_id || ' Config: ' || split_array(2));
IF record_count = 0 THEN
INSERT INTO TEST_REF_SERVICE_CONFIG (REF_CONFIG_ID, SERVICE_ID, CONFIG_NAME, DEFAULT_VALUE, ALLOW_OVERRIDE) VALUES (REF_SERVICE_CONFIG_SEQ.nextval, service_id, split_array(2), split_array(3), TO_NUMBER(split_array(5)));
DBMS_OUTPUT.PUT_LINE('Inserted Service: ' || service_name || '[' || service_version || '], Config: ' || split_array(2) || ' [' || split_array(3) || '], Override: ' || split_array(5));
ELSE
record_count := 0;
SELECT COUNT(REF_CONFIG_ID) INTO record_count FROM TEST_REF_SERVICE_CONFIG WHERE SERVICE_ID = service_id AND CONFIG_NAME = split_array(2) AND DEFAULT_VALUE = split_array(3);
IF record_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('Record count after [in update part]: ' || record_count);
UPDATE TEST_REF_SERVICE_CONFIG SET DEFAULT_VALUE = split_array(3), ALLOW_OVERRIDE = split_array(5) WHERE SERVICE_ID = service_id AND CONFIG_NAME = split_array(2) AND DEFAULT_VALUE = split_array(3);
DBMS_OUTPUT.PUT_LINE('Updated Service: ' || service_name || '[' || service_version || '], Config: ' || split_array(2) || ' [' || split_array(3) || '], Override: ' || split_array(5));
ELSE
record_count := 0;
DBMS_OUTPUT.PUT_LINE('No insert or update performed. Service: ' || service_name || '[' || service_version || '], Config: ' || split_array(2) || ' [' || split_array(3) || '], Override: ' || split_array(5));
END IF;
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('Specified service/version not found ' || service_name || '[' || service_version || ']');
END IF;
i := main_array.NEXT(i);
EXIT WHEN i IS NULL;
END LOOP;
END;
假設TEST_REF_SERVICE_CONFIG表中沒有記錄。 現在讓我們說我爲service_name = Service1和service_version = 2015運行此。service_id是500.插入main_array的所有3個元素。
現在,我運行service_name = Service1和service_version = 2017.服務ID是502.它應該只插入main_array中的最後2條記錄。
下面返回查詢record_count = 1時,它應該是0:
SELECT COUNT(*) INTO record_count FROM TEST_REF_SERVICE_CONFIG WHERE SERVICE_ID = service_id AND CONFIG_NAME = split_array(2);
當我單獨運行查詢,我得到計數爲0
SELECT COUNT(*) FROM TEST_REF_SERVICE_CONFIG WHERE SERVICE_ID = 502 AND CONFIG_NAME = 'service.path';
爲什麼查詢中當它應該是0時,循環返回計數爲1?我也試過:
DECLARE
config_name VARCHAR(250);
...
LOOP
record_count := 0;
split_array := STRING_SPLIT_FNC.SPLIT(main_array(i),'~');
IF (INSTR(split_array(1), service_version) > 0) THEN
config_name := split_array(3);
SELECT COUNT(*) INTO record_count FROM TEST_REF_SERVICE_CONFIG WHERE SERVICE_ID = service_id AND CONFIG_NAME = config_name;
現在我得到record_count爲121 !!! 在此先感謝您的幫助。
您發佈的代碼沒有名爲'service_id'的PL/SQL變量,因此'SELECT SERVICE_ID INTO service_id ...'會失敗;你剛剛離開你發佈的代碼嗎?讓局部變量名稱與表列相同將導致您的問題,但可能會導致你所看到的... –