I have mssql-server and mssql-tools installed on Ubuntu (Linux). When I try to export data with the bcp command using the following command-line:
bcp DBname.dbo.Täble_Name out Täble_Name -c -k -S127.0.0.1 -Usa -PpassWord -r ~
I get this error:
SQLState = 37000, NativeError = 102
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near '�'.
The �
is ä
.
If I surround the Täble_Name
with square-brackets:
bcp DBname.dbo.[Täble_Name] out Täble_Name -c -k -S127.0.0.1 -Usa -PpassWord -r ~
I get this error on the object name:
SQLState = S0002, NativeError = 208
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'DBname.dbo.Täble_Name'.
I went further and added single-quotes ''
along with the -q
option (which enables Quoted Identifiers):
bcp 'DBname.dbo.[Täble_Name]' out Täble_Name -c -k -S127.0.0.1 -Usa -PpassWord -r ~ -q
The error becomes :
SQLState = S0002, NativeError = 208
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid object name 'DBname.dbo.T�ble_Name'.
NB: the command works perfectly with table names without this special character ä .
Best Answer
I believe that this is an encoding issue between the shell and bcp / SQL Server. SQL Server expects UTF-16 Little Endian, but Linux isn't using that. The default for my Linux VM is UTF-8 via
en_GB.UTF-8
.<TL;DR> Use the "queryout" bcp command and specify
"SELECT * FROM ..."
instead of using the "out" command and simply supplying a table name.What follows is my testing...
I got the list of available locales / encodings using:
returned:
I tried several of those options by setting:
and then trying again using:
Nothing seemed to make a difference. And each time I tried with various combinations of square brackets without
-q
and then with-q
, and then no square brackets both with and without-q
.I even tried injecting the bytes that would equate to the UTF-16 LE character of
ä
via$'\xe4\x00'
and even$'\xe4'$'\x00'
, but no improvements.HOWEVER,
what did work was changing the bcp command from
out
to instead bequeryout
, and then changing the table name to be part of aSELECT
statement (I removed the-r ~
switch only here to make the command line not scroll horizontally, but it was in my testing). I created the table in[tempdb]
and ran the following:No problems there. But interestingly enough, I changed the accented
ä
to a non-accenteda
:and received the following error:
That is an error from bcp and must be referring to the meta-data of
tempdb
since the only column in my test table uses theINT
datatype.Now, my instance-level Collation is accent-sensitive, so I wasn't really expecting the non-accented
a
to work (though I did expect an "invalid object" error). So, in order to test accent-insensitivity, I created a new Database with a Collation ofLatin1_General_100_CI_AI_KS_WS_CS
, create that same table in the new DB, and added a few rows. I then ran the following two tests:and both worked!
Going back to the initial bcp command of just specifying the table name instead of a query, I was able to get
ImportTest.dbo.[Table_Name]
andImportTest.dbo.Table_Name
to work. However, I was still not able to get any combination ofImportTest.dbo.[Täble_Name]
to work; all variations got the same errors as before.