BCP Import error "Invalid character value for cast specification" - sql-server-2012

All
I am using BCP for import export and getting "Invalid character value for cast specification" error for only 1(first row of export) row while trying to import back.
Table Structure
Col1 -- Numeric(19,0)
Col2 -- NVARCHAR(400)
Col3 -- NVARCHAR(400)
I am using following commands
FOR Export
EXEC master..xp_cmdshell 'bcp "SELECT TOP 10 Col1, Col2, Col3 FROM Server.dbo.TableName" queryout C:\Data\File.dat -S Server -T -t"<EOFD>" -r"<EORD>" -w'
Same way I am generating a FORMAT file
EXEC master..xp_cmdshell 'BCP Server.dbo.TableName format nul -S Server -T -w -f "C:\Data\File.fmt" -t"<EOFD>" -r"<EORD>" '
Now when I try importing data back into SQL Server table I am getting error "Invalid character value for cast specification"
Error logs shows me something like this
## Row 1, Column 1: Invalid character value for cast specification ##
?1000 Mytestdataunicoded nothing
Now from where this ? added in starting of my column data is still unknown.
I am able to import successfully when trying importing with format file, also able to import successfully when using switch -c, but for some purposes we must use -w switch to do that.

I am using BCP for import export and getting "Invalid
character value for cast specification" error for only 1(first row of
export) row while trying to import back.
Does the first row of your export file contain column definition information?
If so, use -F2.
https://learn.microsoft.com/en-us/sql/tools/bcp-utility?view=sql-server-ver15#F

When using -w option, I believe BCP ignores any -t or -r option and uses \t and \n and field and row terminators.
From MS docs:
-w Performs the bulk copy operation using Unicode characters. This option does not prompt for each field; it uses nchar as the storage
type, no prefixes, \t (tab character) as the field separator, and \n
(newline character) as the row terminator. -w is not compatible with
-c.

Related

Creating a Format File for Bulk Import

I am trying to create a Format File to bulk import a .csv file but i, am getting an error.
Query I used
"BCP -SMSSQLSERVER01.[Internal_Checks].[Jan_Flat] format out -fC:\Desktop\exported data\Jan_FlatFormat.fmt -c -T -Uasda -SMSSQLSERVER01 -PPASSWORD"
I am getting an error
"A valid table name is required for in, out, or format options."
This is the error. can anyone suggest what need to do.
According to the bcp Utility documentation the first parameter should be a [Database.]Schema.{Table | View | "query"}, so don't put -SMSSQLSERVER01 where you've got it. Also use format nul instead of format out.
Try using:
bcp.exe [Internal_Checks].[Jan_Flat] format nul "-fC:\Desktop\exported data\Jan_FlatFormat.fmt" -c -SMSSQLSERVER01 -T -Uasda -PPASSWORD
Note the quotes " around the -f switch because your path name contains space characters.
Also note that the -c switch causes single-byte characters (ASCII/OEM/codepage with SQLCHAR) to be written out. If your table contains nchar, nvarchar or ntext columns you should consider using the -w switch instead so as to write out UTF-16 encoded data (using SQLNCHAR).

SQL Server Bulk Insert Error 7301 "IID_IColumnsInfo"

