I'm getting the error code shown far below when I run the BCP command to import a CSV to an Azure hosted SQL existing table.
I can successfully test the connection using an ODBC user dsn from my PC
There is no ) in my csv file
I've tried with and without headers
I've tried converting the csv to a tab delim file
None of these work and after an two hours searching google, I could use some help...
BCP Command:
bcp us_equities_price_capture in "c:\temp\dev\sql\us_equities_price_capture4.csv" -S mydbserver1.database.windows.net -d mydbinstance -U sql_user1 -P mypassword -q -c -t -h
Error
SQLState = 37000, NativeError = 102
Error = [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Incorrect syntax near ')'.
Related
I have two database servers: LIVE and DB. Both have SQL Server 2017.
If I open SMSS in DB, I can log on to LIVE databases using SQL Server authentication.
My goal is to run BCP from DB to queryout from LIVE into a file on DB.
The command I'm running is:
BCP "Select * from <<DATABASE.dbo.TABLE>>" queryout "D:\data\test.csv" -t^ -r '0x0A'
-U <<USER>> -P <<PASSWORD>> -S "LIVE\MSSQLSERVER" -c -C65001
The problem is that for some reason I'm not connecting successfully:
SQLState = 08001, NativeError = 87
Error = [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces:
Connection string is not valid [87].
SQLState = 08001, NativeError = 87
Error = [Microsoft][ODBC Driver 13 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired
Am I missing something? If on DB, I attempt to connect to LIVE using SMSS, it's ok, but bcp results in not found errors?
Thanks!
I see you're using the MSSQLSERVER as instance name when you're trying to connect to LIVE here:
BCP "Select * from <<DATABASE.dbo.TABLE>>" queryout "D:\data\test.csv" -t^ -r '0x0A'
-U <<USER>> -P <<PASSWORD>> -S "LIVE\MSSQLSERVER" -c -C65001
******************
Normally, MSSQLSERVER is the internal "instance" name of the default, unnamed instance and you connect to such a default instance without providing any instance name - e.g. using just LIVE instead of LIVE\MSSQLSERVER.
Try this BCP command:
BCP "Select * from <<DATABASE.dbo.TABLE>>" queryout "D:\data\test.csv" -t^ -r '0x0A'
-U <<USER>> -P <<PASSWORD>> -S "LIVE" -c -C65001
I am having quite a few problems making MSSQL drivers work in Ubuntu. I have followed the following tutorial to make sqlcmd work in Ubuntu 16.04.
# In CMD:
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
1> select name from sys.databases
2> go
After installing the same tool in Ubuntu, it seems to work, but it timeouts when attempting to connect to the same database:
# In Ubuntu bash
sqlcmd -S my_server_name -U my_username -P my_password -d my_database
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : Login timeout expired.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : TCP Provider: Error code 0x2AFA.
Sqlcmd: Error: Microsoft ODBC Driver 17 for SQL Server : A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online..
I have tried to change configurations of the database, such that the port is static at 1433, but still no luck.
Do you have any suggestions?
I have fixed the problem!
1. Install MSSQL drivers on Ubuntu
Follow this tutorial.
2. Ensure your port on the database is static
Follow this tutorial to set up a static port.
3. Identify the IP adress of your database
I had to call the following code on the database to get the ip-adress: local_net_address.
SELECT
+ CONNECTIONPROPERTY('net_transport') AS net_transport,
+ CONNECTIONPROPERTY('protocol_type') AS protocol_type,
+ CONNECTIONPROPERTY('auth_scheme') AS auth_scheme,
+ CONNECTIONPROPERTY('local_net_address') AS local_net_address,
+ CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port,
+ CONNECTIONPROPERTY('client_net_address') AS client_net_address
4. Connect to database (in Ubuntu bash)
Here are two examples with netcat and sqlcmd.
# Using MSSQL tool
sqlcmd -S my_server_ip_adress//my_server_name,my_port -U my_username -P my_password -d my_database
# Using netcat
nc -z -v -w5 my_server_ip_adress my_port
Just installed sql-server on Ubuntu 16.04, 2 days ago. Using sqlcmd for bulk insert i got:
Msg 4860, Level 16, State 1, Line 6 Cannot bulk load. The file
"~/test_data.txt" does not exist or you don't have file access rights.
Yes the file did exist, i made sure of it using the command cat.
Then i tried bcp tool, but i got:
SQLState = S1000, NativeError = 0 Error = [Microsoft][ODBC Driver 13
for SQL Server]Unable to open BCP host data-file
Also tried installing visual studio code and adding mssql extension but i got the same "file access rights" warning. And already used the chmod 777 trying to fix it. Didn't work.
Command bulk insert sqlcmd:
BULK INSERT TestEmployees FROM '~/test_data.txt'
WITH(
rowterminator = ','
);
Command on bcp tool
bcp auth in path/auth2.tsv -S localhost -U sa -P <my password> -d Trabalho1BD -c
I think your problem is the '~/test_data.txt' part of your bulk insert command. Specifically, that says "find a file called test_data.txt in the home directory". But whose home directory? Not yours! It's looking for the file in the home directory of the account that's running your SQL Server. Try changing that to a full path (i.e. '/home/«username»/test_data.txt' and that should do it.
I am trying to execute a large SQL Server script file from the command prompt as its not loading in Management Studio. I am using this command
sqlcmd -S .\SQLEXPRESS -U ttandel -P ''
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
Note: password is blank. I have tried all the options for keeping password as blank such as ("",'', ) but nothing is working.
Can anyone please suggest how to do this?
From comment:
I tried with this
sqlcmd -S .\SQLEXPRESS -E
-d [Zen.Databases.Suite]
-i D:\NewMachine\COM.B_Address.Table.sql
This is throwing an error
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Login failed for user 'NAPG\ttandel'..
Sqlcmd: Error: Microsoft ODBC Driver 13 for SQL Server : Cannot open database "[Zen.Databases.Suite]" requested by the login. The login failed..
My Server Security information as below.
The following SQL command will do the work.I have used it multiple times for installing 100 million to 200 million records in DB.
sqlcmd -S Krishneil-PC -E -i C:\Users\Krishneil\Desktop\Script.sql
change Krishneil to your suitability.
Change -d [Zen.Databases.Suite] to -d "Zen.Databases.Suite"
I have to use bcp command-line tool to export data from an SQL Server database to a file in a Red Hat server.
I am (apparently) using valid statements but bcp is not producing any kind of output/results.
However, when I execute statements with missing or invalid parameters it displays the respective error.
I am looking for the reason of this issue (e.g. defective installation, bad usage of bcp, lack of permissions or any other known conflict) and how to fix it.
bcp statement:
bcp fully_qualified_table_name out ./data.txt -c -S server -U user -P password
bcp usage:
usage: /opt/microsoft/bin/bcp {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-q quoted identifier]
[-t field terminator] [-r row terminator]
[-a packetsize] [-K application intent]
[-S server name or DSN if -D provided] [-D treat -S as DSN]
[-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-d database name]
bcp version:
BCP - Bulk Copy Program for Microsoft SQL Server.
Copyright (C) Microsoft Corporation. All Rights Reserved.
Version: 11.0.2270.0
SQL Server version (SELECT ##VERSION):
Microsoft SQL Server 2012 - 11.0.5058.0 (X64)
May 14 2014 18:34:29
Copyright (c) Microsoft Corporation
Enterprise Edition: Core-based Licensing (64-bit) on Windows NT 6.3 <X64> (Build 9600: ) (Hypervisor)
Distribution:
Red Hat Enterprise Linux 6.7 (KornShell).
Invalid statements with respective error message (examples).
bcp THAT_TUB_ACE.oh_nerd.table_name out ./data.txt -c -S sr._bear -U you_sr. -P pass_sword
SQLState = S1T00, NativeError = 0
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]Login timeout expired
SQLState = 08001, NativeError = 11001
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
SQLState = 08001, NativeError = 11001
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server]TCP Provider: Error code 0x2AF9
...
bcp fully_qualified_table_name ./data.txt -c -S valid_server -U valid_user -P bad_word
bcp fully_qualified_table_name out ./data.txt -c -S valid_server -U valid_user -P bad_word
SQLState = 28000, NativeError = 18456
Error = [unixODBC][Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Login failed for user 'valid_user'.
SUMMARY.
The objective is to generate a datafile using the following syntax (or similar):
bcp fully_qualified_table_name out ./data.txt -c -S server -U user -P password
The facts are:
When running a valid bcp statement there's nothing in the window at all (no output) and no datafile is created.
I cannot use option -T (trusted connection using integrated security) for bcp so I have to specify the server, user and password.
Tried queryout option on a very simple small table already but still no luck.
Credentials are valid, I successfully tested them using sqlcmd like the following: sqlcmd -S server -U user -P password -Q 'SELECT * FROM really_small_table'.
The bcp statements under "Invalid statements with respective error message (examples)" section of this question are just examples of invalid statements to show that bcp actually does something but giving the expected results.
Hopefully you've already solved your problem, but I had a similar problem configuring PyODBC for Python 3.4 on RedHat 7.1. I was trying to connect to Microsoft SQL Server 2016. Hopefully this will help someone else.
The real problem for me was ODBC/FreeTDS configuration. Here are the installation steps I took and the eventual solution.
I started by installing Microsoft's ODBC driver for redhat. This likely isn't required, but I list it here since I never removed it from my redhat machine for fear of breaking ODBC.
Install ODBC and ODBC development files: yum install unixODBC-devel.x86_64
Install FreeTDS: yum install freetds.x86_64
Update config files. Steps 1-3 took about 10 minutes. I spent a very frustrating day trying various configurations, and usually I was able to connect via tsql (FreeTDS debugging tool, more on that below), but never via Python. I eventually stumbled upon this blog, which pointed me to the correct driver via ldconfig -p | grep libtdsodbc (I believe this points to the FreeTDS driver, hence why Microsoft's driver probably isn't needed).
The eventual configuration I ended up with is as follows:
/etc/odbc.ini (note: I had to create this file as the ODBC/FreeTDS installation process didn't create it for me.)
[SQLServer]
Description = TDS driver (Sybase/MS SQL)
Driver = SQLServer
Servername = your_sql_hostname
TDS Version = 0.95
Database = your_database_name (not to be confused with instance name)
Port = 1433
/etc/odbcinst.ini
[ODBC]
# Enables ODBC debugging output. Helpful to see where things stop working.
Trace = yes
TraceFile = /etc/odbcinst.trace
[SQLServer]
Description=TDS driver (Sybase/MS SQL)
Driver=/lib64/libtdsodbc.so.0
Driver64=/lib64/libtdsodbc.so.0
UsageCount=1
Troubleshooting tips
tsql is debugging/testing tool that comes with FreeTDS. It is your friend. I was able to use it early on to verify that the networking side of things was correct (i.e. no firewalls blocking access, hostnames resolve, etc).
Use tsql -H your_sql_hostname -L to get the instance names and ports from your SQL server. Note that you'll use port 1433 (which is the MS SQL instance discovery port) in your ODBC configuration above, not these port numbers.
ServerName your_sql_hostname
InstanceName instance_1_name
IsClustered No
Version 13.0.x.x
tcp 5555
ServerName your_sql_hostname
InstanceName instance_2_name
IsClustered No
Version 13.0.x.x
tcp 6666
Use the port from the previous tsql command like so:
tsql -H your_sql_hostname -p your_sql_instance_port -D your_database_name -U your_username -P your_password
You should get a prompt. Try querying some data from your database (note that you have to type GO on a separate line to actually run your query). If this works but you still can't connect via Python/other-app, it usually means ODBC/FreeTDS are installed correctly and there are no networking problems, but your ODBC configuration isn't correct.
1> SELECT TOP 1 * FROM your_table
2> GO
{a row will be returned here}
1> # prompt returns, use `exit` to get out