I'm trying to copy csv to database table using bcp command. It's successfully copied, but the problem is non-English letters was migrated as some junk characters.
BCP query:
bcp "[table_name]" in "[file_path]" -F 1 -a 32768 -c -C RAW -t"|~|" -r "|\n|" -S "[server]" -U "[username]" -P "[password]" -d [database]
after migration
This is the source
Another Scenario:
When I use -C 65001 instead of -C RAW above issue is fixed. But I've fixed another bug that is opened again. Please help me on the same.
Source: enter image description here
AfterMigration: enter image description here
I've found the solution to my problem.
Previously I've used text and nvarchar data types in my destination tables.
using -C 65001 the non English letters migrated properly for nvarchar columns not for text datatype.
using -C RAW the non English letters migrated properly for text columns not for nvarchar datatype.
Related
I want to export varbinary with bcp to a flat file (csv).
It seems to work, but in front of the 0x is a strange enconding, I can not get rid of. Can someone explain what I'm doing wrong? Following the bcp-command I execute and the screenshot from notepad++
PS C:\Windows\system32> bcp "SELECT TOP 5 CONVERT(varchar(max), [Bild],1) AS SomeImageFieldAsHex FROM [SWTA_HH].[dbo].[Fenster]" queryout "c:\export.csv" -d "SWTA_HH" -T -n -r \n
Thanks Darius
As per the bcp Utility documentation, the -n switch is instructing BCP to output data in "native" format, which includes binary information to allow a receiving SQL Server to ingest the data again using the correct data types.
If you're wanting to output these varbinary values to a .CSV (comma-separated values) file then you'll probably want to use the -c switch instead, for character data, e.g.:
bcp "SELECT TOP 5 CONVERT(varchar(max), [Bild], 1) AS SomeImageFieldAsHex FROM [SWTA_HH].[dbo].[Fenster]" queryout "c:\export.csv" -d "SWTA_HH" -T -c -r \n
I'am trying to import data into sql server table from a file using a format file.
In fact I have 2 databases: a production database and a local database
I want to insert some row of the table shipper of the production database in the local one. The table shipper don't have neither the same columns nor the same order of column in the 2 databases.
That's why I used a file format to do my bcp.
I generate file containing the rows I want to insert in my local database with the following commande
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -c -T
It works !!
I generate then the format file with the schema of my local table with the following commande
bcp LocalDatabase.dbo.shipper nul -T -n -f shipper-n.fmt
It works !!
Unfortunately when I tried to insert the file data in my local table
with the following commande:
bcp LocalDatabase.dbo.shipper in shipper.txt -T -f shipper-n.fmt
it generates the following error (translated from french)
Can anyone know what is the problem and how can I get arround it.
Thanks in advance
unexpected end of file encountered in the bcp data file
Your format file does not match the data. You are exporting using text using -c
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -c -T
But your format file is made for native (binary) data using -n
bcp LocalDatabase.dbo.shipper nul -T -n -f shipper-n.fmt
Either export both as native (my recommendation), or both as text. To prevent this error, export the data file and the format file at the same time, simply add -f shipper.fmt to your export
Text version:
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -c -T -f shipper.fmt
or
Native Version:
bcp "SELECT shipper_id,Shipper_name FROM ProductionDatabase.dbo.shipper where shipper_id >5" queryout shipper.txt -n -T -f shipper.fmt
PS. Since you can run into scenarios where your record or row delimiters exist in the data you should pick a character sequence that does not exist in your data as a separator for instance -t"\t|\t" (Tab-Pipe-Tab) for fields and -r"\t|\n" (Tab-Pipe-Newline) for rows. If you combine the format statement with the export the data and the format file will match and you have the freedom to change the separators on a single command line.
Specify separators after the -n or -c on the command line
I'm stuck at a very simple bcp and am hoping someone here could tell what i am doing wrong...
I have a single column textfile contaning email addresses. I'm trying to BCP this to SQL server using "bcp" command. Below is the sample file and the command I'm using.
I have used bcp before with different sets of files, but never had an issue. Any idea why this file wouldn't load?
D.out
'a#a.com'
'b#b.com'
'c#c.com'
bcp command:
bcp temp_sf_email in D.out -SMyDBServer\INSTANCEA -T -w
Looks basic. But when run, doesnt do anything. The output I got is:
Starting Copy.....
0 Rows copied.
Thanks in advance for any suggestions..
Regards,
Simak
I would like to suggest this sintax for BCP command
bcp db_name.schema_name.table in "drive:\folder\file.ext" -SServer -T -c
example for your case:
bcp yourdb.dbo.temp_sf_email in "c:\folder\D.out" -SMyDBServer\INSTANCEA -T -c
use the flag -c that means -c character type instead -w
for more info about BCP click here
NOTE: I tested this example in SQL2005 and it work, btw you didnt specificy which SQL version was used
Hope it help
I've got a problem while copying data from SQL Server 2012 to Azure DB.
Here I'm listing the steps I have made.
Created .dat and .xml format files as follows:
bcp.exe dbo.user_sayti out "c:\\dbo.user_sayti.dat" -w -k -Slocalhost -dsource_db -Uuser -Ppwd
bcp.exe dbo.user_sayti format nul -f "c:\\dbo.user_sayti.xml" -w -x -Slocalhost -dsource_db -Uuser -Ppwd
dbo.user_sayti.dat
dbo.user_sayti.xml
Made an attempt to copy them to Azure DB with keeping identity values:
bcp.exe dbo.user_sayti in "c:\\dbo.user_sayti.dat" -E -f "c:\\dbo.user_sayti.xml" -Sserver.database.windows.net -dtarget_db -Uuser -Ppwd
And have got "Invalid character value for cast specification" error.
I don't understand why, because the .dat file contains value 127 for the identity column id (PK, int, not NULL) and the number of values matches the number of rows.
Then I've tried the same command without -E and the process finished successfully (there is the only row in this particular table and it appeared in the target_db with identity column value = 1).
Replacing -f parameter (schema definition) with -w solved that stupid problem :)
What i want to do is copy a table into a file, truncate the table and copy the data back into the table.
For this, i am using the following two commands:
Out: bcp TABLE out file.csv -S SERVER -U user -P password -r '\n' -t '^|' -c
In: bcp TABLE in file.csv -S SERVER -U user-P password-r '\n' -t '^|' -c -J iso_1 -b 5000
This is the error i get:
CSLIB Message: - L0/O0/S0/N36/1/0:
cs_convert: cslib user api layer: common library error: The result is truncated because the conversion/operation resulted in overflow.
The interesting part ( for me, at least ) is that i get the error only for rows with the first column being an ODD number. From the first 3 million rows, it cuts half of them, all having the first column ( the PK ) an odd number.
I tried with different options, but none seem to work: no problem with the charset as far as i can tell, there are no huge columns such that they are truncated and it is not the carriage return missing.
Any help would be greatly appreciated.
UPDATE: After creating a format-file there are no more errors, but it only copies half of the data back into the table.
UPDATE: I managed to create a format file which works and loads all data, but i cannot use it on another server (it works in testing environment, it needs to run in production environment), since it says Attempt to read an unknown version of bcp format-file.? I know what this means, but is there any way of finding the correct values of the version?
SOLVED: After digging back in the database, it seems that the problem was indeed data inconsistency due to the fact that the VIEW used in production to copy the table only copied 25 columns, but the table has 26 columns ( somebody altered the table and i didn't know and hadn't noticed that it happened ). Fixed the View and now it works.
Since you are going out of/into the same server, I recommend you use bcp with the native flag.
bcp DBNAME..TABLE out file.bcp -SSERVER -Uuser -Ppassword -n
bcp DBNAME..TABLE in file.bcp -SSERVER -Uuser -Ppassword -n -b5000
Character mode can get wierd, and I only use it when it is required.