Write into text file | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Write into text file

I’m going to create a stored procedure that checks for a text file. If it exists, appends a line to it. If id doesn’t exist, creates it and appends the line to it. What commands are the most useful one? CanadaDBA
xp_cmdshell. if you want to interact with OS files it’ll be required. that or DTS. Tom Pullen
DBA, Oxfam GB

exec master..xp_cmdshell ‘echo 1,a,b,c,d,e,gfh >> d:Echo.txt’ Madhivanan
Thanks for both replies. Specially, the "Echo" idea works fine! It creates the file if it does not exist and appends to file if it exists. Exactly, what I was looking for. Just for my knowledge, do we have commands in T-SQL that we can check for files or do the above with those commands. I will use the command shell and Echo but I am just curious. CanadaDBA
there are extended stored procs that do this, e.g xp_fileexist .. or you can do it using xp_cmdshell manually. there are also XPs that do stuff like read the registry. Tom Pullen
DBA, Oxfam GB
]]>