欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-20 22:18:40
一、‘xp_cmdshell’的启用 SQL Server阻止了对组件‘xp_cmdshell’的过程‘sys.xp_cmdshell’的访问。因为此组件已作为此服务嚣安全配置的一部分而被关 闭。系统管理员可以通过使用sp_configure启用‘xp_cmdshell’。有关启用‘xp_cmdsh ......

一、‘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 主数据文件

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

 

整体解决方法 存储过程

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

 

上一篇:

下一篇: