12 Jan 2017

Move Data From Data Blocks To CSV File.

Before Running This You must be install WEBUTIL Process.
-- CREATE DIRECTORY
CREATE OR REPLACE DIRECTORY SIGN_IMG
  AS '\\<DATABSE OR APPLICATION SERVER IP>\<DIRECTORY NAME ON OS>\';
-- GRANTING PRIVILEGES ON DIRECTORY
GRANT EXECUTE, READ, WRITE ON DIRECTORY SYS.SIGN_IMG TO <USER NAME> WITH GRANT OPTION;
-- CREATING FORM
Create the Form With Dept and Emp Table as Master Detail. Create one Push Button And Apply Following Code On WHEN-BUTTON-PRESSED Trigger.

IF :Dept.Deptno IS NOT NULL THEN
      SUMMARY_TO_CSV;
END IF
;

--CREATING PROCEDURE IN PROGRAM UNIT OF FORM BUILDER
PROCEDURE SUMMARY_TO_CSV IS
-- FILE HANDLER
LV_FILE       CLIENT_TEXT_IO.FILE_TYPE;
V_QOT         VARCHAR2(01):='''';
--FILE NAME
LV_FILE_NAME  VARCHAR2(1000);
LV_DIR_PATH   VARCHAR2(1000);
L_CTR         NUMBER (05);
CTR           NUMBER (05);
-- TIMER VARIABLE
LV_WAIT       TIMER;
-- RUNTIME PROCEDURE...
PROCEDURE GET_DIRECTORY_NAME(P_DIR_NAME OUT VARCHAR2) IS
  L_TEMP_TEXT  VARCHAR2(8000);
BEGIN
   SELECT DIRECTORY_PATH INTO L_TEMP_TEXT FROM SYS.DBA_DIRECTORIES
   WHERE DIRECTORY_NAME='SIGN_IMG';
   P_DIR_NAME :=L_TEMP_TEXT;
EXCEPTION
  WHEN OTHERS THEN
  P_DIR_NAME:=NULL;
END GET_DIRECTORY_NAME;
BEGIN
-- FILE PATH
GET_DIRECTORY_NAME(LV_DIR_PATH);
IF LV_DIR_PATH IS NULL THEN
  RAISE FORM_TRIGGER_FAILURE;
END IF;

LV_FILE_NAME:= LV_DIR_PATH||'DEPT_EMPLOYEE_'||TO_CHAR(SYSDATE,'DDMMRRRR')||'.CSV';

--OPEN FILE IN WRITE MODE
LV_FILE :=  CLIENT_TEXT_IO.FOPEN(LV_FILE_NAME,'W');

--SWITCHING MASTER BLOCK
GO_BLOCK('DEPT');
-- WRITE HEADER COLUMNS
CLIENT_TEXT_IO.PUT_LINE(LV_FILE,'DEPT CODE,'||:DEPT.DEPTNO||','||:DEPT.DNAME||','||:DEPT.LOC);
CLIENT_TEXT_IO.PUT_LINE(LV_FILE,'AS ON DATE,'||TO_CHAR(SYSDATE,'DD-MON-RRRR'));
CLIENT_TEXT_IO.PUT_LINE(LV_FILE,'EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO');
--SWITCHING DETAIL BLOCK
GO_BLOCK('EMP');  LAST_RECORD;  L_CTR :=:SYSTEM.CURSOR_RECORD; FIRST_RECORD;
--WRITING ROWS INTO THE FILE...
FOR I IN 1..L_CTR LOOP
  CLIENT_TEXT_IO.PUT_LINE(LV_FILE,:EMPNO||','||NVL(:ENAME,'NA')||','||NVL(:JOB,'NA')||','||NVL(:MGR,7839)||','||NVL(:HIREDATE,SYSDATE)||','||NVL(:SAL,0)||','||NVL(:COMM,0)||','||:DEPTNO);
  NEXT_RECORD;
END LOOP;
--CLOSE FILE
CLIENT_TEXT_IO.FCLOSE(LV_FILE);
MESSAGE('FILE :  '||'DEPT_EMPLOYEE_'||TO_CHAR(SYSDATE,'DDMMRRRR')||'.CSV'||' CREATED SUCCESSFULLY...');
END;


You may be used Set_Alert_Property Built-in to display the message instead of Message Built-in. 

No comments:

Post a Comment