---
title: "sql server遍历用户表导出所有表数据到文本文件"
author: "iammonster"
date: "2007-10-13T11:03:00Z"
tags: []
slug: "sql-server"
draft: false
lastmod: "2007-10-13T11:03:00Z"
id: 273298
url: "https://jiajun.iteye.com/blog/273298"
---

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)-- To allow advanced options to be changed.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)EXEC sp\_configure 'show advanced options', 1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)GO  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)-- To update the currently configured value for advanced options.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)RECONFIGURE  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)GO  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)-- To enable the feature.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)EXEC sp\_configure 'xp\_cmdshell', 1  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)GO  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)-- To update the currently configured value for this feature.  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)RECONFIGURE  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)GO  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)---xp\_cmdshell 'cd C:Program FilesMicrosoft SQL Server90ToolsBin'  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)xp\_cmdshell 'dir \*.exe'  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)SELECT NAME,NAME FROM SYS.OBJECTS  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)xp\_cmdshell 'bcp northwind.dbo.orders out c:mytest -T -c'  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)declare @sql nvarchar(max)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)set @sql=''  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select @sql=@sql+' xp\_cmdshell '+'''bcp northwind.dbo.'+quotename(name)+' out c:'+name+'.dat -T -c '+''' ' from sys.objects    
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)where type='u';  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)select @sql  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)exec (@sql)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)