473,725 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using xp_cmdshell in a trigger

I'm using xp_cmdshell to output a text file from a trigger like this

CREATE TRIGGER [CreateFile] ON tblApplications
FOR INSERT
AS

DECLARE @FirstName varchar(75)
DECLARE @LastName varchar(75)

Declare @strcmdshell varchar(150)
Declare @filename varchar(100)
Declare @filecontents varchar(150)

SELECT @FirstName = FirstName FROM INSERTED
SELECT @LastName = LastName FROM INSERTED
SELECT @filecontents = 'FirstName: ' + @FirstName+char (13)+'LastName: ' +
@LastName+char( 13)+
SELECT @filename = 'c:\test.txt'
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell

When I concatenate @FirstName and @LastName(neith er is NULL) and try to
insert a blank line with char(13) the command doesn't run. If I don't use
char(13) it works fine. Does this make sense? IS there any other way I can
format the output file.

Thanks




Jul 20 '05 #1
3 10846
You can't embed a carriage return on a command line so you'll need one ECHO
command per line. Also, you need to code your trigger to handle a multi-row
insert. For example:

CREATE TABLE tblApplications
(
FirstName varchar(75) NOT NULL,
LastName varchar(75) NOT NULL
)
GO

CREATE TRIGGER [CreateFile] ON tblApplications
FOR INSERT
AS
SET NOCOUNT ON
DECLARE @FirstName varchar(75)
DECLARE @LastName varchar(75)
DECLARE @strcmdshell varchar(150)
DECLARE @filename varchar(100)
DECLARE @filecontents varchar(150)
SELECT @filename = 'c:\test.txt'
DECLARE Inserted CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
SELECT FirstName, LastName FROM inserted
OPEN Inserted
WHILE 1 = 1
BEGIN
FETCH NEXT FROM Inserted
INTO @FirstName, @LastName
IF @@FETCH_STATUS = -1 BREAK
SELECT @strcmdshell =
'echo FirstName: ' + @FirstName + '>>' + @filename
EXEC master..xp_cmds hell @strcmdshell
SELECT @strcmdshell =
'echo LastName: ' + @LastName + '>>' + @filename
EXEC master..xp_cmds hell @strcmdshell
END
CLOSE Inserted
GO

INSERT INTO tblApplications
SELECT 'First1', 'Last1'
UNION
SELECT 'First2', 'Last2'
GO

Another consideration is concurrent inserts; only one process can write to a
file at a time. I don't know your business rules so I didn't include an
example here.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Terri" <Te***@spamaway .om> wrote in message
news:bq******** **@reader2.nmix .net...
I'm using xp_cmdshell to output a text file from a trigger like this

CREATE TRIGGER [CreateFile] ON tblApplications
FOR INSERT
AS

DECLARE @FirstName varchar(75)
DECLARE @LastName varchar(75)

Declare @strcmdshell varchar(150)
Declare @filename varchar(100)
Declare @filecontents varchar(150)

SELECT @FirstName = FirstName FROM INSERTED
SELECT @LastName = LastName FROM INSERTED
SELECT @filecontents = 'FirstName: ' + @FirstName+char (13)+'LastName: ' + @LastName+char( 13)+
SELECT @filename = 'c:\test.txt'
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell

When I concatenate @FirstName and @LastName(neith er is NULL) and try to
insert a blank line with char(13) the command doesn't run. If I don't use
char(13) it works fine. Does this make sense? IS there any other way I can
format the output file.

Thanks



Jul 20 '05 #2
Thanks Dan,
I won't have multi-row inserts so I don't believe I'll need the cursor so
this will work..
SELECT @strcmdshell = 'echo FirstName: ' + @FirstName + '>>' + @filename
EXEC master..xp_cmds hell @strcmdshell
SELECT @strcmdshell =
'echo LastName: ' + @LastName + '>>' + @filename
EXEC master..xp_cmds hell @strcmdshell

My next problem is an additional variable I have @Essay which is retrieved
from an html form which will always have carriage returns. I've tried
replacing the carriage returns with the newline character but it seems those
can not be embedded in a command line either.

Any thoughts? Is there some other method which would allow me to write a
file from a trigger where I won't run into these formatting issues.

