I've tried to make this code work, but the result always NULL for output1, output2
I've tested the variable @xml after UPDATE, but @xml is NULL also. This make xml query in SET return 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
*/
Does anybody have experience with this problem ?
Please help.
Thanks
UPDATE: I have made another test. The inline-variable work fine if it was not XML, as the expample below:
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
*/
The result is as expected, what's wrong with XML inline-variable ?