我正在编写 T-SQL UDF 并尝试使用游标获取的值修改 xml 变量。代码执行得很好,但是 xml 变量的修改没有发生。
代码如下:
CREATE FUNCTION GetDateBlockXmlFromTable(@occupieddates occupieddates READONLY)
RETURNS XML
AS
BEGIN
DECLARE @xmlresult xml;
DECLARE @datefrom datetime, @dateto datetime;
SELECT @xmlresult = '<root><DateBlocks/></root>';
DECLARE GetDateBlockXmlFromTable_Cur
CURSOR FOR
SELECT DateFrom, DateTo FROM @occupieddates
OPEN GetDateBlockXmlFromTable_Cur
FETCH NEXT FROM GetDateBlockXmlFromTable_Cur INTO @datefrom, @dateto
WHILE @@FETCH_STATUS = 0
BEGIN
SET @xmlresult.modify('insert <DateBlock><FirstDay>"{sql:variable("@datefrom")}"</FirstDay><EndDay>"{sql:variable("@dateto")}"</EndDay></DateBlock> as last into (/DateBlocks)[1]');
FETCH NEXT FROM GetDateBlockXmlFromTable_Cur INTO @datefrom, @dateto
END
CLOSE GetDateBlockXmlFromTable_Cur
DEALLOCATE GetDateBlockXmlFromTable_Cur
RETURN @xmlresult;
END
占用日期只读输入表如下所示:
CREATE TYPE occupieddates AS TABLE
(
DateFrom datetime,
DateTo datetime,
)