bcp

  • I have a stored procedure that I would like to export this query to a .txt file. 

    select x from #x1

    Can anyone help me with the bcp syntax of this inside my SP?

    Thanks

    Phil

     

  • Please refer to

    http://www.sqlservercentral.com/scripts/contributions/1127.asp

    Thanks

     

    Prasad Bhogadi
    www.inforaise.com

  • Prasad,

     

    Thanks for your input. BUT I have been trying everything to get it to work with no success.  It must be in the syntax.  

    Any and all help is greatly appreciated.

    Thanks,

    Phil

     

     

  • I think you need to add servername as a prameter to the bcp command. Something like:

    'bcp "SELECT ' + @varFieldList + ' FROM ' + @varTableName + '" QUERYOUT "' + @varOutFile + '" -T -c -S'

    where is the full name of your server

  • OOPS ...the parameter should be visible now...

    I think you need to add servername as a prameter to the bcp command. Something like:

    'bcp "SELECT ' + @varFieldList + ' FROM ' + @varTableName + '" QUERYOUT "' + @varOutFile + '" -T -c -S[servername]'

    where [servername] is the full name of your server

  • If you are trying to output the contents of your temp table using BCP, you will not be able to unless you use a global temp table.

    for example to output the first 10 products on Northwind database to a csv file via a global temp table

    SELECT TOP 10 *

    INTO ##test

    FROM Northwind.dbo.Products

    exec master..xp_cmdshell 'bcp "SELECT * FROM ##test" queryout C:\temp\outputfile.csv -c -t ","'

    DROP TABLE ##test

    make sure you DROP the global temp table afterwards as it will not be automatically deleted on connection close

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 6 posts - 1 through 5 (of 5 total)

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