Export result to csv with column header

  • hi all my requirement is to export a query result to CSV with the columns name how do i achieve this.

    any help highly appreciated.

  • Right-click on Database and go to Tasks\Export... Pick table for Source, and Flat File Destination for Destination. There is an option under Format including column names in the first data row.

    If you want to see what it does, save it as an SSIS package and open in Visual Studio.

    Thanks,

    Thomas

    Thomas LeBlanc, MVP Data Platform Consultant

  • Thanks thomas,

    But i wanted to it through BCP is there any way to do it?

  • hey i got the solution.

    it was posted by lowell earlier, tnx buddy.

    i m posting it again for duplication. :)))

    EXECUTE master.dbo.xp_cmdshell 'bcp "SELECT ''LoadSequence'',''Entity'',''EntityType'',''IsNewEntity'',''InsertEntity'',''EntityLogic'',''CheckDuplicate''" queryout C:\Users\ghanshyamk\Desktop\aa.txt -c -T'

    EXECUTE master.dbo.xp_cmdshell 'bcp "SELECT LoadSequence,Entity,EntityType,IsNewEntity,InsertEntity,EntityLogic,CheckDuplicate FROM Avalon_CA.dbo.AvalonEntityDetails" queryout C:\Users\ghanshyamk\Desktop\bb.txt -c -T'

    declare @sql varchar(2000)

    set @sql ='copy C:\Users\ghanshyamk\Desktop\aa.txt + C:\Users\ghanshyamk\Desktop\bb.txt C:\Users\ghanshyamk\Desktop\cc.txt'

    EXEC master..xp_cmdshell @sql

    first create a file with the header then create a file with the data and then copy it to other file.

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

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