Temat: Eskport danych do plików zewnętrznych
kiedys zrobilem to w ten sposob, moze sie komus przyda
--katalog na oracle'u gdzie chcemy zrzucac
CREATE OR REPLACE DIRECTORY SAP_DIR AS 'e:\sap'
/
--funkcja do zrzucania zapytan do pliczkow tekstowych , separator kolumn do ustawienia, funkcja z asktom.oracle.com
CREATE OR REPLACE FUNCTION dump_csv (P_QUERY IN VARCHAR2,
P_SEPARATOR IN VARCHAR2 DEFAULT ' ',
P_DIR IN VARCHAR2, P_FILENAME IN VARCHAR2)
RETURN NUMBER
IS
L_OUTPUT UTL_FILE.FILE_TYPE;
L_THECURSOR INTEGER DEFAULT DBMS_SQL.OPEN_CURSOR;
L_COLUMNVALUE VARCHAR2 (3000);
L_STATUS INTEGER;
L_COLCNT NUMBER DEFAULT 0;
L_SEPARATOR VARCHAR2 (10) DEFAULT ' ';
L_CNT NUMBER DEFAULT 0;
BEGIN
L_OUTPUT := UTL_FILE.FOPEN (P_DIR, P_FILENAME, 'w');
DBMS_SQL.PARSE (L_THECURSOR, P_QUERY, DBMS_SQL.NATIVE);
FOR I IN 1 .. 255
LOOP
BEGIN
DBMS_SQL.DEFINE_COLUMN (L_THECURSOR, I, L_COLUMNVALUE, 3000);
L_COLCNT := I;
EXCEPTION
WHEN OTHERS
THEN
IF (SQLCODE = -1007)
THEN
EXIT;
ELSE
RAISE;
END IF;
END;
END LOOP;
DBMS_SQL.DEFINE_COLUMN (L_THECURSOR, 1, L_COLUMNVALUE, 3000);
L_STATUS := DBMS_SQL.EXECUTE (L_THECURSOR);
LOOP
EXIT WHEN (DBMS_SQL.FETCH_ROWS (L_THECURSOR) <= 0);
L_SEPARATOR := ' ';
FOR I IN 1 .. L_COLCNT
LOOP
DBMS_SQL.COLUMN_VALUE (L_THECURSOR, I, L_COLUMNVALUE);
UTL_FILE.PUT (L_OUTPUT, L_COLUMNVALUE || L_SEPARATOR );
L_SEPARATOR := P_SEPARATOR;
END LOOP;
UTL_FILE.NEW_LINE (L_OUTPUT);
L_CNT := L_CNT + 1;
END LOOP;
DBMS_SQL.CLOSE_CURSOR (L_THECURSOR);
UTL_FILE.FCLOSE (L_OUTPUT);
RETURN L_CNT;
END DUMP_CSV;
/
-- procedurka do eksportowania
CREATE OR REPLACE PROCEDURE exp_proc
AS
l_rows NUMBER;
BEGIN
select
dump_csv ('select * from customers_exp_vw', ' ', 'SAP_DIR',
'customers.txt') into l_rows from dual;
END;
/
-- potem tworzysz joba i gotowe :)
DECLARE
x NUMBER;
BEGIN
SYS.DBMS_JOB.SUBMIT (x, 'begin exp_proc; end;',
TO_DATE ('04-08-2010 23:00:00', 'dd/mm/yyyy hh24:mi:ss'), 'SYSDATE+1', FALSE);
END;
/
Milego uzytkowania,
pozdrawiam
Robert