This Oracle PL/SQL Block will insert a comma-separated value to a table.
set serveroutput on
DECLARE
vcntrycd_list VARCHAR2(100) := 'ABC,DEFG,HIJKLM,NO';
vcntrycd VARCHAR2(10);
vpos NUMBER;
BEGIN
vpos:=INSTR(vcntrycd_list,',',1,1);
vcntrycd := SUBSTR(vcntrycd_list,1,vpos-1);
DBMS_OUTPUT.PUT_LINE('Country Code : '||vcntrycd);
vcntrycd_list := SUBSTR(vcntrycd_list,vpos+1);
WHILE vcntrycd IS NOT NULL
LOOP
insert into country
( country_code)
values
( vcntrycd );
vpos:=INSTR(vcntrycd_list,',',1,1);
vcntrycd := SUBSTR(vcntrycd_list,1,vpos-1);
DBMS_OUTPUT.PUT_LINE('Country Code : '||vcntrycd);
vcntrycd_list := SUBSTR(vcntrycd_list,vpos+1);
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE));
rollback;
END;
/
set serveroutput on
DECLARE
vcntrycd_list VARCHAR2(100) := 'ABC,DEFG,HIJKLM,NO';
vcntrycd VARCHAR2(10);
vpos NUMBER;
BEGIN
vpos:=INSTR(vcntrycd_list,',',1,1);
vcntrycd := SUBSTR(vcntrycd_list,1,vpos-1);
DBMS_OUTPUT.PUT_LINE('Country Code : '||vcntrycd);
vcntrycd_list := SUBSTR(vcntrycd_list,vpos+1);
WHILE vcntrycd IS NOT NULL
LOOP
insert into country
( country_code)
values
( vcntrycd );
vpos:=INSTR(vcntrycd_list,',',1,1);
vcntrycd := SUBSTR(vcntrycd_list,1,vpos-1);
DBMS_OUTPUT.PUT_LINE('Country Code : '||vcntrycd);
vcntrycd_list := SUBSTR(vcntrycd_list,vpos+1);
END LOOP;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM(SQLCODE));
rollback;
END;
/
No comments:
Post a Comment