473,395 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

SQL Trigger to output to a text file

3
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 17895
iburyak
1,017 Expert 512MB
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(CustID 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_result' + replace(replace(convert(varchar(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_cmdshell @bcpCommand
-- delete everything you already saved into a file.
Delete * from CustomerInfo
END
Dec 14 '06 #2
pr33tz
3
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 Expert 512MB
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_result' + replace(replace(convert(varchar(20), getdate()),' ','_'),':','-') + '.csv'

select @FileName

SET @bcpCommand = 'bcp "select char(34)+string_column+char(34)+char(44)+char(34)+ convert(varchar(10),Int_or_date_column) + char(34) from database_name..CustomerInfo " queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -U UID_here -P PWD_here -c'

EXEC master..xp_cmdshell @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
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...
2
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...
8
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...
4
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...
1
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...
4
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...
4
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...
7
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...
8
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...
6
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.