大力:导出到Excel的存储过程,出现"查询提示超过了最大目录缓冲区 1023 字节(1246字节输入)" 如何解决?

stwx 2003-07-04 07:03:08
CREATE proc out2xls
@服务器名 varchar(255),
@库名 varchar(255),
@表名 varchar(255),
@用户名 varchar(100),
@密码 varchar(100),
@路径及文件名 varchar(255)
as
declare @temp1 nvarchar(4000),@temp2 varchar(8000)

set @temp1='select @value1='''',@value2='''' ;

select @value1=@value1+'',''''''+''[''+a.name+'']''+char(39),@value2=@value2+'',cast(''+''[''+a.name+'']''+ '' as varchar(200))'' from '+@库名+'..syscolumns a,'+@库名+'..sysobjects d where a.id=d.id and d.name='''+@表名+''''+' order by a.colorder'

print @temp1
exec sp_executesql @temp1,N'@value1 nvarchar(4000) output , @value2 varchar(8000) output',@temp1 output,@temp2 output

select @temp1=right(@temp1,len(@temp1)-1),@temp2=right(@temp2,len(@temp2)-1)

set @temp2='bcp "select '+@temp1+' union all SELECT '+@temp2+' FROM '+@库名+'..'+@表名+'" queryout '+@路径及文件名+' -c -S'+@服务器名+' -U'+@用户名+' -P'+@密码

print @temp2
EXEC master..xp_cmdshell @temp2
GO

---小的表(少字段)可以,大的表(多字段,40个左右)就不行, 好像是 bcp后面的字符超128就出现错误.大力:如何解决?
...全文
118 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengdali 2003-07-13
  • 打赏
  • 举报
回复
1、我的win2003没问题,这和你的os有关

2、你可以用一个中间表做
CREATE proc out2xls
@服务器名 varchar(255),
@库名 varchar(255),
@表名 varchar(255),
@用户名 varchar(100),
@密码 varchar(100),
@路径及文件名 varchar(255)
as
declare @temp1 nvarchar(4000),@temp2 varchar(8000)

set @temp1='select @value1='''',@value2='''' select @value1=@value1+'',''''''+''[''+a.name+'']''+char(39),@value2=@value2+'',cast(''+''[''+a.name+'']''+ '' as varchar(200))'' from '+@库名+'..syscolumns a,'+@库名+'..sysobjects d where a.id=d.id and d.name='''+@表名+''''+' order by a.colorder'

exec sp_executesql @temp1,N'@value1 nvarchar(4000) output , @value2 varchar(8000) output',@temp1 output,@temp2 output

select @temp1=right(@temp1,len(@temp1)-1),@temp2=right(@temp2,len(@temp2)-1)

select @temp1+' union all SELECT '+@temp2+' into 中间表 FROM '+@库名+'..'+@表名

set @temp2='bcp '+@库名+'.dbo.中间表 out '+@路径及文件名+' -c -S'+@服务器名+' -U'+@用户名+' -P'+@密码

EXEC master..xp_cmdshell @temp2
drop table 中间表
GO
stwx 2003-07-09
  • 打赏
  • 举报
回复
大力,帮帮忙,上面的代码可是你写的
zosky 2003-07-04
  • 打赏
  • 举报
回复
分两次导?

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