473,499 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A trigger to output to a text file

1 New Member
Hi,

I've been using Oracle 9i now and want to know if the following is possible. I've got Customer table. I want to create a trigger that, when a new customer is added or update, will output the changes to a text file. For example, if Mr Smith's address is created, it will pick up his name from the Customer table and output the results to a text file in a '0001, Mr Smith, 12, format or similar.

Any examples or guides would be very helpful!!

Thanks in advance.
Jan 22 '08 #1
3 12192
debasisdas
8,127 Recognized Expert Expert
Try to use the UTL_FILE package for the purpose.
Jan 22 '08 #2
amitpatel66
2,367 Recognized Expert Top Contributor
Hi,

I've been using Oracle 9i now and want to know if the following is possible. I've got Customer table. I want to create a trigger that, when a new customer is added or update, will output the changes to a text file. For example, if Mr Smith's address is created, it will pick up his name from the Customer table and output the results to a text file in a '0001, Mr Smith, 12, format or similar.

Any examples or guides would be very helpful!!

Thanks in advance.
You can use UTL_FILE as suggested in the above post by either implementing a UTL_FILE operation in the trigger itself or create a seperate procedure and call that procedure from the trigger.
Jan 22 '08 #3
subashsavji
93 New Member
may this coding will be helpful to you. modify it and write under trigger

Expand|Select|Wrap|Line Numbers
  1.  
  2. CREATE OR REPLACE PROCEDURE sal_status
  3. (p_filedir IN VARCHAR2,  p_filename IN VARCHAR2)
  4. IS
  5.  v_filehandle UTL_FILE.FILE_TYPE;
  6.  CURSOR emp_info IS
  7.    SELECT ename, sal, deptno
  8.     FROM emp
  9.     ORDER BY deptno;
  10.  v_newdeptno emp.deptno%TYPE;
  11.  v_olddeptno emp.deptno%TYPE := 0;
  12. BEGIN
  13.  v_filehandle := UTL_FILE.FOPEN (p_filedir, p_filename,'w');
  14.  UTL_FILE.PUTF (v_filehandle,'SALARY REPORT: GENERATED ON
  15.                              %s\n', SYSDATE);
  16.  UTL_FILE.NEW_LINE (v_filehandle);
  17.  FOR v_emp_rec IN emp_info LOOP
  18.   v_newdeptno := v_emp_rec.deptno;
  19. IF v_newdeptno <> v_olddeptno THEN
  20.       UTL_FILE.PUTF (v_filehandle, 'DEPARTMENT: %s\n',
  21.                      v_emp_rec.deptno);
  22.     END IF;
  23.     UTL_FILE.PUTF (v_filehandle,'  EMPLOYEE: %s earns: %s\n',
  24.                    v_emp_rec.ename, v_emp_rec.sal);
  25.     v_olddeptno := v_newdeptno;
  26.   END LOOP;
  27.   UTL_FILE.PUT_LINE (v_filehandle, '*** END OF REPORT ***');
  28.   UTL_FILE.FCLOSE (v_filehandle);
  29. EXCEPTION
  30.   WHEN UTL_FILE.INVALID_FILEHANDLE THEN
  31.     RAISE_APPLICATION_ERROR (-20001, 'Invalid File.');
  32.   WHEN UTL_FILE.WRITE_ERROR THEN
  33.    RAISE_APPLICATION_ERROR (-20002, 'Unable to write to
  34.                                      file');
  35. END sal_status;
  36. /
  37.  
  38. create OR REPLACE DIRECTORY emp_dir as 'c:\';
  39. /
  40. execute sal_status('EMP_DIR','YY.TXT'); 
  41. /
  42. execute sal_status('EMP_DIR','C:\YYY.DOC');  -- ONLY CREATES UNDER THE C:\ DRIVE
  43.                                                                                                                                                                                                                              '
  44.  
Feb 4 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
10819
by: Terri | last post by:
I'm using xp_cmdshell to output a text file from a trigger like this CREATE TRIGGER ON tblApplications FOR INSERT AS DECLARE @FirstName varchar(75) DECLARE @LastName varchar(75) Declare...
9
11205
by: Lauren Quantrell | last post by:
Is there a way to create a text file (such as a Windows Notepad file) by using a trigger on a table? What I want to do is to send a row of information to a table where the table: tblFileData has...
4
7414
by: Lauren Quantrell | last post by:
I have created the following trigger: CREATE TRIGGER ON OutputTable FOR INSERT AS Declare @filename nvarchar(35) Declare @filecontents nvarchar(2000) Declare @strcmdshell varchar(150)
3
17906
by: pr33tz | last post by:
Hi, I've been using SQL Server 2000 for a short while now and want to know if the following is possible. I've got two tables, Customer table and an Address table. I want to create a trigger that,...
2
1609
by: Quentin | last post by:
Any code on how to start write to a new file would be appreciated. In the example below: How would I start the write to a file on the word "Recipe" and then write every line beneath it until "End...
6
6780
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue , and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get...
0
2606
by: wugon.net | last post by:
Hi , Anyone know how to monitor db2 trigger activity ? We suffer some trigger issue today and we try to monitor trigger's behavior use event monitor and db2audit, but both tools can not get...
1
2548
by: bwestover | last post by:
I am trying to pull data out of an application database and transform it to another medium. I have direct access to the database, but I cannot alter the program code. What I want to have happen...
9
3199
by: lenygold via DBMonster.com | last post by:
I have the following trigger: --#SET TERMINATOR ! CREATE TRIGGER CROSS_REFF_TRIG AFTER INSERT ON NEW_CATALOG REFERENCING NEW AS nnn FOR EACH ROW MODE DB2SQL BEGIN ATOMIC DECLARE reason...
0
7134
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7012
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7392
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5479
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3105
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3101
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1429
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
667
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
307
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.