简体   繁体   English

与SQL Server的bcp错误

[英]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. 我在sql server 2005中创建了一个存储过程,以将数据从查询导出到网络路径。 The user is a sql user and it is also created in the AD. 用户是SQL用户,也是在AD中创建的。 The server that I am logged in is a remote server, with sysadmin privilege. 我登录的服务器是具有sysadmin权限的远程服务器。 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. 该程序也在上午01:30自动执行。 The command that is executed is a bcp command : 执行的命令是bcp命令:

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. 当我从本地sqlserver中的本地数据库尝试到同一路径时,该命令有效。

Thanks 谢谢

Make sure that you can telnet to the remote server. 确保您可以telnet到远程服务器。 I would also try to connect to the remote server via sqlcmd. 我还尝试通过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. 此外,sql server服务将需要share /目录的权限,以便您为其写入文件。

Another thing you should consider is using an SSIS package to export the data to the remote share. 您应该考虑的另一件事是使用SSIS包将数据导出到远程共享。 Generally is not a great practice to use xp_cmdshell to export the data. 使用xp_cmdshell导出数据通常不是很好的做法。 You can do everything you are trying to do via a simple SSIS package. 您可以通过简单的SSIS包执行您尝试执行的所有操作。 As an added benefit this also minimizes security risk because you can disable xp_cmdshell. 作为额外的好处,这也可以最大限度地降低安全风险,因为您可以禁用xp_cmdshell。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM