Exporting data from SQL Server Express to CSV (need quoting and escaping)

后端 未结 10 1933
执笔经年
执笔经年 2020-12-05 04:16

I\'ve spent 2 days trying to export a 75,000 row table containing a large text field of user input data from a SQL server installation. This data contains every plain ascii

相关标签:
10条回答
  • 2020-12-05 04:56

    I've been trying to figure this out as well. Not sure if this will work for you since your table is much larger than mine, but this is what I did just out of a whim:

    1. I pulled up my table in Express by doing a SELECT * statement
    2. Simply selected the resulting rows and Ctrl + C
    3. Opened Excel
    4. Highlighted the amount of columns the table I was pasting had
    5. Pasted, and it friggin' worked!
    6. Now just have to Export Excel as CSV and done.

    I know it probably sounds stupid, but it actually worked for me.

    0 讨论(0)
  • 2020-12-05 04:56

    We made a small script to convert SSMS "broken" CSV into proper CSV, find it in this answer:

    https://stackoverflow.com/a/46876236/1532201

    0 讨论(0)
  • 2020-12-05 04:59

    Here's the essence of a script I use to do just this:

    require 'rubygems'
    require 'active_record'
    require 'tiny_tds'
    require 'activerecord-sqlserver-adapter'
    require 'acts_as_reportable'
    require 'ruport'
    
    ActiveRecord::Base.logger = Logger.new("log/debug.log")
    ActiveRecord::Base.establish_connection(
      :adapter    => 'sqlserver',
      :mode       => 'dblib',
      :dataserver => 'servername',
      :username   => 'username',
      :password   => 'password',
      :timeout    => '60000'
    )
    
    class Table1 < ActiveRecord::Base
      set_table_name 'table_name'
      set_primary_key 'table_id'
      acts_as_reportable
    end
    
    Table1.report_table(:all).save_as("finished/table1.csv")
    

    Hope it helps!

    0 讨论(0)
  • 2020-12-05 05:01

    It can be done! However you have to specifically configure SSMS to use quoted output, because for some daft reason it is not the default.

    In the query window you want to save go to Query -> Query Options...

    Check the box "quote strings containing list separators when saving .csv results".

    enabling quoted csv output

    then

    select 'apple,banana,cookie' as col1,1324 as col2,'one two three' as col3,'a,b,"c",d' as col4
    

    will output

    col1,col2,col3,col4
    "apple,banana,cookie",1324,one two three,"a,b,""c"",d"
    

    which is what we all want.

    0 讨论(0)
提交回复
热议问题