SQL Server Performance

Write into text file

Discussion in 'T-SQL Performance Tuning for Developers' started by CanadaDBA, Jan 31, 2005.

  1. CanadaDBA New Member

    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
  2. thomas New Member

    xp_cmdshell. if you want to interact with OS files it'll be required. that or DTS.

    Tom Pullen
    DBA, Oxfam GB
  3. Madhivanan Moderator


    exec master..xp_cmdshell 'echo 1,a,b,c,d,e,gfh >> d:Echo.txt'

    Madhivanan
  4. CanadaDBA New Member

    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
  5. thomas New Member

    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

Share This Page