Hi @VenkataVarada ,
You can use bcp.exe for what you need.
SQL
/*
-T tells BCP to use Windows Authentication to connect to SQL Server. It will operate as the user who's running the command.
-U <login_id> -P <password> tells BCP to use SQL Server account while connecting to SQL Server.
*/
DECLARE @SQLCmd VARCHAR(8000)
, @outputFileName VARCHAR(256) = 'e:\Temp\SampleXMLOutput.xml'
, @bcp VARCHAR(256) = 'c:\Program Files\Microsoft SQL Server\Client SDK\ODBC\170\Tools\Binn\bcp.exe'
, @SQL VARCHAR(2048) = 'SET NOCOUNT ON; select EmailBody from table where id =''100'';'
, @tcpPort VARCHAR(10) = '1433'
, @WindowsAuth BIT = 1 -- 1 is for Windows Authentication, 0 is for SQL Server Authentication
, @loginID VARCHAR(100) = 'loginID'
, @password VARCHAR(100) = 'password'
, @serverName VARCHAR(100) = 'SPACESHIP';
-- older versions of SQL Server
-- /B "WindowTitle" parameters produce output in the SSSMS !!!
SET @SQLCmd = 'START /B "WindowTitle" "' + @bcp + '"'
+ ' "' + @SQL + '"'
+ ' queryout "' + @outputFileName + '"'
--+ IIF(@WindowsAuth = 1, ' -T', ' -U '+ @loginID + ' -P ' + @password)
+ CASE WHEN @WindowsAuth = 1 THEN ' -T'
ELSE ' -U '+ @loginID + ' -P ' + @password
END
+ ' -x -c -C 1252 -a 32768'
+ ' -S "' + @serverName + ',' + @tcpPort + '"';
-- just to see it
SELECT @SQLCmd AS [Command to execute];
-- create file on the file system
EXECUTE master.sys.xp_cmdshell @SQLCmd;