I'm trying to insert through a CSV file, which by the way will be executed every day through a procedure, but it gives the same error.
Msg 7301, Level 16, State 2, Line 16
Cannot obtain the required interface ("IID_IColumnsInfo") from OLE DB provider "BULK" for linked server "(null)".
The table I'm trying to import I put all the fields as nvarchar and all of them with at least 500 characters, because I was thinking that this was the problem.
This CSV file I am exporting through PowerShell as follows:
Export-Csv -Path $DirPath -Delimiter ';' -NoTypeInformation -Encoding UTF8
The file has 40 columns and 685 rows, I already tried to save the CSV file with ',' delimiter and ';' delimiter, but both have the same error.
I tried to do the Bulk Insert in several ways as below, but without success.
BULK INSERT DEV_DS_SANTOGRAU..GB_TBIMP_FOTOS_CSV
FROM 'C:\Users\userbi\Desktop\Projetos-Santo-Grau\Projeto1-RelatoriodeEstoque\TBIMP_FOTOS_CSV.csv'
WITH (FORMAT = 'CSV',
--MAXERRORS = 0,
--CODEPAGE = '65001',
CODEPAGE = 'ACP',
--FIELDQUOTE = '"',
FIELDTERMINATOR ='";"',
--ROWTERMINATOR ='"\n"',
ROWTERMINATOR = '\r\n',
--ROWTERMINATOR = "0x0a"
FIRSTROW = 2,
ERRORFILE = 'C:\Users\userbi\Desktop\Projetos-Santo-Grau\Projeto1-RelatoriodeEstoque\TBIMP_FOTOS_CSV_ERROS.csv');
Once he exported a CSV and TXT file with errors, using the code above, the data was like this (but not in the original file):
What should I do?
I would not like it, but if it is possible to ignore these records but the insert is completed, it would be less worse.
Information:
SQL Server 2019 (v15.0.18330.0)
SQL Server Management Objects (SMO) v16.100.37971.0
Microsoft SQL Server Management Studio v18.5
It's usually easier to BULK INSERT data with a format file. Use the bcp.exe utility to create a format file with a command such as the following:
bcp.exe DEV_DS_SANTOGRAU..GB_TBIMP_FOTOS_CSV format nul -c -t; -f C:\Temp\TBIMP_FOTOS_CSV.fmt -S(local) -T
Where:
DEV_DS_SANTOGRAU..GB_TBIMP_FOTOS_CSV is the Database.Schema.Table we're interacting with.
format specifies format file creation mode.
nul specifies the input/output data file, which in this case means "don't write any data".
-c specifies character mode, as opposed to native (binary) mode.
-t; specifies to use ; as the field separator character.
-f C:\Temp\TBIMP_FOTOS_CSV.fmt specifies the path to write the format file to, relative to your local computer.
-S(local) is the SQL Server to connect to, (local) in my case.
-T means Trusted Authentication (Windows authentication), use -uUsername and -pPassword if you have SQL Login authentication instead.
This creates a format file something like the following (yours will have more and different columns):
14.0
2
1 SQLCHAR 0 510 ";" 1 Filename SQL_Latin1_General_Pref_CP1_CI_AS
2 SQLCHAR 0 510 "\r\n" 2 Resolution SQL_Latin1_General_Pref_CP1_CI_AS
Now, in SSMS, you should be able to run something like the following to import your data file (adjust file paths relative to your SQL Server as appropriate):
BULK INSERT DEV_DS_SANTOGRAU..GB_TBIMP_FOTOS_CSV
FROM 'C:\Temp\TBIMP_FOTOS_CSV.csv'
WITH (
CODEPAGE = '65001',
DATAFILETYPE = 'char',
FORMAT = 'CSV',
FORMATFILE = 'C:\Temp\TBIMP_FOTOS_CSV.fmt'
);
-- edit --
On SQL Server and international character support.
SQL Server and UTF-8 has had a bit of a checkered history, only gaining partial support with SQL Server 2016 and really only supporting UTF-8 code pages with SQL Server 2019. Importing and exporting files with international characters is still best handled using UTF-16 encoded files. Adjustments to the workflow are as follows...
In PowerShell, use the Unicode encoding instead of UTF8:
Export-Csv -Path $DirPath -Delimiter ';' -NoTypeInformation -Encoding Unicode
When generating the BCP format file, use the -w switch (for widechar) instead of -c (for char):
bcp.exe DEV_DS_SANTOGRAU..GB_TBIMP_FOTOS_CSV format nul -w -t; -f C:\Temp\TBIMP_FOTOS_CSV-widechar.fmt -S(local) -T
This causes the SQLCHAR columns to be written out as SQLNCHAR, aka. national character support:
14.0
2
1 SQLNCHAR 0 510 ";\0" 1 Filename SQL_Latin1_General_Pref_CP1_CI_AS
2 SQLNCHAR 0 510 "\r\0\n\0" 2 Resolution SQL_Latin1_General_Pref_CP1_CI_AS
When using BULK INSERT specify DATAFILETYPE = 'widechar' instead of DATAFILETYPE = 'char' and specifying a codepage, e.g.:
BULK INSERT GB_TBIMP_FOTOS_CSV
FROM 'C:\Temp\TBIMP_FOTOS_CSV.csv'
WITH (
DATAFILETYPE = 'widechar',
FORMATFILE = 'C:\Temp\TBIMP_FOTOS_CSV-widechar.fmt'
);

Pass byte[] as parameter to sql insert script

I am trying to upload the binary[] of a Zip folder to my database. I used Get-Content -Encoding Byte -ReadCount 0 to read the data into a variable. I want to use this variable in an INSERT statement. Unfortunately, sqlcmd doesn't like the size of the variable, and gives me this error:
Program 'SQLCMD.EXE' failed to run: The filename or extension is too longAt line:1 char:1.
I have tried using the -Q option to run the query, and also -i to run a sql file.
DECLARE #data varbinary(MAX)
SET #data = '$(data_stuff)'
INSERT INTO MyTable
(v1,v2,v3,v4,v5)
VALUES
(v1,v2,v3,v4,#data)
sqlcmd -S servername -E -i .\file.sql -v data = "$binarydata"
Is there a workaround for doing this?
In a SQL query/batch/.sql file, binary/varbinary/image literal data values must be in hexadecimal format with a 0x prefix:
INSERT INTO tableName ( binaryColum ) VALUES ( 0x1234567890ABCDEF )
I don't know what the maximum length of a binary literal is, but I suspect things might stop working, or be very slow, if you exceed more than a few hundred kilobytes.
I recommend using ADO.NET directly via PowerShell, which will also let you use binary parameter values (SqlParameter): How do you run a SQL Server query from PowerShell?

SQL Server BCP Empty File

I'm trying to use bcp to query out a comma-separated-value file but each time I get an empty file.
Here's my bcp command:
bcp "SELECT * FROM ##OutAK " QUERYOUT D:\Outbound\raw\li14090413.raw -c -T -t -S DB1
I have verified that ##OutAK is NOT empty because select count (*) from ##OutAK is not 0. When open file using HEX editor, I see the following:
0D 0A
I found the problem. It seems BCP is "allergic" with NULL. So, I just put ISNULL() to all the null-able fields and the output file is back to normal now.

how to export sql output to excel with text format

I have requirement that, Need to export the sql output data to excel sheet. To acheive this i am executing the below query,
xp_cmdshell'sqlcmd -s -U aaaa -P bbbbb -d dbao -Q " set nocount on;select * from table_tmp" -s "," -o D:\temp_table.xls
The query is executing fine but the issue which i am facing while executing that query is,
I am loosing the extract format of the columns
E.g., 0000012345 is stored as 12345 ( since it is numeric value xls by default removing preceeding zero's.
Is there a way to load the data in excel with format as text.
Thanks.