我创建了一个存储过程来创建XML文件。 但是,当我运行此存储过程时,我只在MS SQL Server Management Studio中看到结果。但我希望在运行存储过程时,将自动创建一个XML文件。
有人有什么想法吗?
这是存储过程:
USE [NICK_OWNER]
GO
/****** Object: StoredProcedure [dbo].[procWO] Script Date: 09/29/2015 09:30:31 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[procWO]
AS BEGIN
SELECT
WO.WOID AS '@WOID',
WO.WONumber AS '@WONumber'
FROM
WO
WHERE WO.WOID = 2
FOR XML PATH('WO'), ROOT('ALLWO')
--Here should be some code to save it to a XML file
END
答案 0 :(得分:1)
您可以更改使用函数而不是过程的方法......
CREATE FUNCTION dbo.AllWOsAsXML()
RETURNS XML
AS
BEGIN
RETURN (SELECT WO.WOID AS [@WOID]
,WO.WONumber AS [@WONumber]
FROM WO
WHERE WO.WOID = 2
FOR XML PATH('WO'), ROOT('ALLWO'),TYPE);
END
您可以这样设置变量:
DECLARE @v XML=dbo.AllWOsAsXML();
如何将此内容写入您可以轻松找到的文件中(例如:SQL Server- How to write variables and query results to Text file?)