How to display the [CHAR (9)] tab in EXEC

I know I can do the following:

DECLARE @Tab CHAR(1)
SET @Tab = CHAR(9)
EXEC('xp_cmdshell ''echo ' + @Tab + 'Some text>> C:\test.txt'', NO_OUTPUT')

But is there a way to do this in 1 line? That is, remove the need to declare and set a tab and display it directly in EXEC?

+5
source share
2 answers

In one line, but without deleting ads. EXEC and sp_executesql do not allow the use of function calls in a command or parameters. Therefore, you need to declare a string variable to insert CHAR (9) into the command:

exec('DECLARE @str varCHAR(200);
      set @str = ''xp_cmdshell ''''echo '' + CHAR(9) + ''Some text>> C:\test.txt'''', NO_OUTPUT'';
      EXEC(@str)');
+5
source

try the following:

   declare @str varchar(500)= 'xp_cmdshell ''echo '+ char(9)+' Some text>>
   C:\test.txt'', NO_OUTPUT'
exec(@str)
+1
source

All Articles