我正在遍历一个数组并执行插入/更新。在循环中,我检查记录是否存在。如果没有,请执行插入。如果是,则执行更新。STRING_SPLIT_FNC 是一个包,它接受一个字符串并将其拆分为分隔符 (~),并将拆分后的字符串存储在一个数组中。
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 条记录。
下面的查询在它应该为 0 时返回 record_count = 1:
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 !!!在此先感谢您的帮助。