Home > Database > Mysql Tutorial > body text

数据从SQL Server输到文本文件

WBOY
Release: 2016-06-07 16:18:22
Original
1148 people have browsed it

你需要做的第一件事是确定xp_cmdshell是可用的。你可以选择下面两种方法中的一种来实现。 1.你可以使用sp_configure并执行下面的脚本。 EXEC master.dbo.sp_configure 'show advanced options', 1RECONFIGUREEXEC master.dbo.sp_configure 'xp_cmdshell', 1R

  你需要做的第一件事是确定xp_cmdshell是可用的。你可以选择下面两种方法中的一种来实现。

  1.你可以使用sp_configure并执行下面的脚本。

数据从SQL Server输到文本文件 三联

EXEC master.dbo.sp_configure 'show advanced options', 1 RECONFIGURE EXEC master.dbo.sp_configure 'xp_cmdshell', 1 RECONFIGURE

  2.你可以使用Surface Area Configuration工具。

  选择功能中的Surface Area Configuration并检查Enable xp_cmdshell检验栏。

数据从SQL Server输到文本文件

  图一

  现在既然你已经使xp_cmdshell可用,那么你可以准备输出你的文件。

  下面是一个示例命令,它将把系统文件表的结果输出到以逗号分割的文件bcptest.txt中。只需复制并把它粘贴到一个查询窗口中且执行这个查询。

  EXEC xp_cmdshell 'bcp "SELECT * FROM sysfiles" queryout "C:bcptest.txt" -T -c -t,'

  注:BCP是一个命令行实用工具,xp_cmdshell要求只能在一个SQL批处理内使用。

  当在"master"数据库中执行上面的命令时将得到这个输出结果。

  图二

  使用的参数是:

  l Queryout选项允许你指定一个查询来输出。这可以像我们使用的查询那样简单也可以像你需要的那样复杂。你也可以创建一个视图并从视图中选择数据。

  l file name是存储结果的,它位于queryout选项之后。

  l -T参数指定bcp实用工具通过集成安全使用一个可信连接来连接到SQL Server。如果你想使用一个SQL Server登陆,那么你可以使用-P(密码)和-U(用户)。

  l -c指定将用于各个字段的字符数据类型。

  l -t参数允许你指定一个字段定界符。在-t之后的字符将用于隔开数据字段。如果-t被删除了,,那么tab将用于默认定界符。

  另一个你可能需要使用的参数是-S,你可以用它来指定服务器名称。如果你有一个指定的实例,那么你需要使用这个参数。下面是一个连接到服务器"DEVELOP" 和实例 "DEV1"的例子。

  EXEC xp_cmdshell 'bcp "select name, type_desc, create_date from sys.objects" queryout "C:bcptest2.txt" -T -SDEVELOPDEV1 -c -t,'EXEC xp_cmdshell 'bcp "select name, type_desc, create_date from sys.objects" queryout "C:bcptest2.txt" -T -SDEVELOPDEV1 -c -t,'

  你也必须知道使用参数-t和-T的实例是不一样的,所以要保证你在实例中使用正确的参数。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!