I have one server with the sql database and manager studio and another server from where I want to execute the bcp command from. Could I use (possibly a .bat file) to generate a .txt file on the same server? Or does anyone have any other suggestions? It is for a person who does not have any knowledge of BD and wants to execute at any time, so I'm not using a job scheduled.
I tried the following, it does not work on the second server but it shows the expected result on the first.
DECLARE #SQL VARCHAR(255), #bcpc VARCHAR(8000)
SET #SQL = 'select * from BaseDatos.dbo.tabla'
SET #bcpc = 'bcp "' + #SQL + '" queryout'
SET #bcpc = #bcpc + ' c:\test\archivo.txt -c -t"|" -Usa -P'
EXEC master..xp_cmdshell #bcpc
print #bcpc
The bcp command you are building does not include the -S option to specify which SQL Server to get data from. I believe, if it's working on your "Server A" this is because BCP must default to the local default instance. However, when you try to run the BCP command on "Server B" you no longer have the same default SQL Server instance (if you have one at all). Try adding the -S option to specify the name of the "Server A" into the BCP command.
Imma double check on the "default" server value if -S is not used.
Yep...
-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.
How about
EXECUTE (master.xp_cmdshell#bcpc) AT server2
If you are getting no result:
1) Is the Basedatos.dbo.tabla on server 2? If not, create a linked server so that you can reference server1.basedatos.dbo.tabla in order for BCP running on server 2 to get the data from Server 1
2) Check all permissions on server 2.
Related
The following connection attempt works!
sqlcmd -S SOMEOTHERPC\SQLEXPRESS,1433 -Q "SELECT * WHERE Lol=1" -d "mydb"
the following does not work, it times out after 10 seconds and fails
sqlcmd -S SOMEOTHERPC\SQLEXPRESS -Q "SELECT * WHERE Lol=1" -d "mydb"
given that 1433 is the default port for sql server, why is it that I have to provide it? used to work without it?
many applications are getting this connection time out issue And i feel it is due to this?
had the default port been changes somewhere in sqlserver config?
EDIT:
Microsoft SQL Server 2014
Windows Embedded Standard 32bit (6.1 Build 7601)
Running this from my SQL Server Management Studuio (SSMS) connecting to Azure sql database. I am using SQLCMD mode of SSMS.
:Connect -S taiobdemo1.database.windows.net -d taiobcustomer -U sqlusername -P strongpw
Getting this error:
A fatal scripting error occurred. Incorrect syntax was encountered
while parsing :Connect.
The :connnect SqlCmd command does not allow you to specify a target database. You'll always connect to your default database. So it's pretty useless in Azure SQL Database, as you'll be connected to Master and won't be able to switch.
eg
:Connect xxxx.database.windows.net -U someuser -P xxxx
select db_name()
Will always output master
So I've been surfing through some of the questions related to my issue but I still can't seem to solve it. I had this working on my own SQL server but as soon as I moved it to a different server I now have an issue.
Here is the bcp out command I am using:
declare #sql varchar(1000)
set #sql = 'bcp DatabaseName.dbo.TableName out C:\'+#name+'.txt -c -t"\t" -T -U"'+#user+'" -P"'+#password+'" -S"'+ ##SERVERNAME +'"'
exec master..xp_cmdshell #sql
But I am getting this in return from the server:
SQLState = 37000, NativeError = 4060
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot open database "DatabaseName" requested by the login. The login failed.
SQLState = 28000, NativeError = 18456
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Login failed for user 'NT AUTHORITY\SYSTEM'.
NULL
I have no idea what I'm doing wrong right now and I've tried doing the fixes I've seen in other threads about it but still nothing.
Edit: I previously used this command on my own server and it worked just fine.
declare #sql varchar(1000)
set #sql = 'bcp DatabaseName.dbo.TableName out C:\'+#name+'.txt -c -t"\t" -T -S'+ ##SERVERNAME
I also removed the -T in my first command and it had a login failed for the user instead of the NT Authority system account. Even though the login is correct.
Solved... I'm stupid.
Options for bcp command line:
-S server name
-T trusted connection
-w wide character type
-t field terminator
bcp AdventureWorks2014.HumanResources.vEmployee out C:\DataFiles\SsisExample\source\EmployeeData.txt -w -t, -S localhost\SqlSrv2014 -T
I'm just starting out with sqlcmd on SQL Server 2005, enterprise edition.
I can connect to my server fine, from the command line:
sqlcmd -SSQLSERVERNAME -Q"select test=1"
However, when I create a junk.sql file that has just this one line:
:connect -SSQLSERVERNAME
...and try to run it by running the following at the command line:
sqlcmd -i C:\junk\junk.sql
...I can't even connect - I get this error:
Named Pipes Provider: Could not open a connection to SQL Server [2].
Sqlcmd: Error: Microsoft SQL Native Client : An error has occurred while establishing a connection to the server. When c
onnecting 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.
From what I read, I thought that using the connect command should work. Am I just using it incorrectly? What I'd like to do is connect in the sql file using the connect command (eventually using variables for different servers) and then run different sql files using the :r command
Thoughts?
thanks,
Sylvia
The :connect command does not need the -S switch, just use
:connect "SQLSERVERNAME"
Okay - I was doing 2 things wrong:
** It appears that when you run sqlcmd, you must be connecting to something from the get-go. I thought I could just do this:
sqlcmd -i C:\junk\junk.sql
but I had to do this
sqlcmd -i -SServerName C:\junk\junk.sql
I didn't have a local default instance running - presumably if you have a local default instance running, there's not a problem.
** Then in the junk.sql, you can do the following
:connect ServerName
note that it's NOT
:connect -SServerName
Hope this helps!
I have a script that connects to SQL Server 2005 default instance. But I'm and being connected and instead got the following message
[SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [53].
[SQL Native Client]Login timeout expired
[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.
The script's content is:
osql -E -S <servername> -i D:\scripts\script1.sql -o D:\scripts\script1.txt
I looked at the network connection from SQL Server Configuration Manager. The TCP/IP is enabled but the Named Pipes is disabled.
My question is what could be the possible cause and solution for this error?
try osql -E -S(local) or osql -E -S. I have not figured out why sometimes it does work, and other times it does not.