I am trying to create a text file that includes line breaks. I have this code:
DECLARE #Text AS VARCHAR(100)
DECLARE #Cmd AS VARCHAR(100)
SET #Text = 'This is my text line 1 (supposed to be new line character) This is line2'
SET #Cmd ='echo ' + #Text + ' > C:\FileStore\test.txt'
EXECUTE Master.dbo.xp_CmdShell #Cmd, NO_OUTPUT
What should i use to have a new line character on the text file so that the output should be
This is my text line 1
This is line2
UPDATE:
I've tried this as per suggestions
SET #Text = 'This is my text line 1' + char(13) + char(10)+ 'This is line2'
But with this, it won't create the txt file anymore
Thanks
Use CHAR(13) to output a carriage return, or CHAR(13) + CHAR(10) for carriage return plus line feed (for windows)
Windows uses a carriage-return line-feed sequence to indicate a new line, character codes 13 & 10 so:
select 'hello' + char(13) + char(10) + 'world'
(Note than in SSMS grid view you will just see a double space)
it won't create the txt file any more
Ah thats the echo command line not supporting new-lines.
You could
echo This is my text line 1 > "C:\FileStore\test.txt" & echo This is line2 >> "C:\FileStore\test.txt"
Or find a different way that does not involve the command line.
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 1;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
DECLARE #cmd varchar(8000), #var varchar(2000)
SET #cmd = 'echo line1 > "d:\test.txt"'
+ '& echo line2 >> "d:\test.txt"'
+ '& echo line3 >> "d:\test.txt"'
+ '& echo line4 >> "d:\test.txt"'
+ '& echo line5 >> "d:\test.txt"'
EXEC master..xp_cmdshell #cmd;
EXEC sp_configure 'show advanced options', 1;
GO
-- To update the currently configured value for advanced options.
RECONFIGURE;
GO
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell', 0;
GO
-- To update the currently configured value for this feature.
RECONFIGURE;
GO
Related
I have an SP that invokes xp_cmdshell to call an .exe. The .exe has logic to avoid requiring user input, but a few still slip through, which cause the SP to hang indefinitely and leave things in a bad state.
Is there a way to add timeout logic to the xp_cmdshell call, so that the .exe call is aborted, gracefully if possible, after 60 seconds (i.e. equivalent of CTRL-Z or CTRL-C in cmd prompt)?
SP
CREATE OR ALTER PROCEDURE RC.USR_SP_REFRESHEXCELFILE (
#SourceFilePath VARCHAR(1000)
, #SourceFileName VARCHAR(255)
, #TargetFilePath VARCHAR(1000)
, #TargetFileName VARCHAR(255)
, #Result INTEGER OUTPUT -- 0 = success, 1 = failure
) AS
BEGIN
-- Declare variables
DECLARE
#ExcelRefreshProgramLocation VARCHAR(1000),
#CommandString VARCHAR(8000)
-- Get exe path
SET #ExcelRefreshProgramLocation = <...some location...>
-- Set command string
SET #CommandString =
#ExcelRefreshProgramLocation + ' ' +
#SourceFilePath + ' "' +
#SourceFileName + '" ' +
#TargetFilePath + ' "' +
#TargetFileName + '"' -- quotes not working with file paths
-- Allow advanced options to be changed
EXECUTE sp_configure 'show advanced options', 1;
RECONFIGURE;
-- Enable command shell
EXECUTE sp_configure 'xp_cmdshell', 1;
RECONFIGURE;
-- Execute command
EXEC #Result = xp_cmdshell #CommandString;
-- Disable command shell
EXECUTE sp_configure 'xp_cmdshell', 0;
RECONFIGURE;
-- Prevent advanced options from being changed
EXECUTE sp_configure 'show advanced options', 0;
RECONFIGURE;
END
I received one requirement from client,if any take the database backup from SQL Server, backup will by default contains the password.
so No One can restore the database without password.
I suggests the TDE encryption solution but he is not agree with this.
anyone one know, Is there any SQL setting for this?
Thanks
Nirav
IDK if there is any built-in option (except TDE). We are using WinRAR for similar purpose :
EXEC sp_configure 'show advanced options', 1
GO
RECONFIGURE
GO
EXEC sp_configure 'xp_cmdshell', 1
GO
RECONFIGURE
GO
DECLARE #fileName varchar(1000)
DECLARE #pathName varchar(1000)
DECLARE #backupName varchar(1000)
DECLARE #backupFullName varchar(1000)
DECLARE #rarName varchar(1000)
DECLARE #compressCommand varchar(1000)
DECLARE #deleteBakCommand varchar(1000)
DECLARE #deleteRarCommand varchar(1000)
DECLARE #moveCommand varchar(1000)
DECLARE #deleteOldRarCommand varchar(1000)
SET #fileName = 'database' + CONVERT(varchar, DATEPART(weekday,getdate()))
set #pathName = 'D:\folder\'
SET #backupName = #fileName + '.bak'
SET #rarName = #fileName + '.rar'
SET #backupFullName =#pathName + #backupName
SET #compressCommand = 'C:\Progra~1\WinRAR\rar a -m5 -pPASSWORD '+ #pathName + #rarName + ' ' + #pathName+#backupName + '"'
SET #deleteBakCommand = 'del ' + #pathName + #backupName
SET #deleteRarCommand = 'del ' + #pathName + #rarName
SET #deleteOldRarCommand = 'del ' + #pathName + 'TARGET\db\' + #rarName
set #moveCommand = 'copy /y ' + #pathName+#rarName + ' ' + #pathName + 'TARGET\db\' + #rarName
EXEC master..xp_cmdshell #deleteBakCommand;
EXEC master..xp_cmdshell #deleteRarCommand;
BACKUP DATABASE DBNAME TO DISK = #backupFullName;
EXEC master..xp_cmdshell #deleteOldRarCommand;
EXEC master..xp_cmdshell #compressCommand;
EXEC master..xp_cmdshell #deleteBakCommand;
EXEC master..xp_cmdshell #moveCommand;
EXEC master..xp_cmdshell #deleteRarCommand;
GO
EXEC sp_configure 'xp_cmdshell', 0
I'm trying to create a txt file in a shared folder using xp_cmdshell in SQL Server.
The code works perfectly, but I need to begin the text with:
"<0600 ..."
So when I write " < " or " > " in the query, I get an error:
The system cannot find the file specified.
Here's an example of my code:
declare #Text as nvarchar(150) declare #Cmd as nvarchar(100) set #Text = '<0600 /SERIAL= ....>' set #Cmd ='echo ' + #Text + ' > C:\Test.txt' execute ..xp_CmdShell #Cmd
So, how can I create the text file with these characters?
Escape the redirection symbols (< and >) with ^
declare #Text as nvarchar(150)
declare #Cmd as nvarchar(100)
set #Text = '^<0600 /SERIAL= ....^>'
set #Cmd ='echo ' + #Text + ' > C:\Test.txt'
execute ..xp_CmdShell #Cmd
http://www.robvanderwoude.com/escapechars.php
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;
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....
Whenever you would like to write html tags using:
Set #body = '<b>' + 'THIS IS TEST TEXT';
SET #cmd = '(echo '+#body+') >>//server02-2010/test/SprintTask.txt';
EXEC master..xp_cmdshell #cmd, no_output;
you need to understand that CMD.EXE echo ... is actually being performed. Since your #body contains < and > characters, CMD.EXE interpret this as:
redirect input from a file: <
redirect output to a file: >
To avoid this CMD.EXE's interpretation, you need to escape all html tag < and > characters, by placing a ^ (caret character) in front of the redirection characters like:
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;
Cheers, Paul
I am trying to run the following commands to generate a file and populate the file with varchar(MAX) amount of data. Here is the issue, when I try to run the code it creates the files but they are empty. I know the #classString variable has data in it. Is there a string enclosing sequence I can wrap around the #classString to delimit it? I am trying to generate a script so I am assuming the #classString contains characters that are interfering with the file write.
SET #sqlString = 'echo off > ' + #FilePath + '\' + #table_name + '.cs"'
exec xp_cmdshell #sqlString
SET #sqlString = 'echo ' + #classString + ' >> ' + #FilePath + '\' + #table_name + '.cs"'
exec xp_cmdshell #sqlString
There is no easy way to delimit or quote the whole string, instead you need to escape any shell metacharacters within it using the ^ symbol. For example:
SET #classString = (SELECT REPLACE(#classString, '>', '^>'))
SET #classString = (SELECT REPLACE(#classString, '&', '^&'))
...and so on for any cmdshell metacharacters that may feature in your string.