1. create below log table to capture the error records details .
CREATE TABLE XX_BAD_DATA_LOG_TBL
(
FILE_NAME VARCHAR2(250 BYTE),
CREATION_DATE DATE,
CREATED_BY NUMBER,
ERROR_MESSAGE VARCHAR2(2000 BYTE),
FILE_DATA VARCHAR2(2000 BYTE)
);
2. Create the following function.
CREATE OR REPLACE FUNCTION apps.xx_upload_data_to_tbl (
p_table IN VARCHAR2
, p_dir IN VARCHAR2
, p_filename IN VARCHAR2
, p_delimiter IN VARCHAR2 DEFAULT ','
)
RETURN NUMBER
IS
l_utl_file UTL_FILE.file_type;
l_cursor INTEGER DEFAULT DBMS_SQL.open_cursor;
l_buffer VARCHAR2 (4000);
l_data VARCHAR2 (4000);
l_status INTEGER;
l_column_cnt NUMBER DEFAULT 0;
l_count NUMBER DEFAULT 0;
l_sep CHAR (1) DEFAULT NULL;
l_error_message VARCHAR2 (4000);
l_column_names VARCHAR2 (4000);
l_num NUMBER;
BEGIN
l_num := 10;
l_utl_file := UTL_FILE.fopen (p_dir
, p_filename
, 'r'
);
l_num := 20;
l_buffer := 'INSERT INTO ' || p_table || ' VALUES ( ';
l_num := 30;
FOR z IN (SELECT column_name
FROM dba_tab_columns
WHERE table_name = p_table)
LOOP
IF l_column_names IS NULL
THEN
l_column_names := z.column_name;
ELSE
l_column_names := l_column_names || ',' || z.column_name;
END IF;
END LOOP;
l_num := 40;
DBMS_OUTPUT.put_line (l_num || ' ' || l_column_names);
BEGIN
l_column_cnt := LENGTH (l_column_names) - LENGTH (REPLACE (l_column_names
, ','
, ''
)) + 1;
l_num := 45;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (l_num || ' Error ' || SQLERRM);
END;
DBMS_OUTPUT.put_line (l_column_cnt);
l_num := 50;
FOR i IN 1 .. l_column_cnt
LOOP
l_buffer := l_buffer || l_sep || ':b' || i;
l_sep := ',';
END LOOP;
l_num := 60;
l_buffer := l_buffer || ')';
l_num := 70;
DBMS_SQL.parse (l_cursor
, l_buffer
, DBMS_SQL.native
);
l_num := 80;
LOOP
BEGIN
UTL_FILE.get_line (l_utl_file, l_data);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
EXIT;
END;
l_num := 90;
l_buffer := l_data || p_delimiter;
l_num := 100;
FOR i IN 1 .. l_column_cnt
LOOP
DBMS_SQL.bind_variable (l_cursor
, ':b' || i
, SUBSTR (l_buffer
, 1
, INSTR (l_buffer, p_delimiter) - 1
)
);
l_buffer := SUBSTR (l_buffer, INSTR (l_buffer, p_delimiter) + 1);
l_num := 110;
END LOOP;
BEGIN
l_status := DBMS_SQL.EXECUTE (l_cursor);
l_count := l_count + 1;
l_num := 120;
EXCEPTION
WHEN OTHERS
THEN
l_error_message := SQLERRM;
INSERT INTO xx_bad_data_log_tbl
VALUES (p_filename
, SYSDATE
, fnd_global.user_id
, l_error_message
, l_data
);
END;
END LOOP;
DBMS_SQL.close_cursor (l_cursor);
UTL_FILE.fclose (l_utl_file);
l_num := 130;
COMMIT;
DBMS_OUTPUT.put_line (l_num || ' ' || l_count);
RETURN l_count;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (l_num || ' Unexpected Error ' || SUBSTR(SQLERRM,1,250));
RETURN 0;
END xx_upload_data_to_tbl;
/
CREATE TABLE XX_BAD_DATA_LOG_TBL
(
FILE_NAME VARCHAR2(250 BYTE),
CREATION_DATE DATE,
CREATED_BY NUMBER,
ERROR_MESSAGE VARCHAR2(2000 BYTE),
FILE_DATA VARCHAR2(2000 BYTE)
);
2. Create the following function.
CREATE OR REPLACE FUNCTION apps.xx_upload_data_to_tbl (
p_table IN VARCHAR2
, p_dir IN VARCHAR2
, p_filename IN VARCHAR2
, p_delimiter IN VARCHAR2 DEFAULT ','
)
RETURN NUMBER
IS
l_utl_file UTL_FILE.file_type;
l_cursor INTEGER DEFAULT DBMS_SQL.open_cursor;
l_buffer VARCHAR2 (4000);
l_data VARCHAR2 (4000);
l_status INTEGER;
l_column_cnt NUMBER DEFAULT 0;
l_count NUMBER DEFAULT 0;
l_sep CHAR (1) DEFAULT NULL;
l_error_message VARCHAR2 (4000);
l_column_names VARCHAR2 (4000);
l_num NUMBER;
BEGIN
l_num := 10;
l_utl_file := UTL_FILE.fopen (p_dir
, p_filename
, 'r'
);
l_num := 20;
l_buffer := 'INSERT INTO ' || p_table || ' VALUES ( ';
l_num := 30;
FOR z IN (SELECT column_name
FROM dba_tab_columns
WHERE table_name = p_table)
LOOP
IF l_column_names IS NULL
THEN
l_column_names := z.column_name;
ELSE
l_column_names := l_column_names || ',' || z.column_name;
END IF;
END LOOP;
l_num := 40;
DBMS_OUTPUT.put_line (l_num || ' ' || l_column_names);
BEGIN
l_column_cnt := LENGTH (l_column_names) - LENGTH (REPLACE (l_column_names
, ','
, ''
)) + 1;
l_num := 45;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (l_num || ' Error ' || SQLERRM);
END;
DBMS_OUTPUT.put_line (l_column_cnt);
l_num := 50;
FOR i IN 1 .. l_column_cnt
LOOP
l_buffer := l_buffer || l_sep || ':b' || i;
l_sep := ',';
END LOOP;
l_num := 60;
l_buffer := l_buffer || ')';
l_num := 70;
DBMS_SQL.parse (l_cursor
, l_buffer
, DBMS_SQL.native
);
l_num := 80;
LOOP
BEGIN
UTL_FILE.get_line (l_utl_file, l_data);
EXCEPTION
WHEN NO_DATA_FOUND
THEN
EXIT;
END;
l_num := 90;
l_buffer := l_data || p_delimiter;
l_num := 100;
FOR i IN 1 .. l_column_cnt
LOOP
DBMS_SQL.bind_variable (l_cursor
, ':b' || i
, SUBSTR (l_buffer
, 1
, INSTR (l_buffer, p_delimiter) - 1
)
);
l_buffer := SUBSTR (l_buffer, INSTR (l_buffer, p_delimiter) + 1);
l_num := 110;
END LOOP;
BEGIN
l_status := DBMS_SQL.EXECUTE (l_cursor);
l_count := l_count + 1;
l_num := 120;
EXCEPTION
WHEN OTHERS
THEN
l_error_message := SQLERRM;
INSERT INTO xx_bad_data_log_tbl
VALUES (p_filename
, SYSDATE
, fnd_global.user_id
, l_error_message
, l_data
);
END;
END LOOP;
DBMS_SQL.close_cursor (l_cursor);
UTL_FILE.fclose (l_utl_file);
l_num := 130;
COMMIT;
DBMS_OUTPUT.put_line (l_num || ' ' || l_count);
RETURN l_count;
EXCEPTION
WHEN OTHERS
THEN
DBMS_OUTPUT.put_line (l_num || ' Unexpected Error ' || SUBSTR(SQLERRM,1,250));
RETURN 0;
END xx_upload_data_to_tbl;
/
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.