/*--存为XML
将表/查询存储为标准的XML文件
--*/
/*--调用示例
--用SQL用winows身份验证的情况
exec p_savexml @sql='地区资料',@fname='c:\地区资料.xml'
--用指定的用户
exec p_savexml @sql='地区资料',@fname='c:\地区资料.xml',@userid='sa'
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_savexml]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[p_savexml]
GO
create proc p_savexml
@sql varchar(8000), --要保存的表/视图/SQL查询语句
@fname varchar(1000)='c:\tmp.xml', --保存的XML文件名
@userid varchar(50)=', --用户名,如果为nt验证方式,则为'
@password varchar(50)=' --密码
as
declare @err int,@src varchar(255),@desc varchar(255)
declare @obj int,@constr varchar(1000)
if isnull(@userid,')='
set @constr='Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog='
+db_name()+';Data Source='+@@servername
else
set @constr='Provider=SQLOLEDB.1;Persist Security Info=True;'
+'User ID='+@userid+';Password='+isnull(@password,')
+';Initial Catalog='+db_name()+';Data Source='+@@servername
exec @err=sp_oacreate 'adodb.recordset',@obj out
if @err<>0 goto lberr
exec @err=sp_oamethod @obj,'open',null,@sql,@constr
if @err<>0 goto lberr
set @sql='del '+@fname
exec master..xp_cmdshell @sql,no_output
exec @err=sp_oamethod @obj,'save',null,@fname,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