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? I.e. remove the need to declare and set tab, and output it directly in the EXEC?
In one line but without removing declarations. EXEC and sp_executesql doesn’t allow to use functions call in the command or parameters. So you need to declare a string variable to insert CHAR(9) in the command: