473,624 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL Trigger to output to a text file

3 New Member
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, when a new customer is added or amended, 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 his address from the Address table (linked by Cutomer_ID) and output the results to a text file in a '0001, Mr Smith, 12, Temple Road, London, NW2 4ET, 02084521234' format or similar.

I think this is possible for a single table but not sure about multiple tables. Also, any examples or guides would be very helpful!!

Thanks in advance.
Dec 14 '06 #1
3 17925
iburyak
1,017 Recognized Expert Top Contributor
Unfortunately it is not a solution it is jut a food to chew on and find your own way to do it.

--1 Create tables.
Create table Customer(CustID int, CustName varchar(50))
Create table Address(CustID int, Address varchar(4000))

--2. Insert Customer data
insert into Customer values (1, 'pr33tz')

--3. Create ouput table
Create table CustomerInfo(Cu stID int, CustName varchar(50), CustAddr varchar(4000))

--4. Create a trigger

CREATE TRIGGER tI_Address ON Address FOR INSERT AS
BEGIN

Insert into CustomerInfo
Select i.CustID, c.CustName, i.Address
From inserted i
join Customer c on i.CustID = c.CustID

END


--5. Try to insert data into Address table and see results.

insert into Address values(1, 'Some address')

select * from CustomerInfo

--6. Insert data into a text file in a separate scheduled job. You can run it every 5 min if you wish.
-- I tried to do it in a trigger but process that does insert into a table blocks Command line from accessing
-- the same table from a different process ID which is the case when you execute xp_cmdshell

BEGIN
DECLARE @FileName varchar(50),
@bcpCommand varchar(2000)

SET @FileName = 'c:\query_resul t' + replace(replace (convert(varcha r(20), getdate()),' ','_'),':','-') + '.txt'

select @FileName

SET @bcpCommand = 'bcp "select * from database_name.. CustomerInfo " queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -U UID_here -P PWD_here -c'

EXEC master..xp_cmds hell @bcpCommand
-- delete everything you already saved into a file.
Delete * from CustomerInfo
END
Dec 14 '06 #2
pr33tz
3 New Member
Thanks, that has partly worked. Is it possible to have the text file display the result in a csv format? i.e., "cust id","address"," tel_no"
Dec 18 '06 #3
iburyak
1,017 Recognized Expert Top Contributor
Basically I am trying to forge csv file here by adding quotes and commas. Try if it works for you.

[PHP]BEGIN
DECLARE @FileName varchar(50),
@bcpCommand varchar(2000)

SET @FileName = 'c:\query_resul t' + replace(replace (convert(varcha r(20), getdate()),' ','_'),':','-') + '.csv'

select @FileName

SET @bcpCommand = 'bcp "select char(34)+string _column+char(34 )+char(44)+char (34)+convert(va rchar(10),Int_o r_date_column) + char(34) from database_name.. CustomerInfo " queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -U UID_here -P PWD_here -c'

EXEC master..xp_cmds hell @bcpCommand
-- delete everything you already saved into a file.
Delete * from CustomerInfo
END[/PHP]
Dec 19 '06 #4

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

Similar topics

2
5515
by: David Thomas | last post by:
Hi there, I am trying to store data in a text file and output it to the browser using PHP. All very easy - if I was using English! the problem is, I want to use Japanese and I'm finding it a tad difficult to get PHP to understand. I have two files, a text file containing data to display and a php file with a script to display the data. I can get the contents of a text file displayed in English, no problemo, but in Japanese, it's another...
2
3019
by: Oleg Ogurok | last post by:
Hi all, On my web page, I have a <OBJECT DATA="mydatafile.ica" ...> which loads Citrix ICA web client. The problem is that I want to generate the mydatafile.ica on the fly (e.g. based on session variables) and pass it to the user. So I was thinking of doing it this way: <OBJECT DATA="datafilegen.asp" ...>
8
2585
by: Scott | last post by:
I wish to create a text document (to the users desktop on any machine) which outputs to a standard text file with information obtained from there use of my program... Some of the text will be fixed and some will be variables generated in VB.net. For example... You have chosen a SINGLE TRIP, which is within EUROPE and is for FAMILY and last for 27 days. The trips starts from 24/04/05 until 30/04/05 and you chose to use more than 17 days of...
4
5732
by: gualtmacchi | last post by:
I'm processing an XML input file getting a plain text file where from M nodes I got N output lines... It's not relevant but the input file is a recordset coming from a database and the output is an EDI message set. Now I need to put into the output file also some information regarding just the output file itself and not the input one. And it cannot be expressed in terms of input information. For example I need to determine and write how...
1
4748
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a Visual Basic Script file, call it "Move and Reformat Text File.VBS," that will run from a Windows Script Host command-prompt-based version as follows: C:\> Cscript.exe "Move and Reformat Text File.VBS" The objective of the VBScript file, "Move...
4
6434
by: Max Vit | last post by:
Here is my problem: I have an application built in Access that outputs sensitive data to a text file. I would like to encrypt this data *whilst* the file is being outputted. The encryption I was using before (very weak) was to encrypt the file *after* the output had been completed but if for some reason the output did halt before being completed; then the text file was able to be read. ....and the encryption needs to be able to be...
4
3522
Sheepman
by: Sheepman | last post by:
My quest, to put 100 random numbers in ten rows of ten. Then output the same to the screen and a file. My screen output is working,yeah! Text file, not so much. The data is getting there but not formated. If word wrap is off it's a single row. If word wraps on, well ... it wraps. I've considered sizing the notebook window to wrap at the tenth character but I don't think the TA working on his Phd will buy it. Help please! My code for this portion...
7
2784
by: dlbuller | last post by:
Hi, I apologize if this question has been asked already. I have searched with no luck. I am trying to output a specifically formatted text file and then change the extension to another program type: outputting .txt changing to .dva (DVDAuthorGUI cue file). The .txt output format looks the same as a DVDAuthorGUI created file, but when I try to open the file in DVDAuthroGUI, I get an END OF FILE Error 62. I must be missing something in my...
8
3360
by: jyaseen | last post by:
I used the follwing code to download the text file from my server location. my php file to read the following code name is contacts.php it is downloading the text file but , this text file includes the script of contacts.php too $select_group = $_REQUEST; /*echo "file name ". $select_file = $_FILES;*/ if($select_group == 1){ $qry_contacts = "select eml_id from tbl_contacts "; }else{
6
2953
by: mabrynda | last post by:
Dear Experts, I have the following problem. I'm generating output txt files from access (an access 2002 table is converted into deleimited text with tabs including field names). For that I have a small function that I call from the main VBA script, which includes the so-called Export Specification. Everything works fine as long as the name of the output file is written in characters in the file specification of the DoCmd.TransferText command. ...
0
8677
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8620
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2605
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 we have to send another system
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.