SQL Server bcp with stored procedure - sql-server

I'm struggling with bcp. If I run the following SQL
exec xp_cmdshell 'bcp "select a.displayname, a.samaccountname, a.mail, a.title, a.givenname, a.sn from web_repository.dbo.activedirectory a ORDER BY displayname" queryout "C:\phonegapdownload\test.dat" -N -S IISSERVER -T '
it works fine, however if I run this
exec xp_cmdshell 'bcp "EXEC SelectAllFromADNoJoin" queryout "C:\phonegapdownload\test.dat" -N -S IISSERVER -T '
which has the exact same SQL in it, it throws an error
SQLState = 37000, NativeError = 2812
// not sure where I got this error from
It's running on SQL Server 2008 R2
Can anyone help?
I have checked that SQL Server allows remote connections and that tcpip is enabled
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Server Native Client 10.0]Named Pipes Provider: Could not open a connection to SQL Server [53].
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Server Native Client 10.0]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][SQL Server Native Client 10.0]Login timeout expired

Posting the full text of the error message would help. Error 2812 is "Could not find stored procedure". You will want to specify the -d option to specify your database, or use a quoted name (MyDB.dbo.SelectAllFromADNoJoin).

Related

Pyodbc: Login timeout expired

I am trying to connect to MS SQL Server using pyodbc on local system and on connect to instance i get error:
[2020-06-21 15:39:04.110750]: ('08001', '[08001] [Microsoft][ODBC Driver 13 for SQL Server]SQL Server Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. (-1) (SQLDriverConnect); [08001] [Microsoft][ODBC Driver 13 for SQL Server]Login timeout expired (0); [08001] [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. (-1)')
My SQL Server is Express 2005 and try with drivers:
{ODBC Driver 13 for SQL Server}
{ODBC Driver 17 for SQL Server}
{SQL Native Client}
I test in sqlcmd with this command:
C:\Users\Moein>sqlcmd -S '.\Moein' -U 'sa' -P 'xxxx'
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.
My Code :
import pyodbc
conn = pyodbc.connect(f'DRIVER={SQL Native Client};SERVER=.\Moein;DATABASE=Moein;UID=sa;PWD=xxxx',autocommit=True)
cursor = conn.cursor()
More:
Test -l 600 switch for login timeout more in sqlcmd > not answer recived
Test computername\instance and 127.0.0.1\instance > Not changed
Try full reinstall sql server on local > noting changed things
i found my problem
i use this connection string with pipe mode:
conn = pyodbc.connect('DRIVER={ODBC Driver 13 for SQL Server};ServerName=Provider=SQLOLEDB.1;SERVER=.\testsv;DATABASE=test;Persist Security Info=False;UID=sa;PWD=xxxxx;Data Source=np:\\.\pipe\MSSQL$testsv\sql\query',autocommit=True)

Suddenly getting "Login timeout expired" error when connecting to SQL Server

Other people have asked this question, but I didn't find an answer that specifically addresses the problem I was getting nor that solved the issue. So I'm posting the question and answer.
A few days ago I had a couple automated scripts that connect to SQL Server and export some data, suddenly stop working. They've been running fine for like 7 years, so that this was odd. Both scripts use different methods to connect. One uses a Perl library, the other uses the bcp command. But both were getting the same error. I'll just show the bcp command for brevity. This is all running on Windows Server 2008, with SQL Server 2008 R2, v10.50. This is what I was running:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" "<my query>" queryout "<output file name>" -T -S MYSERVER -c -k
This is the error I was getting:
SQLState = 08001, NativeError = 10061
Error = [Microsoft][SQL Server Native Client 10.0]TCP Provider: No connection could be made because the target machine actively refused it.
SQLState = 08001, NativeError = 10061
Error = [Microsoft][SQL Server Native Client 10.0]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][SQL Server Native Client 10.0]Login timeout expired
The solution was pretty simple. I just added the port number to the server name (changed MYSERVER to MYSERVER,1433). So my final bcp command looked like:
"C:\Program Files\Microsoft SQL Server\100\Tools\Binn\bcp.exe" "<my query>" queryout "<output file name>" -T -S MYSERVER,1433 -c -k
I'm not sure what changed to require including the port. But that's all it took.
It's kinda weird that this isn't just the default port. You'd think bcp would use that without needing to explicitly set it. Though, this could just be some weird server configuration thing that suddenly requires the port.

