• A few things:

    1) osql is deprecated in SQL 2005. Consider switching to use sqlcmd or bcp for this work.

    2) accessing the file system from T-SQL can create some scenarios that are difficult to debug. Consider switching from running this process in a cursor in T-SQL to something in SSIS or PowerShell so you can move away from using xp_cmdshell. Moving in this direction could make item 1 moot since SSIS and PowerShell both have the ability to write to files natively, i.e. without the help of SQL Server command lines tools.

    3) We cannot see what you see. Please post the code that includes the cursor and the call to xp_cmdshell so we can see too. Make sure to cleanse any sensitive information from the code before posting including items in code comments and object names that may reveal company or personal information.

    There are no special teachers of virtue, because virtue is taught by the whole community.
    --Plato