1

I'm making some test variable and I insert a new line character into it. When I print it everything is ok, 1 word is below the second one. But when I write the value of this variable into *.txt file the new line character doesn't work. Here is my example:

DECLARE @cmd varchar(200), @var varchar(200)
SET @var = 'Hello' + CHAR(13) + 'world'

SET @cmd = 'echo ' + @var + ' > E:\s.txt'
print @cmd
EXEC master..xp_cmdshell @cmd

I was trying with DECLARE @NewLineChar AS CHAR(2) = CHAR(13) + CHAR(10) or \r\n instead of this CHAR(13) but no effect :/ Could someone give me small hint about this problem? I'm using Microsoft SQL Server 2008 R2

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3762819
  • 105
  • 11

2 Answers2

0

Does this work?

DECLARE @cmd varchar(200), @var varchar(200);
SET @var = 'Hello
world';

SET @cmd = 'echo ' + @var + ' > E:\s.txt';
print @cmd;
EXEC master..xp_cmdshell @cmd;

EDIT:

I believe your problem is escaping the new line in the windows shell. Try the escape character '^':

DECLARE @cmd varchar(200), @var varchar(200);
SET @var = 'Hello^
world';

SET @cmd = 'echo ' + @var + ' > E:\s.txt';
print @cmd;
EXEC master..xp_cmdshell @cmd;

Be sure there is no space or other character after the '^'.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • No, it doesn't create any file then, normally result information is: output (newline) ----- (newline) NULL, but whene somethins is wrong like here there is ------ (newline) Hello (newline) NULL and no file is created – user3762819 Dec 27 '14 at 16:05
  • Still no difference - 'Hello^world' works, but making ENTER between '^' and 'world' doesn't work :/ – user3762819 Dec 27 '14 at 16:49
  • @user3762819 . . . I'm out of ideas. I'll leave this answer here in case it helps anyone else trying to solve this problem. – Gordon Linoff Dec 27 '14 at 17:00
0

How To add blank line in output/log/txt file Sample Code

BEGIN

--Ebnable xp_cmdshell
EXEC master.dbo.sp_configure 'show advanced options', 1;
RECONFIGURE WITH OVERRIDE;
EXEC master.dbo.sp_configure 'xp_cmdshell', 1;
RECONFIGURE WITH OVERRIDE;

DECLARE @cmd VARCHAR(255); DECLARE @var NVARCHAR(MAX) = ''; DECLARE @v_MyOutFile_v VARCHAR(20)= 'C:\MyOutFile.txt'; --Provide output filename with path

--Add 1st line
SET @var = '1st Line';
SET @cmd = concat('echo ', @var, '>> "', @v_MyOutFile_v, '"');
exec xp_cmdshell @cmd, no_output

--Add Blank line
SET @var = '& echo.'; --Blank line
SET @cmd = concat('echo ', @var, '>> "', @v_MyOutFile_v, '"'); --Do not add any spaces before >> otherwise it will be printed in the output file
exec xp_cmdshell @cmd, no_output

--Add 2nd line after blank line
SET @var = '2nd Line';
SET @cmd = concat('echo ', @var, '>> "', @v_MyOutFile_v, '"');
exec xp_cmdshell @cmd, no_output

--Disable xp_cmdshell
EXEC master.dbo.sp_configure 'xp_cmdshell', 0;
RECONFIGURE WITH OVERRIDE;
EXEC master.dbo.sp_configure 'show advanced options', 0;
RECONFIGURE WITH OVERRIDE;
END;

JRD
  • 1
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 16 '23 at 20:16