bcp cannot connect to AWS SQL Server but SSMS can

Anyone know a good reason why bcp cannot connect to a sql server hosted by AWS while SSMS can?
I have double checked the server and user account details and they both match.
I'm using the generic command to import a csv file:
bcp DB_Name.dbo.Table in "somefile_file.csv" -c -S xxx.rds.amazonaws.com -U username -P xxx -b 1000
The error is:
SQLState = 08001, NativeError = 53
Error = [Microsoft][ODBC Driver 13 for SQL Server]Named Pipes Provider: Could not open a connection to SQL Server [53].
SQLState = 08001, NativeError = 53
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
Is bcp using a different port maybe?
The first error shows that bcp uses the Named Pipes protocol to connect to the server. Apparently, this protocol is inaccessible for network connections.
On the client workstation where bcp is run, check SQL Server Configuration Manager and ensure that, in the client configuration list TCP/IP is enabled and prioritised. Strangely enough, bcp doesn't have a command line switch for protocol selection, so this is the only way to manage it I can think of.
P.S. Another option is to specify the server address in the form ip_address,port_number, but I don't think you will find this option particularly attractive.

bcp error with sql server

I have created a stored procedure in sql server 2005 to export data from a query to a network path. The user is a sql user and it is also created in the AD. The server that I am logged in is a remote server, with sysadmin privilege. I am encountering the following error while executing the procedure or even the query:
output
SQLState = 08001, NativeError = 53
Error = [Microsoft][SQL Native Client]Named Pipes Provider: Could not open a connection to SQL Server [53].
SQLState = 08001, NativeError = 53
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.
SQLState = S1T00, NativeError = 0
Error = [Microsoft][SQL Native Client]Login timeout expired
NULL
It looks like I have no access to the remote server, while I am executing the query while I am logged in the server. The procedure also executes automatically at 01.30 am. The command that is executed is a bcp command :
DECLARE #SQLCommand [varchar](max)
,#Query [varchar](max)
,#SERVER VARCHAR(1000)
SET #Query = 'SELECT * FROM INFINITY_SYSTEM.[dbo].[CTI_WFM_LILO_View]'
SET #SERVER = '[server_name]'
SET #SQLCommand = 'EXEC xp_cmdshell ''bcp "' + #Query + '" queryout "\\network_path\lilo'+CONVERT(VARCHAR,GETDATE(),112)+'.csv" -Uusername -Ppassword -c -t, -r, -S"'+#SERVER+'"''' --also tried with -T in stand of the -Uusername -Ppassword
EXEC (#SQLCommand)
The command works when I try it from a local DB in my local sqlserver to the same path.
Thanks
Make sure that you can telnet to the remote server. I would also try to connect to the remote server via sqlcmd. You may have a firewall blocking the connection. Also the sql server services will need permissions to the share/directory, in order for you to write a file to it.
Another thing you should consider is using an SSIS package to export the data to the remote share. Generally is not a great practice to use xp_cmdshell to export the data. You can do everything you are trying to do via a simple SSIS package. As an added benefit this also minimizes security risk because you can disable xp_cmdshell.

incoherence in bcp command execution

I execute a bcp command in 2 computer. The first computer has windows xp and second one has windows server 2003. when I execute command in first, the bcp run successfully but when I run this on second one, occur this error:
Starting copy...
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]Code page 720 is not supp
orted by SQL Server
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC SQL Server Driver]Unable to resolve column level collat
ions
BCP copy out failed
what is the problem?
You might take a look at the following article and play with the switches in you BCP statement.
Copying Data Between Different Collations

Resources