BCP SQL导出EXCEL常见问题及解决方法;数据导出存储过程

一、‘xp_cmdshell’的启用

SQL Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问。因为此组件已作为此服务嚣安全配置的一部分而被关 闭。系统管理员可以通过使用sp_configure启用‘xp_cmdshell’。有关启用‘xp_cmdshell’的详细信息

解决方法:

1、通过SQL语句开启。[推荐此方法,因为在任何版本的SQL SERVER中都可以使用。]
通过查询分析器,选择Master数据库,然后执行以下SQL内容:
sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go

2、开始 --> SQL安装目录 --> 配置 SQL server managerment 外围应用配置器-->“功能的外围应用配置器”-->找到“xp_cmdshell”点击启用

 

二、SQLState = S0002, NativeError = 208   Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]对象名 'XXX' 无效。

SQLState = 37000, NativeError = 8180

Error = [Microsoft][SQL Server Native Client 10.0][SQL Server]无法预定义语句。

解决方法: 数据表名补全 [库名].[用户名].[表名]

 exec  xp_cmdshell 'bcp " select  * from [库名].[用户名].[表名]" queryout "  路径 "  -T  -c -U   -P  '

 

三、SQLState = S1000, NativeError = 0

Error = [Microsoft][SQL Server Native Client 10.0]无法打开 BCP 主数据文件

解决方法:检查存储文件路径是否正正确,磁盘是否存在

 

四、在执行 sp_xp_cmdshell_proxy_account 的过程中出错。可能原因: 提供的帐户无效或无法创建 '##xp_cmdshell_proxy_account##' 凭据。错误代码: '0'。

master-->可编程性-->扩展存储过程-->系统扩展存储过程-->xp_cmdshell-->右键 属性-->权限--->添加当前执行用户--》勾选“执行”

 

整体解决方法 存储过程

CARATE  PROCEDURE execname @filename nvarchar(4000),@path nvarchar(2000) 
AS
BEGIN
declare @sql nvarchar(4000)
set @path=Replace (@path,'\','/');  --替换路径符号
set @filename=Replace (@filename,'from ','from [库名].[用户名].'); --添加数据表全名
set @sql='bcp "'+@filename+'" queryout "'+@path+'"  -T  -c -U用户名 -P密码' 
 exec  xp_cmdshell @sql
END

 

posted @ 2018-11-05 15:18  心韵  阅读(2939)  评论(0编辑  收藏  举报