473,786 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Insert Trigger and xp_cmdshell with carriage return text

I have created the following trigger:
CREATE TRIGGER [CreateFile] ON OutputTable
FOR INSERT
AS

Declare @filename nvarchar(35)
Declare @filecontents nvarchar(2000)
Declare @strcmdshell varchar(150)
SELECT @filecontents = OutputText FROM INSERTED
SELECT @filename = 'c:\' + OutputFileName FROM INSERTED
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell

It works fine as long as the column OutputText has no carriage
returns.
I have used my Access2K front end to dump about ten lines of text into
OutputText, each line broken by vbcrlf so that OutputText looks like:
line1test
line2text
line3text
etc.
The trigger won't fire with this text.
Is there something I can do to remedy this?
lq
Jul 20 '05 #1
4 7432
Lauren Quantrell (la************ *@hotmail.com) writes:
I have created the following trigger:

CREATE TRIGGER [CreateFile] ON OutputTable
FOR INSERT
AS

Declare @filename nvarchar(35)
Declare @filecontents nvarchar(2000)
Declare @strcmdshell varchar(150)
SELECT @filecontents = OutputText FROM INSERTED
SELECT @filename = 'c:\' + OutputFileName FROM INSERTED
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell
Didn't I tell you that must handle multi-row inserts? "I don't
have to", you said in an earilier posting. Well, someone changes
the application, and...
It works fine as long as the column OutputText has no carriage
returns.
I have used my Access2K front end to dump about ten lines of text into
OutputText, each line broken by vbcrlf so that OutputText looks like:
line1test
line2text
line3text
etc.
The trigger won't fire with this text.


Of course the trigger fires no matter the data. But what you think
is going to happen with this command batch:

ECHO line1test > c:\filenmane
line2text
line3text

You are only writing line1test to c:\filename - and only if you are
lucky. (Would line1test include a DOS meta-character, something else
will happen.)

I don't know if you can write multi-row strings with ECHO, but I don't
think so. In that case, you would have to break the strings into lines
and write each line with xp_cmdshell. You could set up a cursor over
iter_charlist_t o_table(*) , and call xp_cmdshell for each line, but
it's not good for performance.

(*) http://www.sommarskog.se/arrays-in-s...ist-of-strings

You are still sure that you don't want to run a job from Agent that
reads the table and writes the files?

