博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Simple way to export SQL Server data to Text Files
阅读量:7088 次
发布时间:2019-06-28

本文共 3008 字,大约阅读时间需要 10 分钟。

 

Simple way to export SQL Server data to Text Files
    By: Ken Simmons   |   Read Comments (7)   |   Related Tips: More
Win an eBook - select from over 1,000 titles!
Problem
Since the transition from DTS to SSIS, I have found myself looking for alternative ways to do simple tasks such as exporting data from SQL Server into text files. SSIS is a far more powerful tool than DTS, but I generally try to stay away from it for simple operations.  With the combination of a few T-SQL commands this tip shows you a simple way to export data to text files.
Solution
One of the ways I have worked around this is by using bcp with xp_cmdshell. It's fast, easy and I can integrate it right into my code.
The first thing you have to do is make sure xp_cmdshell is enabled. You can do this in one of two ways.
1. You can use sp_configure and execute the following script.
EXEC master.dbo.sp_configure 'show advanced options', 1
RECONFIGURE
EXEC master.dbo.sp_configure 'xp_cmdshell', 1
RECONFIGURE
2. You can use the Surface Area Configuration Tool.
Select Surface Area Configuration for Features and check the Enable xp_cmdshell checkbox.
Now that you have xp_cmdshell enabled you are ready to export your files.
Here is a sample command that will export the results of the sysfiles table to a comma delimited file called bcptest.txt. Just copy and paste this into a query window and execute the query.
EXEC xp_cmdshell 'bcp "SELECT * FROM sysfiles" queryout "C:\bcptest.txt" -T -c -t,'
Note: BCP is a command line utility and xp_cmdshell is only required to use it within a SQL Batch.
This is the output for the above command when run in the "master" database.
The parameters that were used are:
    The queryout option allows you to specify a query to export. This could be as simple as the query we have are as complicated as you want.  You can also create a view and select the data from a view.
    The file name where the results will be stored is placed after the queryout option.
    The -T parameter specifies that the bcp utility connects to SQL Server with a trusted connection using integrated security. You could use -P (password) and -U (user) if you want to use a SQL Server login.
    The -c specifies the character dataype will be used for each field.
    The -t parameter allows you to specify a field delimiter. The character after -t will be used to separate the data fields. If -t is removed, tab will be used as the default delimiter.
Another parameter you may need to use is -S to specify the server name.  If you have a named instance, you will need to use this parameter.  Here is an example connecting to server "DEVELOP" and instance "DEV1".
EXEC xp_cmdshell 'bcp "select name, type_desc, create_date from sys.objects" queryout "C:\bcptest2.txt" -T -SDEVELOP\DEV1 -c -t,'
Also be aware that the case of the parameter such as -t and-T makes a difference, so make sure you have correct case for the parameter you are using.  You can find more options for BCP here:  http://msdn.microsoft.com/en-us/library/ms162802.aspx
Next Steps

转载地址:http://kxyql.baihongyu.com/

你可能感兴趣的文章
saltstack知识点2
查看>>
Jenkins Pipeline
查看>>
ansible 模块之 yum模块详解
查看>>
PhoneGap跨平台Android环境的搭建
查看>>
西北大学(Northwestern University)-大数据分析课程
查看>>
php框架-hoby
查看>>
7.1 vim编辑器
查看>>
bash 词频统计
查看>>
Python之转义字符表
查看>>
mysql并发插入重复数据问题的解决思路
查看>>
MySQL 5.7.x修改root默认密码(CentOS下)
查看>>
Linux下动态加载SO文件
查看>>
Mysql创建、删除用户
查看>>
我的友情链接
查看>>
MySQL-MySQL常用命令
查看>>
Linux iptraf 网络监控
查看>>
Swift::8::枚举
查看>>
ZABBIX web端 显示 server 运行状态 不
查看>>
Aspose.Words使用教程之在文档中找到并替换文本
查看>>
ORACLE官方文档如何学习
查看>>