请问怎么用存储过程生成文件

jcos 2003-12-18 02:24:23
用存储过程生成文件如何实现
...全文
229 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjcxc 2003-12-19
  • 打赏
  • 举报
回复
不能.
jcos 2003-12-19
  • 打赏
  • 举报
回复
请问zjcxc(邹建)可以不用临时表,直接赋值给一个ntext或者是长的nvarchar输出吗.
等会我给分结帖的了,谢谢你的帮助
tangyanjun1 2003-12-19
  • 打赏
  • 举报
回复
这个得学习。
zjcxc 2003-12-19
  • 打赏
  • 举报
回复
--下面就是生成一个test.htm的文件,你们的内容是"你好,测试中".

--创建表,生成html文件内容
create table ##tb(aa varchar(1000))
insert into ##tb
select '<html>'
union all select '<head>'
union all select '<meta http-equiv="Content-Type" content="text/html; charset=gb2312">'
union all select '<title>测试</title>'
union all select '</head>'
union all select '<body>'
union all select '你好,测试中'
union all select '</body>'
union all select '</html>'

--生成test.htm文件
exec master..xp_cmdshell 'bcp ##tb out c:\test.htm /P"" /c'

--删除临时表
drop table ##tb
jcos 2003-12-19
  • 打赏
  • 举报
回复
举个实例,我想用存储过程生成一个test.htm的文件,你们的内容是"你好,测试中".
如何实现?
zjcxc 2003-12-18
  • 打赏
  • 举报
回复
那只能根据情况分析啦.
jcos 2003-12-18
  • 打赏
  • 举报
回复
例如说我想生成一个网页得htm页面,里面既包含数据库的东西也包扩htm代码,该怎么搞?
zjcxc 2003-12-18
  • 打赏
  • 举报
回复
/*--用ADO存储XML
--*/

declare @err int,@src varchar(255),@desc varchar(255)
declare @obj int,@str varchar(1000)
set @str='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='
+db_name()+';Data Source='+@@servername
--上面是用ADO连接SQL的字符串,根据你的情况做调整
/*--ADO连接SQL的字符串模板
@str='Provider=SQLOLEDB.1;Persist Security Info=True;User ID=用户名;Password=密码;Initial Catalog=数据库名;Data Source=SQL服务器名'
--*/

exec @err=sp_oacreate 'adodb.recordset',@obj out
if @err<>0 goto lberr

exec @err=sp_oamethod @obj,'open',null,'地区资料',@str
if @err<>0 goto lberr

exec master..xp_cmdshell 'del c:\a.xml'
exec @err=sp_oamethod @obj,'save',null,'c:\a.xml',1
if @err<>0 goto lberr

exec @err=sp_oadestroy @obj
return

lberr:
exec sp_oageterrorinfo 0,@src out,@desc out
select cast(@err as varbinary(4)) as 错误号
,@src as 错误源,@desc as 错误描述
go
zjcxc 2003-12-18
  • 打赏
  • 举报
回复
--生成文本文件?

/*--实现数据导入/导出的存储过程

可以实现导入/导出 指定表 到文本文件

邹建 2003.07-----------------*/

/*--调用示例
导出调用示例
--导出指定表,这里指定导出表:地区资料
exec file2table 'zj','','','c:\zj.txt','xzkh_new..地区资料'

导入调用示例
--导入指定表,这里指定导入表:地区资料
exec file2table 'zj','','','c:\zj.txt','xzkh_sa..地区资料',0
--*/

if exists(select 1 from sysobjects where name='File2Table' and objectproperty(id,'IsProcedure')=1)
drop procedure File2Table
go

create procedure File2Table
@servername varchar(200) --服务器名
,@username varchar(200) --用户名,如果用NT验证方式,则为空''
,@password varchar(200) --密码
,@filename varchar(1000) --目录名+文件名
,@tbname varchar(500)='' --数据库..表名
,@isout bit=1 --1为导出(默认),0为导入
as
declare @sql varchar(8000)

set @sql='bcp "'+@tbname
+case when @isout=1 then '" out' else '" in' end
+' "'+@filename+'" /c' +' /S"'+@servername
+case when isnull(@username,'')='' then ''
else '" /U"'+@username end
+'" /P"'+isnull(@password,'')+'"'
exec master..xp_cmdshell @sql
go
zjcxc 2003-12-18
  • 打赏
  • 举报
回复
生成什么文件?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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