--
Erland Sommarskog, SQL Server MVP, es****@sommarsk og.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
Erland,
The front end app controls every aspect of the output and the
destination table is created for the sole purpose of handling this
output. The front end creates a specific string that is inserted into
the OutputText column so there's no chance of a stray DOS
meta-character popping in (the string is created entirely from columns
from static lookup tables based on user values.
I know I have to deal with multiple inserts for future development but
I have a sever time issue to roll this out and I constructed this on
the fly...
The front end app only inserts one row at this time.
I have to figure out how to deal with the carriage returns though...
thanks,
lq

ps(when I get time I'll convert this to run from Agent...)

Erland Sommarskog <es****@sommars kog.se> wrote in message news:<Xn******* *************** @127.0.0.1>...
Lauren Quantrell (la************ *@hotmail.com) writes:
I have created the following trigger:

CREATE TRIGGER [CreateFile] ON OutputTable
FOR INSERT
AS

Declare @filename nvarchar(35)
Declare @filecontents nvarchar(2000)
Declare @strcmdshell varchar(150)
SELECT @filecontents = OutputText FROM INSERTED
SELECT @filename = 'c:\' + OutputFileName FROM INSERTED
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell


Didn't I tell you that must handle multi-row inserts? "I don't
have to", you said in an earilier posting. Well, someone changes
the application, and...
It works fine as long as the column OutputText has no carriage
returns.
I have used my Access2K front end to dump about ten lines of text into
OutputText, each line broken by vbcrlf so that OutputText looks like:
line1test
line2text
line3text
etc.
The trigger won't fire with this text.


Of course the trigger fires no matter the data. But what you think
is going to happen with this command batch:

ECHO line1test > c:\filenmane
line2text
line3text

You are only writing line1test to c:\filename - and only if you are
lucky. (Would line1test include a DOS meta-character, something else
will happen.)

I don't know if you can write multi-row strings with ECHO, but I don't
think so. In that case, you would have to break the strings into lines
and write each line with xp_cmdshell. You could set up a cursor over
iter_charlist_t o_table(*) , and call xp_cmdshell for each line, but
it's not good for performance.

(*) http://www.sommarskog.se/arrays-in-s...ist-of-strings

You are still sure that you don't want to run a job from Agent that
reads the table and writes the files?

Jul 20 '05 #3
I have come up with this using a stored procedure instead of a
trigger. An SP fires and stores the text in a table with five columns.
The SP below extracts the text into a file.

The only problem is, I need to create a file that only contains text
from one of the columns, a column names OutputText.
DO you know how I can do this?
Alter PROCEDURE "usp_ExportData "
@FileName varchar(100)
AS
SET NOCOUNT ON
DECLARE @ReturnCode int
DECLARE @ExportCommand varchar(255)

SET @ExportCommand =
'BCP myDatabaseName. .myTableName out "C:\' +

@FileName +
'" -T -c -S ' + @@SERVERNAME
EXEC @ReturnCode = master..xp_cmds hell @ExportCommand
RETURN(@ReturnC ode)
/* GO */

DECLARE @ReturnCodeX int
EXEC @ReturnCodeX = usp_ExportData 'MyFile.txt'
PRINT @ReturnCodeX
"Mischa Sandberg" <mi************ *@telus.net> wrote in message news:<KQxLc.816 24$Rf.10988@edt nps84>...
Okay, the cmd shell is interpreting the crlf as end-of-command for each of
the echo commands.
Here's the crude solution:

SELECT @strcmdshell = 'copy NUL '+@filename+'
echo '+replace(@file contents, char(10), ' >>'+@filename+c har(10)+'echo ')+'
'+@filenana me


Print the resulting string and you'll see what the multiline command does.

"Lauren Quantrell" <la************ *@hotmail.com> wrote in message
news:47******** *************** ***@posting.goo gle.com...
I have created the following trigger:
CREATE TRIGGER [CreateFile] ON OutputTable
FOR INSERT
AS

Declare @filename nvarchar(35)
Declare @filecontents nvarchar(2000)
Declare @strcmdshell varchar(150)
SELECT @filecontents = OutputText FROM INSERTED
SELECT @filename = 'c:\' + OutputFileName FROM INSERTED
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell

It works fine as long as the column OutputText has no carriage
returns.
I have used my Access2K front end to dump about ten lines of text into
OutputText, each line broken by vbcrlf so that OutputText looks like:
line1test
line2text
line3text
etc.
The trigger won't fire with this text.
Is there something I can do to remedy this?
lq

Jul 20 '05 #4
My bad, I didn't test what I posted. And the way it appears here, the line
breaks
may have gotten a bit munged.

Try using '&' as a command separator instead of char(10); i.e:

SELECT @strcmdshell = 'copy NUL '+@filepath
+ ' & echo'
+ replace(@fileco ntents, char(10), ' >>'+@filepath +' & echo ')+'
'+@filepath
"Lauren Quantrell" <la************ *@hotmail.com> wrote in message
news:47******** *************** **@posting.goog le.com...
Mischa,
Thanks for your response.
The template is now created with the proper filename but it is blank
(no text.)
I'm using this, modified from your example:
CREATE TRIGGER CreateTemplate ON tblEventsTempla tes
FOR INSERT
AS

Declare @filename nvarchar(35)
Declare @filepath nvarchar(45)
Declare @filecontents nvarchar(2000)
Declare @strcmdshell varchar(150)
SELECT @filecontents = TemplateText FROM INSERTED
SELECT @filename = TemplateFileNam e FROM INSERTED
SELECT @filepath = 'c:\' + @filename
SELECT @strcmdshell = 'copy NUL '+@filepath+' echo
'+replace(@file contents, char(10), ' >>'+@filepath+c har(10)+'echo ')+''+@filepath exec master..xp_cmds hell @strcmdshell


"Mischa Sandberg" <mi************ *@telus.net> wrote in message

news:<KQxLc.816 24$Rf.10988@edt nps84>...
Okay, the cmd shell is interpreting the crlf as end-of-command for each of the echo commands.
Here's the crude solution:

SELECT @strcmdshell = 'copy NUL '+@filename+'
echo '+replace(@file contents, char(10), ' >>'+@filename+c har(10)+'echo ')+'
>'+@filenana me


Print the resulting string and you'll see what the multiline command

does.
"Lauren Quantrell" <la************ *@hotmail.com> wrote in message
news:47******** *************** ***@posting.goo gle.com...
I have created the following trigger:
CREATE TRIGGER [CreateFile] ON OutputTable
FOR INSERT
AS

Declare @filename nvarchar(35)
Declare @filecontents nvarchar(2000)
Declare @strcmdshell varchar(150)
SELECT @filecontents = OutputText FROM INSERTED
SELECT @filename = 'c:\' + OutputFileName FROM INSERTED
SELECT @strcmdshell = 'echo '+ @filecontents+ ' >'+ @filename
exec master..xp_cmds hell @strcmdshell

It works fine as long as the column OutputText has no carriage
returns.
I have used my Access2K front end to dump about ten lines of text into
OutputText, each line broken by vbcrlf so that OutputText looks like:
line1test
line2text
line3text
etc.
The trigger won't fire with this text.
Is there something I can do to remedy this?
lq

Jul 20 '05 #5

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

Similar topics

3
7673
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
4
8095
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
11234
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...
1
8471
by: Paradigm | last post by:
I want to insert a carriage return into a text field. I am using insert into and /n for newline. The MYSQL database is accessed by a MS Access front end and the text field appears with a small square where the new line should be. It appears that the /n is interpreted in Access differently. Is there a way of putting a carriage return into the field so that it is seen as such by Accesss? Alex
1
2975
by: gchavez | last post by:
I'm running MSSQL 2K on Win 2k and just upgraded SQL Server from SP3 to SP4 (that's when my problem started). I have a procedure that bulk inserts from a text file that is located on a Netware 4.11 file server. The procedure actually references the file using the UNC path to a share on a NT 4 server that is connected to the novel server using gateway services for NetWare. Now when it tries to reference the file I get: Server: Msg 4861,...
2
18775
by: Torsten Zachert | last post by:
I would like to insert some text with embedded carriage return/line feed into a MS Access text field with OleDb and C# ADO.NET. I tried to use "\n" in combination with "\r". If I display the input in a text field I only see quads. Tia Torsten
1
8355
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
12963
by: junkmail115 | last post by:
Greetings, A novice needs some advice: I am able to bulk insert data from a text file into a table. Is it possible to run a query and insert the results into a text file on the local or network hard drive? Thanks For Reading, Aaron
0
9650
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
10363
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
10110
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
9962
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
8992
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...
0
5398
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
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4067
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
3
2894
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.