bcp error with sql server - 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.

Related

Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot open server 'azurserver' requested by the login

I am connecting to Azure from SQL Server.
Task: Exporting Azure SQL Database data using bcp from SQL Server 2008 R2.
Step 1: Added IP Address in firewall settings in Azure.
Step 2: Running following script from SQL Server 2008 R2:
EXEC sp_configure N'show advanced options', 1
RECONFIGURE WITH OVERRIDE;
EXEC sp_configure N'xp_cmdshell', 1
RECONFIGURE WITH OVERRIDE;
DECLARE #SQL NVARCHAR(MAX) = ''
SET #SQL = N'Execute xp_cmdshell ''bcp "SELECT * FROM Table;" queryout "D:\AzureTableData.txt" -c -t#_# -S azure.database.windows.net -d DatabaseName -U user1#azure.database.windows.net -P *****"''';
NOTE: IP address is not static at my system and because of this reason, I keep getting an error:
Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]Cannot open server 'azurserver' requested by the login. Client with IP address 'XXX.XXX.XXX.XX' is not allowed to access the server. To enable access, use the Windows Azure Management Portal or run sp_set_firewall_rule on the master database to create a firewall rule for this IP address or address range. It may take up to five minutes for this change to take effect.
I can't configure firewall settings every time at Azure Portal.
You mean that Add client IP doesn't work for you, do you?
If you want to connect to the Azure SQL database for one time, you can set the firewall IP range from 0.0.0.0 to 255.255.255.255.
But for security reasons, we don't suggest to do that.
Azure SQL database could not set the dynamic IP firewall for now.
Hope this helps.

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.

Issues connecting from SQL Server to MS Access database

Environment: Windows Server 2016, SQL Server 2016; MS Access .accdb files (various versions)
Attempting to establish a linked server via oledb to an .accdb file content like this:
EXEC master.dbo.sp_addlinkedserver
#server = N'redacted',
#srvproduct = N'',
#provider = N'Microsoft.ACE.OLEDB.16.0',
#datasrc = N'D:\AccessTest\redacted.accdb',
#provstr = N'Jet OLEDB:Database Password=redacted;'
This causes an error:
The OLE DB provider "Microsoft.ACE.OLEDB.16.0" for linked server "redacted" reported an error. The provider reported an unexpected catastrophic failure. (Microsoft SQL Server, Error: 7399)
Attempted fixes:
Varying provider strings, like 'Uid=;Pwd='
Check AccessDB engine is installed (64bit)
Moved db to local drive (from network)
OLE provider set 'Allow Inprocess' to true
The database appears to be accessible through MS Access front end. No signs of corruption.
Any ideas?

SQL Server bcp with stored procedure

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).

BCP Permissions Error When Using queryout with SQL Azure

I'm trying to query data from one database1..table and import it to another database2..table. database2..table already has some of the data, so I need to filter it. I'm calling this command:
bcp "SELECT * FROM Foo.dbo.Bar WHERE id > 10000" queryout ./Bar.txt -N -U user#machine -S tcp:hash.database.windows.net -P password
Error: SQLState = 37000, NativeError = 4060
Error = [Microsoft][SQL Server Native Client 11.0][SQL Server]Cannot open database "master" requested by the login. The
login failed.
Since user#machine doesn't have access to the master database, the query fails. Note that if I use bcp Foo.dbo.Bar out ./Bar.txt... instead of the above, it works, since it never tries to open the master database.
It's a SQL Azure instance, so I cannot link the servers together.
So: how can I do this filter on data freshness in Foo.dbo.Bar without BCP ever trying to connect to master?
According to the documentation, you must specify the -d parameter when using queryout with SQL Azure.

Resources