This is the error that I am getting while executing bcp command:
SQLState = 07006, NativeError = 0 Error = [Microsoft][SQL Server
Native Client 11.0][SQL Serve owed from the given field type 0x3D for
field 9 to column 9.
command used is :
bcp "VM_Asia.dbo.dim_budget" in C:\BCP_DATA_INDIA\datafile\dim_budget.dat -f C:\BCP_DATA_INDIA\formatcorrected\Dim_budget.fmt -T
And my fmt file is like this:
11.0
12
1 SQLINT 0 4 "" 1 budget_SK ""
2 SQLINT 1 4 "" 2 country_sk ""
3 SQLINT 0 4 "" 3 budget_ID ""
4 SQLNCHAR 2 36 "" 4 budget_Type Latin1_General_CI_AI
5 SQLNCHAR 2 200 "" 5 budget_Description Latin1_General_CI_AI
6 SQLDATE 1 3 "" 6 budget_Applied_From ""
7 SQLDATE 1 3 "" 7 budget_Applied_To ""
8 SQLDATETIME 1 8 "" 8 Inserted_Date ""
9 SQLDATETIME 1 8 "" 9 Modified_Date ""
10 SQLINT 1 4 "" 10 batchno ""
11 SQLINT 1 4 "" 11 Status ""
12 SQLINT 1 4 "" 12 company_id ""
What version are you on? Possible explanation here. https://msdn.microsoft.com/en-us/library/bb677301.aspx
SQL Server Native Client (SNAC) is not supported beyond SQL Server 2012. Avoid using SNAC in new development work, and plan to modify applications that currently use it. The Microsoft ODBC Driver for SQL Server provides native connectivity from Windows to Microsoft SQL Server and Microsoft Azure SQL Database.
Related
My CSV has text like this:
Côté fenêtres,
carré
I'm trying to open this CSV file using openrowset in SQL Server like below:-
select * from openrowset(BULK 'C:\Import_Orders\Files\PO.csv',
FORMATFILE = 'C:\Import_Orders\Format\Cust_441211.fmt.txt') as PO
But the result is like this:
C+¦t+¬ fen+¬tres,
Carr+¬
How can I tackle this issue? Let me know if I need to add anything more to this question.
SQL Version -
Microsoft SQL Server 2017 (RTM-CU29-GDR) (KB5014553) - 14.0.3445.2 (X64)
This is the format file:-
11.0
8
1 SQLCHAR 0 250 "|" 1 PARTNO ""
2 SQLCHAR 0 250 "|" 2 CODE ""
3 SQLCHAR 0 250 "|" 3 PRICEKG ""
4 SQLCHAR 0 250 "|" 4 FOOTKG ""
5 SQLCHAR 0 250 "|" 5 LENGTH ""
6 SQLCHAR 0 250 "|" 6 QTY ""
7 SQLCHAR 0 250 "|" 7 COLOR ""
8 SQLCHAR 0 250 "\r\n" 8 TOTKG ""
(1) You can try to add an additional parameter CODEPAGE = '65001' to specify a code page to support UNICODE characters.
(2) Use may try to use SQLNCHAR data type instead of SQLCHAR in the format file. For a text file you should always specify SQLCHAR for all fields, unless you have a Unicode file in in UTF‑16 encoding in which case you should use SQLNCHAR.
SQL
SELECT * FROM openrowset(BULK 'C:\Import_Orders\Files\PO.csv',
FORMATFILE = 'C:\Import_Orders\Format\Cust_441211.fmt.txt',
CODEPAGE = '65001') as PO;
I have the following csv I wish to import into my db
"LE";"SOURCE";"VAR_SCTARGET_NAME"
"B";"A/K";"A/K"
"A";"A/B";"A/B"
"A";"A/B";"A/C"
I arranged the following format file
10.0
3
1 SQLCHAR 0 0 "\";\"" 1 A ""
2 SQLCHAR 0 0 "\";\"" 2 B ""
3 SQLCHAR 0 0 "\"\r\n\"" 3 AA ""
which works just fine, if it weren't for the last line. The output in my db is the following
LE SOURCE VAR_SCTARGET_NAME
B A/K A/K
A A/B A/B
A A/B A/C"
How can I remove the quote on the last row? I'm working on a SQL Server platform, if it can be of any help.
I'm beginner in SQL Server, when I write this query:
select ANUMBER
from CDRTABLE
it shows me data, but I want to add new column to result change that query to this:
select '028', ANUMBER
from CDRTABLE
This query adds a new column to query result, so I write this bcp query for saving results to a text file:
EXEC xp_cmdshell 'bcp "SELECT rtrim(ltrim(ANUMBER)),rtrim(ltrim(BNUMBER)),rtrim(ltrim(DATE)),rtrim(ltrim(TIME)),rtrim(ltrim(DURATION)) FROM [myTestReport].[dbo].[CDRTABLE]" queryout f:\newOUTPUT.txt -S DESKTOP-A5CFJSH\MSSQLSERVER1 -Umyusername -Pmypassword -f "f:\myFORMAT.fmt" '
and my format file is this:
9.0
5
1 SQLNCHAR 0 5 "," 1 ANUMBER ""
2 SQLNCHAR 0 10 "," 2 BNUMBER ""
3 SQLNCHAR 0 10 "," 3 DATE ""
4 SQLNCHAR 0 10 "," 4 TIME ""
5 SQLNCHAR 0 10 "\r\n" 5 DURATION ""
Everything is ok, but I want add new column to bcp result, for example add '028' to bcp query result. How can I do that? Thanks.
Because it looks like you're adding a character string to the front of the select, something like this should work:
9.0
6
1 SQLCHAR 0 3 "," 1 NEWCOLUMN "SQL_Latin1_General_CP1_CI_AS"
2 SQLNCHAR 0 5 "," 2 ANUMBER ""
3 SQLNCHAR 0 10 "," 3 BNUMBER ""
4 SQLNCHAR 0 10 "," 4 DATE ""
5 SQLNCHAR 0 10 "," 5 TIME ""
6 SQLNCHAR 0 10 "\r\n" 6 DURATION ""
See https://msdn.microsoft.com/en-us/library/ms191479.aspx for more details on the format of the format file.
I am having a .dat file which I have to upload on my SQL Server 2012 database. The table is as follows:
Primary Key TAB_KEY(bigint, not null)
SESSION_KEY (varchar(32),null)
HIT_KEY(varchar(32),null)
NAME(nvarchar(256),null)
VALUE(nvarchar(1024),null)
SESSION_TIMESTAMP(datetime,null)
The data file is like this in the .dat file:
NOTE : When attempting to import below these datas via BCP I get an error
Column 3: String data, right truncation
BTW Column 3 is NAME column in the column.
Sample data for Column 3 (Name Column field) :
_2__Kart_Ücreti_Yans_t_l_rken_180_Gün_Aktiflik_Kontrolü_Yap_lmal_d_r__Kart_ücreti_yans_rken__kart_n_en_son_hangi_tarihte_al__veri__nakit_çekim_veya__Axess_kartlarda__chip_para_harcamas__yap_ld____kontrol_edilecektir__E_er_günün_tarihi_ve_bu_son_aktiflik_t
Format file :
9.0
5
1 SQLCHAR 0 32 "\t" 2 SESSION_KEY RAW
2 SQLCHAR 0 32 "\t" 3 HIT_KEY RAW
3 SQLCHAR 0 512 "\t" 4 NAME RAW
4 SQLCHAR 0 1024 "\t" 5 VALUE RAW
5 SQLCHAR 0 24 "\r\n" 6 SESSION_TIMESTAMP ""
Error message :
Starting copy... SQLState = 22001, NativeError = 0 Error = [Microsoft][SQL Server Native Client 11.0]String data, right truncation SQLState = 22001,
My BCP command is :
bcp TLWEB.dbo.TLWEB_URLFIELD_8X in BulkUrlField8x.20141209_000000_20141209_235959.cxconnect_2_1418121388.1418121852_10032_1.dat -F 2 -b 250000 -m 50 -a 32000 -U username -P xxxxx -S ServerName\InstanceName,Port_Number -f UrlField8x.fmt
I've thoroughly searched Stack Overflow as well as many other resources but still have an issue. Here's my export script, running under Cygwin:
#!/usr/bin/env bash
#-*- coding: cp1255; -*-
bcp "declare #billing_types table(k int null, t varchar(14)
collate SQL_Latin1_General_CP1255_CI_AS)
insert #billing_types
values (null, 'לא פעיל')
,(1, 'אשרי')
,(2, 'צ׳ק')
,(3, 'הוראת קבע')
declare #standing_order_status table(i int null, s varchar(14)
collate SQL_Latin1_General_CP1255_CI_AS)
insert #standing_order_status
values (null, 'אין')
,(4, 'מבותל')
,(3, 'לא מאושר')
,(2, 'ממתין')
,(1, 'מאושר')
select billing_company_id
,internal_company_name
, t collate SQL_Latin1_General_CP1255_CI_AS as payment_type_string
,isnull(company_email, '') collate SQL_Latin1_General_CP1255_CI_AS as email
,company_fax
,company_address
,company_comments
,invoice_send_with_details
,invoice_send_fax
,invoice_print
,cc_name
,cc_number
,cc_cvv
,cc_id
,cc_expire
,bank_number
,bank_branch
,bank_account
,bank_hoshen
,s collate SQL_Latin1_General_CP1255_CI_AS
from billing_companies
join #billing_types bt on bt.k = payment_type
join #standing_order_status os on os.i = bank_standing_order_status" \
queryout billing-companies.csv -t"," -r"\n" -S server -T \
-U user -P password -f ./billing-companies.fmt
Here's the format file:
9.0
20
1 BIGINT 0 1 "" 1 billing_company_id ""
2 VARCHAR 0 1000 "" 2 internal_company_name SQL_Latin1_General_CP1255_CI_AS
3 VARCHAR 0 14 "" 3 payment_type_string SQL_Latin1_General_CP1255_CI_AS
4 VARCHAR 0 200 "" 4 email SQL_Latin1_General_CP1255_CI_AS
5 VARCHAR 0 100 "" 5 company_fax SQL_Latin1_General_CP1255_CI_AS
6 VARCHAR 0 4000 "" 6 company_address SQL_LATIN1_GENERAL_CP1255_CI_AS
7 NTEXT 0 1 "" 7 company_comments SQL_LATIN1_GENERAL_CP1255_CI_AS
8 BIT 0 1 "" 8 invoice_send_with_details ""
9 BIT 0 1 "" 9 invoice_send_fax ""
10 BIT 0 1 "" 10 invoice_print ""
11 VARCHAR 0 200 "" 11 cc_name SQL_LATIN1_GENERAL_CP1255_CI_AS
12 VARCHAR 0 50 "" 12 cc_number SQL_LATIN1_GENERAL_CP1255_CI_AS
13 VARCHAR 0 50 "" 13 cc_cvv SQL_LATIN1_GENERAL_CP1255_CI_AS
14 VARCHAR 0 50 "" 14 cc_id SQL_LATIN1_GENERAL_CP1255_CI_AS
15 VARCHAR 0 50 "" 15 cc_expire SQL_LATIN1_GENERAL_CP1255_CI_AS
16 VARCHAR 0 100 "" 16 bank_number SQL_LATIN1_GENERAL_CP1255_CI_AS
17 VARCHAR 0 100 "" 17 bank_branch SQL_LATIN1_GENERAL_CP1255_CI_AS
18 VARCHAR 0 100 "" 18 bank_account SQL_LATIN1_GENERAL_CP1255_CI_AS
19 INT 0 1 "" 19 bank_hoshen ""
20 varchar 0 14 "" 20 standing_order_status SQL_LATIN1_GENERAL_CP1255_CI_AS
The collation matches the collation in the database. When I run the query in the studio, I'm getting the expected result, no warnings.
Perhaps here's a caveat: the database uses single-byte prehistoric Hebrew encoding... and I'm not sure whether Cygwin, or anyone later after it isn't trying to convert between encodings. However I've doublechekced that I did my part properly. I.e. the script file is itself in cp-1255.
It works if I remove all mentions of Hebrew from the script. So, I'm guessing this must be the problem, however, I've no idea of how would I solve it.
Have you tried using the -C switch to specify a specific code page?
Here's the BCP syntax page from Books Online:
http://msdn.microsoft.com/en-us/library/ms162802.aspx
Looking here, it looks like you may want to use code page 1255:
http://msdn.microsoft.com/en-us/library/ms186356(v=sql.105).aspx
HTH.