使用XP_CMDSHELL将结果导出为CSV

时间:2021-05-18 16:48:54

I am beginner in SQL Server. When I am trying to run code:

我是SQL Server的初学者。当我尝试运行代码时:

'EXEC xp_cmdshell 'bcp "SELECT * FROM sysfiles" queryout "D:\sysobjects.txt" -T -c -t'

I am getting this error:

我收到此错误:

[Microsoft][SQL Server Native Client 11.0]Unable to open BCP host data-file NULL

1 个解决方案

#1


When you run xp_cmdshell, SQL server will start a new process. The user which this process runs as can vary depending on the user configured to run SQL server and whether the SQL agent is enabled.

运行xp_cmdshell时,SQL Server将启动一个新进程。此进程运行的用户可能会有所不同,具体取决于配置为运行SQL Server的用户以及是否已启用SQL代理。

To find which user the command runs as (I'm assuming at least Windows 7 on the server), execute:

要查找命令运行的用户(我假设服务器上至少有Windows 7),请执行:

EXEC xp_cmdshell 'whoami & cd & path'

The first line will be the user who is running xp_cmdshell, the second shows what the proces' working directory is and the rest will list the folders in the path.

第一行是运行xp_cmdshell的用户,第二行显示进程的工作目录是什么,其余的将列出路径中的文件夹。

To check effective permissions, use Explorer to navigate to:

要检查有效权限,请使用资源管理器导航到:

  • "d:\" Properies
  • Security
  • Advanced
  • Effective permissions
  • 'Select' and enter the name returned from whoami
  • '选择'并输入从whoami返回的名称

E.g. on my system, whoami returned nt authority\network service which is NETWORK SERVICE for the effective security dialog.

例如。在我的系统上,whoami返回了权限\网络服务,这是NETWORK SERVICE的有效安全对话框。

The path output will include a 'SQL server\xxx\binn' folder, check that the first of these matches the version of SQL your connected server is running - otherwise you may have to specify the path explicitly.

路径输出将包含“SQL server \ xxx \ binn”文件夹,检查其中第一个是否与您连接的服务器正在运行的SQL版本匹配 - 否则您可能必须明确指定路径。

#1


When you run xp_cmdshell, SQL server will start a new process. The user which this process runs as can vary depending on the user configured to run SQL server and whether the SQL agent is enabled.

运行xp_cmdshell时,SQL Server将启动一个新进程。此进程运行的用户可能会有所不同,具体取决于配置为运行SQL Server的用户以及是否已启用SQL代理。

To find which user the command runs as (I'm assuming at least Windows 7 on the server), execute:

要查找命令运行的用户(我假设服务器上至少有Windows 7),请执行:

EXEC xp_cmdshell 'whoami & cd & path'

The first line will be the user who is running xp_cmdshell, the second shows what the proces' working directory is and the rest will list the folders in the path.

第一行是运行xp_cmdshell的用户,第二行显示进程的工作目录是什么,其余的将列出路径中的文件夹。

To check effective permissions, use Explorer to navigate to:

要检查有效权限,请使用资源管理器导航到:

  • "d:\" Properies
  • Security
  • Advanced
  • Effective permissions
  • 'Select' and enter the name returned from whoami
  • '选择'并输入从whoami返回的名称

E.g. on my system, whoami returned nt authority\network service which is NETWORK SERVICE for the effective security dialog.

例如。在我的系统上,whoami返回了权限\网络服务,这是NETWORK SERVICE的有效安全对话框。

The path output will include a 'SQL server\xxx\binn' folder, check that the first of these matches the version of SQL your connected server is running - otherwise you may have to specify the path explicitly.

路径输出将包含“SQL server \ xxx \ binn”文件夹,检查其中第一个是否与您连接的服务器正在运行的SQL版本匹配 - 否则您可能必须明确指定路径。