SQL Server BCP: How to put quotes around all fields?

前端 未结 7 949
星月不相逢
星月不相逢 2021-02-08 22:01

I have this BCP command:

\'bcp DBName..vieter out c:\\test003.txt -c -T /t\"\\\",\\\"\" -S SERVER\'

The output CSV I get does not put quotes ar

相关标签:
7条回答
  • 2021-02-08 22:18

    The actual workable answer, that removes the leading quote, is to :

    A) generate format file with bcp :

    bcp db.schema.tabel format nul -c -x -f file.xml -t"\",\"" -r"\"\r\n" -T -k
    

    B) edit that file to manually copy field 1 to field 0 above, as the first field, set Max_Length=1 and remove the separator and one quot the was in field1

    <FIELD ID="0" xsi:type="CharTerm" TERMINATOR="\&quot;" MAX_LENGTH="1" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
    

    The trick works, as you are adding a field (interface to the file) to detect the first seprator, which results in an always null-value, but not add a row (interface for the query output).

    0 讨论(0)
  • 2021-02-08 22:18

    Alternatively, if you are fine for Powershell based script, you can try with below code, which does automatic quoting.

    Invoke-sqlcmd -ConnectionString "Server=SERVERNAME, `
    3180;Database=DATABASENAME;Trusted_Connection=True;"  `
    -Query "SET NOCOUNT ON;SELECT * FROM TABLENAME" -MaxCharLength 700 | `
    Export-Csv -NoTypeInformation -path C:\temp\FileName.csv -Encoding UTF8
    
    0 讨论(0)
  • 2021-02-08 22:20
    bcp "SELECT char(34) + * +char(34) FROM atable queryout "C:\temp\out.csv" -T -N -c /t"\",\""
    

    This will put quotes before and after each field (including the first and the last).

    0 讨论(0)
  • 2021-02-08 22:21

    I guess your goal was to clearly seperate field values by using an unique identifier so that import procedure doesn't have an issue.

    I had same issue and found this workaroud useful: Using an unusual field terminator, for example | or even a string /#/ can be very unique and shouldn't mess with your string content. You also can HEX-Values (limited, see https://docs.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-2017)

    export

    bcp DB.dbo.Table out /tmp/output2.csv -c -t "/#/" -U sa -P secret -S localhost
    

    import

    bcp TargetTable in /tmp/output2.csv -t "/#/" -k -U sa -P secret -S localhost -d DBNAME -c -b 50000 
    
    0 讨论(0)
  • 2021-02-08 22:24

    Here are the list of commands i used .

    BCP "DECLARE @colnames VARCHAR(max);SELECT @colnames = COALESCE(@colnames + ',', '') + column_name from databaseName.INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='tableName'; select @colnames;" queryout "C:\HeadersOnly.csv" -r"\n\""  -c -T -Uusername -Ppassword -SserverName
    
    bcp databaseName.schema.tableName out "C:\EmployeeDatawithoutheaders.csv" -T -t"\",\"" -r"\"\n\"" -c -Uusername -Ppassword -SserverName
    
    copy /b C:\HeadersOnly.csv+C:\EmployeeDatawithoutheaders.csv C:\EmployeeData.csv
    
    del C:\HeadersOnly.csv
    
    del C:\EmployeeDatawithoutheaders.csv
    
    0 讨论(0)
  • 2021-02-08 22:39

    Setting the row terminator in addition to the field terminator should do the trick

    'bcp DBName..vieter out c:\test003.txt -c -T -t"\",\"" -r"\"\n\"" -S SERVER'
    

    This will likely work, but miss off the leading " for the first field of the first line, and perhaps the last field of the last line - I'm not sure, just guessing really, no server here!

    or try using QUOTENAME to wrap text fields (you could also wrap numbers, but that isn't normally required.)

    'bcp "SELECT id, age, QUOTENAME(name,'"') FROM DBName..vieter" queryout c:\test003.txt -c -T -t"," -S SERVER'
    
    0 讨论(0)
提交回复
热议问题