BCP

  • I have a stored procedure that exports data from a table into .csv file. There are several different record sets that get exported. They are all comma delimited with double quotes as a text qualifier. All of my files export and you can double click open the .csv file into excel and everything is fine, except for one. The interesting thing about this record set is that some of the data has a trademark symbol.

    BCP:

    DECLARE @bcp varchar(1000) = 'bcp "SELECT * from dbo.tempExportData" queryout "' + @filePath + + @FileOutputName + '" -T -w';

    EXEC @Result = xp_cmdshell @bcp;

    In the process I am getting the data:

    DECLARE @ColumnList varchar(5000)

    select @ColumnList = coalesce(@ColumnList + ',','') + quotename(cast(Name as varchar(50)) , '"')

    from tempdb.sys.columns

    where object_id = object_id('tempdb..#tempData')

    insert into dbo.tempExportData (outputData)

    values (@ColumnList)

    insert into dbo.tempExportData (outputData)

    select

    quotename(isnull(FieldA, ''), '"') + ',' +

    quotename(isnull(FieldB, ''), '"')

    from #tempData

    CSV:

    FieldA,"FieldB"

    ACME®,"some more data"

    ,"even more data"

    Big Company,"still more data"

    All of the data is contained in column A of the spreadsheet and FieldA is not text qualified. If you open the .csv in notepad,textpad, etc all of the columns are text qualified. I know that I can open excel and use the import wizard to successfully import the data but I am wondering why when you open the .csv excel is not handling it correctly?

    Let me know if you need more information.

  • I have a bit of a confusion here. BCP can build a comma delimited file for you. Why are you pre-constructing the CSV? What other issue did it solve for you?


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • I needed to text qualify the data as some of the data had commas in it, e.g. ACME, Inc.

  • 24tjf (3/25/2013)


    I needed to text qualify the data as some of the data had commas in it, e.g. ACME, Inc.

    http://msdn.microsoft.com/en-us/library/aa174646(v=sql.80).aspx

    I think you will find BCP can perform that function in itself.

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

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