Outputting sql query into flat file (txt)

  • I'm using windows server 2003 with microsoft server enterprise sql manager which has a huge db stored on it of information. I need to query some of those information and format it in a specific way into a flat file (i.e., the firs 10 characters = client id, second 10 characters is the name). Whats the best way to do this?

    My ideal setup would be - I would enter in the client id, it would query the sql for the rest of the information, then if its the correct one, output it the way i want to a flat file (txt). The main issue is outputting the selected data into a flat file in a specific format, I need to do this efficiently as it will probably be done often

  • Congratulations on your first post here.... 🙂

    Welcome buddy.

    You have planty of choices. Top 2 would be:

    1. run SQLCMD with format file

    2. use SSIS to export data

    For further info, have a look at:

    http://msdn.microsoft.com/en-us/library/ms162773(SQL.90).aspx

  • Hi;

    hope this could help you

    DECLARE @cmd sysname, @var sysname

    SET @var = 'Hello world'

    SET @cmd = 'echo ' + @var + ' > c:\\var_out.txt'

    EXEC master..xp_cmdshell @cmd

  • You could also tell SSMS to output your results to a file. This will allow you to tweak your query\T-SQL script however you want and then run it from right within SSMS and the output will be to a file.

    Joie Andrew
    "Since 1982"

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply