How to write using BCP to a remote SQL Server? - sql-server

I have a remote SQL Server with a hostname I am using to connect to.
BCP suggests to use
bcp DBName.dbo.tablename in C:\test\yourfile.txt -c -T -t
However when I try this it does not connect to DBName as that is not a valid alias. I get native error 2.
How to I run BCP but specify an internet / network address to connect to, not an MSSQL server name?

How to I run BCP but specify an internet / network address to connect to, not an MSSQL server name?
You can specify the IP address (here just 127.0.0.1) instead of the server name.
bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1\instance"
If you truly want to go IP\port only then use IP address and port separated by comma
bcp DBName.dbo.tablename in "C:\test\yourfile.txt" -c -T -t -S"127.0.0.1,60905"
Of course in that case you would need to know the port the instance is using, and your instance should be set to use a fixed port

The DBName is the name of the database, you need to use -S option to provide the server (or server\instance) name.
From MSDN:
-S server_name [\instance_name] Specifies the instance of SQL Server to which to connect. If no server is specified, the bcp utility
connects to the default instance of SQL Server on the local computer.
This option is required when a bcp command is run from a remote
computer on the network or a local named instance. To connect to the
default instance of SQL Server on a server, specify only server_name.
To connect to a named instance of SQL Server, specify
server_name\instance_name.

Related

Cannot manage docker-based mysql server from Linux

I installed an mssql server using docker container in an ubuntu EC2 as follows:
sudo docker run --name my_mssql -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<mypassword>" -p 1433:1433 -v /home/ubuntu/db:/var/opt/mssql -d microsoft/mssql-server-linux
sudo netstat -lpnt |grep 1433
shows 1433 is listening
I can login the mssql server using username=sa and password=mypassword using SQLPRO for MSSQL – a database manager, I can then create a database from that manager.
However, if I operate the mssql server from the EC2 in which the mssql server is installed, using the below command,
mysql -h <EC2 IP> -P 1433 -u sa -p
and then provide the password, it just stuck there and finally stop connecting with error message:
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2
Why I cannot operate mssql server from the ubuntu EC2? How can I do that?
You DB server is MSSQL, and you are trying to connect with MSSQL using Mysql client, which will defiantly not work.
Can I use mysql to connect to SQL Server?
microsoft/mssql-server-linux
you can verify accessibility using
telnet EC2_IP 1433
or you can use sqlcmd or MSSQL client to connect with your DB container.
You're using SQL Server, not MySQL. You can't use another database's tools to connect to it.
You need to use SQL Server's command-line tools:
sqlcmd is the CLI query tool, similar to mysql
bcp is used for bulk import/export
You can also use Azure Data Studio (the unfortunate marketing name of what started as SQL Server Operations Studio). ADS can connect to SQL Server directly and PostgreSQL through an extension
It's a cross-platform editor/IDE similar to Visual Studio Code built for querying, visualizing results, managing databases and executing Python scripts and Jupiter Notebooks. It comes with server and database dashboards out of the box. There are several extensions for advanced monitoring and troubleshooting too.
It's also trivial to create new dashboards based on your own queries: Just copy the visualisations JSON definition into a settings file.

Using Perl to connect to MS SQL without use of an ODBC DSN

Is there a way to use Perl to connect to a MS SQL database without the use of ODBC? I have a few scripts running in my production environment which I intend to migrate to perl.
Sample SQLCMD used:
sqlcmd -S <dbHostName> -i <input_file> -o <output_file>
Sample BCP used:
bcp <sqlQuery> queryout <output_file> -c -t, -T -S <dbHostName>
The main reason is that I do not know the password to my production database, and I cannot have knowledge of it. Therefore there is no way I can pass in the pass phrase whether in my perl script or via the ODBC setup.
I notice that your sqlcmd statement does not include -U and -P. This means it is using the logged-in user's credentials to access the database.
To accomplish the same with ODBC & DBI, include Trusted_Connection=True; in the connection string. This is akin to using the "With Windows NT authentication using the network login ID." radio button on the 2nd page of the DSN setup wizard.
For example on my Win 8.1 box, where SQL Server is the name of the relevant driver (not DSN) in my ODBC Data Source Administration interface, I might give the DBI connect method the following string:
dbi:ODBC:Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
In contrast a string using a DSN might look like:
dbi:ODBC:myDSN
FWIW: I find http://connectionstrings.com to be a valuable resource.

Using sqlcmd to capture the data from a remote server and putting the excel file in some other server

I have used sqlcmd to capture the required data from a remote server ,which is coming fine .
I have used the below query in a batch file -
>>"Output_hvac.xls" echo %date% %time%
>>"Output_hvac.xls" SQLCMD ...
Now This batch file is kept on a local machine ,thus when it runs it produces an output _hvac excel file .
I want that this excel file be stored in some other server which is not having sql server installed .The batch has to run on the local machine and the excel sheet to be updated on the server .Now I have access to the server but if i am trying to do this with the following query ,it shows access denied .
>>"//172......./d$/Output_hvac.xls" echo %date% %time%
>>"Output_hvac.xls" SQLCMD ...
Now how would i do this .
You need to formally connect to a remote server when you use the sqlcmd command.
Check out the -S argument:
-S [protocol:]server[\instance_name][,port] Specifies the instance of SQL Server to which to connect. It sets the sqlcmd scripting variable SQLCMDSERVER.
Specify server_name to connect to the default instance
of SQL Server on that server computer. Specify server_name [
instance_name ] to connect to a named instance of SQL Server on that
server computer.
If no server computer is specified, sqlcmd connects
to the default instance of SQL Server on the local computer. This
option is required when you execute sqlcmd from a remote computer on
the network. protocol can be tcp (TCP/IP), lpc (shared memory), or np
(named pipes). If you do not specify a server_name [ \instance_name ]
when you start sqlcmd, SQL Server checks for and uses the SQLCMDSERVER
environment variable.
Full article on msdn.

How to work with BCP

I am connecting to sql server which is in another server from my server.
I am executing the following:
Exec master..xp_cmdshell 'bcp "select * from databasename.dbo.tablename "
queryout "D:\tempexportfile.txt" -c -t, -T -S[Connected servername]'
it is writing the text file in the connected server from my server, but i want to the text file is in my server,
Is there any posibility to specify my current server and write text file here?
On the server that you execute the bcp on should connect via a share drive to your server. Then replace the D:\ with the share drive letter.
Another way is to make sure that you have a share on your server that is accessible from the SQL Server, then replace the D:\ with \yourservername\sharename.
Hope that helps.

Unable to access an instance of SQL Server 2008 R2 remotely

A very mysterious problem coming up :P
I have a server configured with a static IP. I have installed SQL Server 2008 R2 with additional instance (ITAPP). Now When I use the IP to access the SQL Server following things happens:
Client Machine:
sqlcmd -S XXX.XXX.XXX.XXX -U sa -P mypass
Connected Successfully....
But when I use:
sqlcmd -S XXX.XXX.XXX.XXX\ITAPP -U sa -P mypass
HResult 0xFFFFFFFF, Level 16, State 1 SQL Network Interfaces: Error
Locating Server/Instance Specified [xFFFFFFFF].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred
while establishing a connection to the server.
When connecting to SQL Server 2005, this failure may be caused by the
fact that under the default settings SQL Server does not allow remote
connections..
Sqlcmd: Error: Microsoft SQL Native Client : Login timeout expired.
Even on the same machine (where the SQL Server is installed) using SQL Server Management Studio. I have put off my firewall on both machines, even allow all the protocols for ITAPP (Shared Memory, Named Pipes, TCP/IP), also set Allow Remote connection to true.
One thing more when I use 127.0.0.1\SQLITRAX to connect on server machine it connects Immediately.
Please help me out from this mess :)
maybe it's because it's a named instance on a remote server.
Named instance don't use the SQL Server standard Tcp-port 1433, only the default (unnamed) instance use the 1433 port.
Any other "named" instance simply listen on another port.
So you should check in the SQL-Server configurator, on which TCP port is listening, and then tell the client to connect to this port.
Say that your named instance is listening to 12345 port, then the client should connect using the following command
sqlcmd -S XXX.XXX.XXX.XXX,12345 -U sa -P mypass
when you specify the TCP port, then you don't need the name of the instance.
In my understanding the name of the instance is a way for SQL Server services to find the TCP port that this named instance is listening on.
But for this to work your client need to be able to access those other services that resolve the instance name of SQL Server (maybe it's the Sql server Agent / SQL Server browser, but I'm not sure)
Update
Here is a screenshot that show where to set the TPC port for a SQL Server named instance.
so, on which TCP port is your named instance of SQL Server listening?
The SQL Server Browser service needs to be accessible to be able to connect to named instances.
See this article for more info on what it does and which ports to open on your firewall
This technet article provides a handy script to open up all required ports

Resources