我试图让这段代码工作,但是对于output1,output2
,结果总是为NULL我在UPDATE之后测试了变量@xml,但@xml也是NULL。这使得SET中的xml查询返回NULL。
CREATE FUNCTION fnXml
(
@input1 DECIMAL(8, 2) ,
@input2 INT
)
RETURNS XML
AS
BEGIN
DECLARE @xml XML
SET @xml = ( SELECT @input1 + 1 AS c ,
@input2 + 2 AS i
FOR
XML PATH('r')
)
RETURN @xml
END
GO
CREATE TABLE TestXml
(
ID INT ,
Input1 DECIMAL(8, 2) ,
Input2 INT ,
Output1 DECIMAL(8, 2) ,
Output2 INT
)
INSERT TestXml
VALUES ( 1, 1, 2, NULL, NULL )
INSERT TestXml
VALUES ( 2, 3, 4, NULL, NULL )
DECLARE @xml XML
UPDATE TestXml
SET @xml = dbo.fnXml(Input1, Input2) , -- @xml is always NULL ???
Output1 = ( SELECT r.value('.', 'decimal(9,2)') AS item
FROM @xml.nodes('//c') AS records ( r )
) ,
Output2 = ( SELECT r.value('.', 'decimal(9,2)') AS item
FROM @xml.nodes('//i') AS records ( r )
)
SELECT *
FROM testxml
/*
Result
ID Input1 Input2 Output1 Output2
1 1.00 2 NULL NULL
2 3.00 4 NULL NULL
*/
--test fnXml independently
SET @xml = dbo.fnXml(1,2)
SELECT r.value('.', 'decimal(9,2)') AS item
FROM @xml.nodes('//c') AS records ( r )
/*
Result:
item
2.00
*/
有没有人遇到过这个问题?
请帮忙。
由于
更新:我做了另一个测试。如果内联变量不是XML,则内联变量可以正常工作,如下面的示例所示:
CREATE FUNCTION fnTestNumber ( @input1 INT )
RETURNS INT
AS
BEGIN
RETURN @input1 + 1
END
GO
CREATE TABLE TestNumber
(
ID INT ,
Input1 INT ,
Output1 INT,
Output2 INT
)
INSERT TestNumber VALUES ( 1, 1, NULL, NULL )
INSERT TestNumber VALUES ( 2, 2, NULL, NULL )
DECLARE @temp INT
UPDATE TestNumber
SET @temp = dbo.fnTestNumber(Input1),
Output1 = @temp + 1,
Output2 = @temp + 2
SELECT * FROM TestNumber
/* Result
ID Input1 Output1 Output2
1 1 3 4
2 2 4 5
*/
结果如预期的那样,XML内联变量出了什么问题?
答案 0 :(得分:1)
使用 WITH语句:
;WITH t AS
(
SELECT dbo.fnXml(Input1, Input2) AS NewXML,Output1,Output2
FROM TestXml
)
UPDATE t
SET Output1=NewXML.value('(/r//c/node())[1]', 'DECIMAL(8,2)'),
Output2=NewXML.value('(/r//i/node())[1]', 'INT')
SELECT *
FROM testxml
答案 1 :(得分:1)
不要理解你的问题(请参阅我的评论),但我可以提供简单的解决方案:
UPDATE t
SET Output1 = x.value('(/r/c)[1]','decimal(8,2)'),
Output2 = x.value('(/r/i)[1]','int')
FROM TestXml t
CROSS APPLY (
SELECT dbo.fnXml(t.Input1, t.Input2) as x
) fX
SELECT *
FROM testxml
输出:
ID Input1 Input2 Output1 Output2
1 1.00 2 2.00 4
2 3.00 4 4.00 6
没有XML(相同的输出):
UPDATE testxml
SET Output1 = Input1 + 1,
Output2 = Input2 + 2