SQL将数据导出到XML文件

导出XML文件有两种方法: 方法一:直接导出到XML文件,无须创建表来存储XML文本到数据库,但是如果值含中文时无法正常打开XML文件,有待研究,上代码: 需要注意的是查询数据时表前面一定要跟 数据库..表名

1 create proc [dbo].[OutputXML] 

AS
declare @strsql VARCHAR(4000)

declare @strPath VARCHAR(100)
declare @str VARCHAR(200)
SELECT @str= convert(varchar(12),getdate()-1,112)

SET @strPath='F:\XMLOutFile\'

SET @strPath+='P_'+@str

SET @strPath+='.XML'

SET @strsql='BCP "select id, isnull(name,'''') AS rname from Tdb..TableTest'

SET @strsql+='FOR XML PATH(''roots''),TYPE , ELEMENTS ,ROOT(''rootP'')" QUERYOUT "'

SET @strsql+=@strPath
SET @strsql+='" -c -t -T -S localhost'

EXEC xp_cmdshell @strsql

 

方法二:需要创建一个表来存储文本

crate table xmltab (xtable varchar(max)) 
crate PROC [dbo].[OutputXML] 
AS
 declare @strsql VARCHAR(4000) 
declare @strPath VARCHAR(100)
 declare @str VARCHAR(200)
SELECT @str= convert(varchar(12),getdate()-1,112) 
SET @strPath='F:\XMLOutFile\'
 SET @strPath+='B_'+@str 
SET @strPath+='.XML' 
insert into tdb..xmltab values('<?xml version="1.0" encoding="GB2312"?>') declare @x xml set @x=( select id, isnull(name,'''') AS rname from Tdb..TableTest FOR XML PATH('roots'),TYPE , ELEMENTS ,ROOT('rootp')) insert into Tdb..xmltab select cast(@x as varchar(max)) SET @strsql='bcp Tdb..dbo.xmltab out ' SET @strsql+=@strPath SET @strsql+=' -c -T -k' exec master..xp_cmdshell @strsql DELETE as_product..xmltab --删除数据

  启动SQL对应服务,

sp_configure 'show advanced options',1
reconfigure
go
sp_configure 'xp_cmdshell',1
reconfigure
go

其它生成XML数据方式

select * from   [TABLES]  FOR XML RAW,XMLDATA  

 

FOR { BROWSE | XML { raw | auto | explicit }
[ , XMLDATA ]
[ , ELEMENTS ]
[ , binary BASE64 ]
}

转载于:https://www.cnblogs.com/leojon/p/4390643.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值