Thanks...
"Dan Guzman" <da*******@nosp am-earthlink.net> wrote in message
news:pc******** ********@newsre ad2.news.atl.ea rthlink.net...
You can't embed a carriage return on a command line so you'll need one ECHO command per line. Also, you need to code your trigger to handle a multi-row insert. For example:

CREATE TABLE tblApplications
(
FirstName varchar(75) NOT NULL,
LastName varchar(75) NOT NULL
)
GO

CREATE TRIGGER [CreateFile] ON tblApplications
FOR INSERT
AS
SET NOCOUNT ON
DECLARE @FirstName varchar(75)
DECLARE @LastName varchar(75)
DECLARE @strcmdshell varchar(150)
DECLARE @filename varchar(100)
DECLARE @filecontents varchar(150)
SELECT @filename = 'c:\test.txt'
DECLARE Inserted CURSOR LOCAL FAST_FORWARD READ_ONLY FOR
SELECT FirstName, LastName FROM inserted
OPEN Inserted
WHILE 1 = 1
BEGIN
FETCH NEXT FROM Inserted
INTO @FirstName, @LastName
IF @@FETCH_STATUS = -1 BREAK
SELECT @strcmdshell =
'echo FirstName: ' + @FirstName + '>>' + @filename
EXEC master..xp_cmds hell @strcmdshell
SELECT @strcmdshell =
'echo LastName: ' + @LastName + '>>' + @filename
EXEC master..xp_cmds hell @strcmdshell
END
CLOSE Inserted
GO

INSERT INTO tblApplications
SELECT 'First1', 'Last1'
UNION
SELECT 'First2', 'Last2'
GO

Another consideration is concurrent inserts; only one process can write to a file at a time. I don't know your business rules so I didn't include an
example here.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Terri" <Te***@spamaway .om> wrote in message
news:bq******** **@reader2.nmix .net...
I'm using xp_cmdshell to output a text file from a trigger like this

CREATE TRIGGER [CreateFile] ON tblApplications
FOR INSERT
AS

DECLARE @FirstName varchar(75)
DECLARE @LastName varchar(75)

Declare @strcmdshell varchar(150)
Declare @filename varchar(100)
Declare @filecontents varchar(150)

SELECT @FirstName = FirstName FROM INSERTED
SELECT @LastName = LastName FROM INSERTED
SELECT @filecontents = 'FirstName: ' + @FirstName+char (13)+'LastName: '
+
@LastName+char( 13)+
SELECT @filename = 'c:\test.txt'
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell

When I concatenate @FirstName and @LastName(neith er is NULL) and try to
insert a blank line with char(13) the command doesn't run. If I don't

use char(13) it works fine. Does this make sense? IS there any other way I can format the output file.

Thanks




Jul 20 '05 #3
File output is probably best handled in a client application rather than in
a trigger. I suggest you insert data into a staging table and process data
from there. You can schedule an ActiveX scropt via SQL Server agent to
generate the output periodically. Example below:

CREATE TABLE tblApplications
(
FirstName varchar(75) NOT NULL,
LastName varchar(75) NOT NULL,
Essay varchar(75) NOT NULL,
)
GO

CREATE TABLE PendingOutputs
(
SequenceNumber int IDENTITY(1, 1) NOT NULL
CONSTRAINT PK_PendingOutpu ts PRIMARY KEY,
FirstName varchar(75) NOT NULL,
LastName varchar(75) NOT NULL,
Essay varchar(75) NOT NULL,
)
GO

CREATE TRIGGER [CreateFile] ON tblApplications
FOR INSERT
AS
SET NOCOUNT ON
INSERT INTO PendingOutputs
SELECT FirstName, LastName, Essay
FROM inserted
GO

