Cannot write tags to text file in SQL Server trigger

时间:2015-07-28 16:20:10

标签: sql-server tsql triggers

I've been working around a limitation put in place by a vendor and it is requiring me to write a SQL Server trigger that populates a text file that I then move to a mail pickup folder (it would be so much easier if the vendor would allow me to email directly form SQL.

With that said I can write to text files and move them just fine but what I cant seem to do is insert <html> tags into the text file.

When I try it just leaves that line out. Any help out there?

SET @from = 'FROM: no-reply@domainname.com' ;
set @to = 'TO: keith@domainname.com';
SET @subject = @subjecttext
SET @cmd = '(echo '+@from+' && echo '+@to+' && echo ' +@subject+') >//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

--set @body = 'more text';
--SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
--EXEC master..xp_cmdshell @cmd, no_output;

set @body = 'Content-Type: multipart/mixed;' 
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

Set @body = 'boundary="Attached"';
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

Set @body = '--Attached' ;
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

Set @body = 'Content-Type: text/html;charset=UTF-8' ;
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

Set @body = 'Content-Transfer-Encoding: 7bit' ;
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

Set @body = '--Attached--'
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

Set @body = '<b>' + 'THIS IS TEST TEXT';
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

2 个答案:

答案 0 :(得分:4)

A trigger should be very small and nimble since it executes in the context of the transaction that caused the trigger to fire.

It should most definitely not do any heavy lifting and extensive processing, like writing out to an external file...

I would recommend rethinking your trigger strategy - in your case, I would

  • write a trigger that outputs the relevant information into a separate "PendingEmails" table (or something like this) - but nothing more

  • create a separate program (could be a T-SQL stored procedure, or a front-end application written in C# or something) that would then consult that "PendingEmails" table on a scheduled basis, and if needed, would actually send out those e-mails you want to send out - in a separate process, not within the same database transaction

Otherwise, your trigger might end up taking a long time to complete, especially if it might need to wait for an external source to return with an acknowledgement - and this will happen in the context of your actual SQL statement - a sure fire way to kill off any performance your database might have had....

答案 1 :(得分:0)

每当你想使用:

编写html标签时
Set @body = '<b>' + 'THIS IS TEST TEXT';
SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell @cmd, no_output;

您需要了解CMD.EXE echo ...实际上正在执行。因为你的@body包含&lt;和&gt;字符,CMD.EXE将其解释为:

  1. 从文件重定向输入:&lt;
  2. 将输出重定向到文件:&gt;
  3. 要避免此CMD.EXE的解释,您需要转义所有html标记&lt;和&gt;字符,通过在重定向字符前面放置^(插入符号字符),如:

    Set @body = '<b>' + 'THIS IS TEST TEXT';
    SET @body = REPLACE(@body, '<', '^<'); -- escape <
    SET @body = REPLACE(@body, '>', '^>'); -- escape >
    SET @cmd = '(echo '+@body+') >>//server02-2010/test/SprintTask.txt'; -- here, of course, output redirection is needed
    EXEC master..xp_cmdshell @cmd, no_output;
    

    干杯,保罗