Question

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

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top