T-SQL-在函数中使用sp_xml_preparedocument

T-SQL-在函数中使用sp_xml_preparedocument,sql,sql-server,xml,user-defined-functions,Sql,Sql Server,Xml,User Defined Functions,我对函数(UDF)和系统过程有问题。 我不能在UDF中使用过程。 但是有解决这个问题的办法吗 我有一个视图,在该视图中,a将调用函数xmlHistory 在函数中,我将准备XML,然后返回结果。 但是由于EXEC sp_xml_preparedocument,我无法运行该函数 是否有其他解决方案可以使用sp_xml_preparedocument过程 SELECT irsys.referencenumber AS [Ticket-Nr], dbo.xmlHis

我对函数(UDF)和系统过程有问题。 我不能在UDF中使用过程。 但是有解决这个问题的办法吗

我有一个视图,在该视图中,a将调用函数xmlHistory

在函数中,我将准备XML,然后返回结果。 但是由于EXEC sp_xml_preparedocument,我无法运行该函数

是否有其他解决方案可以使用sp_xml_preparedocument过程

SELECT 
        irsys.referencenumber AS [Ticket-Nr], 
        dbo.xmlHistory(irsys.caseid,1,'AssignedRole') AS [Role] ...



CREATE FUNCTION [dbo].[xmlHistory] (@caseID int, @historyID int, @name varChar(50) )
RETURNS varChar

AS
BEGIN
    DECLARE @val varChar(50)
    DECLARE @docHandle int
    DECLARE @xmlDocument nvarchar(max) -- or xml type
        SET @xmlDocument = (SELECT [historyitem]
          FROM [HLDATA].[dbo].[hlsyscasehistory]
          where caseid = @caseID and historyid = @historyID)
        EXEC sp_xml_preparedocument @docHandle OUTPUT, @xmlDocument
        -- Use OPENXML to provide rowset consisting of customer data.

        set @val = (SELECT Value 
        FROM OPENXML(@docHandle, 'HistoryDataDto/ODEs/ODE/Attributes/Attribute',2) 

        WITH (Name varchar(50), Value varchar(50))

        where name = @name)
     RETURN @val
END

GO
多谢各位


Felix

为什么不使用更“本机”的XML函数,例如或?当我使用query()或value()时,我无法使用,因此无法获得表resultsetWell,这听起来像是您想要的,但如果您随后对标量变量执行赋值,如果这两个函数中的任何一个返回多行,实际上都会出现问题。