bcp doesn't close files

  • Not sure if anyone else has seen this before, but I'm at a loss. I'm exporting records from a global temp table using bcp and this works fine. This is being done in a stored procedure using xp_cmdshell to call bcp to create the file, and then the file is also emailed.

    The problem is that when I try to overwrite the file using bcp it fails. And when I try to just manually delete it, I get the error message "The actionan't be completed because the file is open in SQL Server Close the file and try again"

    The bcp command I'm using is:

    bcp "select * from tempdb..##Sub_Error_Temp" queryout "J:\SubLoad_Errors.csv" -c -t, -U xxxx -P xxxx -S SERVERNAME\INSTANCE

    Has anyone else seen this before?

  • I use BCP but usually do not use a global temp. I am curious when you dispose of the global ##temp. When you create the .txt it should not have anything to do with the #table. I would think it is something in the process that is locking it like a process running against which will not release a lock.

  • BCP won't hold a table open like that. Something else has a grip on the table.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

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

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