'ActiveX script step (VBScript)
Set Connection = CreateObject("A DODB.Connection ")
Set RecordSet = CreateObject("A DODB.Recordset" )
Set FSO = CreateObject("S cripting.FileSy stemObject")
Set OutFile = FSO.OpenTextFil e("C:\Temp\MyFi le.txt", 8, True)
Recordset.Curso rLocation = 3
Connection.Open _
"Provider=SQLOL EDB;" & _
"Data Source=MyServer ;" & _
"Integrated Security=SSPI;" & _
"Initial Catalog=MyDatab ase"
Set Recordset.Activ eConnection = Connection
RecordSet.Open _
"SELECT " & _
" SequenceNumber, " & _
" FirstName," & _
" LastName," & _
" Essay" & _
" FROM PendingOutputs"
Do While RecordSet.EOF = False
OutFile.WriteLi ne "FirstName: " & RecordSet.Field s(1)
OutFile.WriteLi ne "LastName: " & RecordSet.Field s(2)
OutFile.WriteLi ne "Essay: " & RecordSet.Field s(3)
Connection.Exec ute "DELETE FROM PendingOutputs" & _
" WHERE SequenceNumber = " & RecordSet.Field s(0)
RecordSet.MoveN ext
Loop
OutFile.Close
RecordSet.Close
Connection.Clos e
Set Connection = Nothing
Set RecordSet = Nothing
Set File = Nothing
Set FSO = Nothing

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Terri" <Te***@spamaway .om> wrote in message
news:bq******** **@reader2.nmix .net...
Thanks Dan,
I won't have multi-row inserts so I don't believe I'll need the cursor so
this will work..
SELECT @strcmdshell = 'echo FirstName: ' + @FirstName + '>>' + @filename EXEC master..xp_cmds hell @strcmdshell
SELECT @strcmdshell =
'echo LastName: ' + @LastName + '>>' + @filename
EXEC master..xp_cmds hell @strcmdshell

My next problem is an additional variable I have @Essay which is retrieved
from an html form which will always have carriage returns. I've tried
replacing the carriage returns with the newline character but it seems those can not be embedded in a command line either.

Any thoughts? Is there some other method which would allow me to write a
file from a trigger where I won't run into these formatting issues.

Thanks...

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7665
by: Yvonne | last post by:
Would like to implement Blat on SQL Server 2000. I'm looking for example syntax for setting up BLAT with xp_cmdshell. TIA
1
33280
by: Dan Bart | last post by:
I need to execute xp_cmdshell in a trigger and pass a command line parameter to the .exe program i.e. I have a .exe program c:\program files\savedata.exe In the trigger I have a parameter @Id bigint I need to pass the parameter @Id to savedata.exe In the trigger, I am trying
4
8090
by: Joel Thornton | last post by:
Whenever something is inserted to a given table, I want to run some shell commands using xp_cmdshell. Would it be a bad idea to put this xp_cmdshell in the INSERT trigger of this table? I understand that when using xp_cmdshell, the sql thread in question waits until xp_cmdshell finishes what it's doing. Does this mean if my xp_cmdshell call takes 30 seconds, that nobody else can insert to this table until my xp_cmdshell and rest of the...
9
11231
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 only one column: txtOutput I want to use the DB front end (MS Access) to send the text string to the SQL backend, then have the SQL Server create a file to a path, such as F:/myfiledate.txt that holds the text in txtOutput, then the trigger...
4
7429
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)
2
2531
by: Lauren Quantrell | last post by:
I have created a view named viewOutput that shows one column in a table. I insert a row into the table and then I'm using this code to create a file with the text in the single row. This code works fine when I'm signed on with an account that has server admin rights, however it fails when I signin with an account that does not have server admin rights. I'm hoping someone can help me with this.
1
8348
by: lytung | last post by:
Hi all, i am having trouble trying to export this in xml. Basically inside an update trigger i need to export in xml or file the qty that has been changed. This then gets updated to another system. Is this a good idea ? should i be using another table in between in case something happens on the other end? However, I tried something like this and it didn't recognize the selected table inside the bcp:
2
4266
by: tolcis | last post by:
I have a trigger that should be execute on each row insert and only if appcode = 'I' and datasent = 0. It should execute a DTS package. The DTS package by itself runs about 6 seconds. Trigger was created successfuly. When I try to insert a row my db hangs. I can see that SPID on my db is hang by SPID from master db. It doesn't complete until I kill that SPID. Why is that? This is the trigger that I am trying to execute: CREATE...
6
5343
by: mcolson | last post by:
Is it possible to use sql to execute a batch file? I would like to execute the following "C:\BTW\bartend.exe /f=C:\BTW\Toolbox\Formats \carnum.btw /p", 6 Thanks, Matt
0
8874
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8748
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9393
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...
1
9164
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9092
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
6000
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4506
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3212
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

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.