问题
I have the following stored procedure:
ALTER PROCEDURE [dbo].[UpPro]
(
@PN varchar(200),
@xml xml,
@gVa varchar(10),
)
AS
/* update the gender */
BEGIN
SET NOCOUNT ON;
Select @gVa = t1.[Gender]
From [myDb].[dbo].[myTable1] t1 --replace Value2 and table to the table which is updated through SSIS
Where t1.Name = @PN
PRINT @gVa //displays 'F'
Set @xml.modify('replace value of (/root/Phys/gender/text())[1] with sql:variable("@gVa")');
END
/* once all the node has been "temporarily" changed, update the table columns for the provider */
BEGIN
--update the table after casting dummy xml variable
Update [myDb].[dbo].[TC]
Set [chtml] = cast(cast(@xml as nvarchar(max)) as ntext)
Where [ctitle] = @PN
END
When I run the query, I get the following error:
Msg 5302, Level 16, State 1, Procedure UpPro, Line 115
Mutator 'modify()' on '@xml' cannot be called on a null value.
How can I resolve the error. I am trying to update xml value in a column (chtml
) which is of type ntext
in the TC
table.
Please let me know if I have to provide more information.
Just to test out the code, I just tried the following and it still gave the same error:
DECLARE @gVa varchar(10)
DECLARE @xml xml
Select @gVa = t1.[Gender]
From [myDb].[dbo].[myTable1] t1 --replace Value2 and table to the table which is updated through SSIS
Where t1.Name = 'Doctor 1'
PRINT @gVa
If @xml IS NOT NULL
BEGIN
Set @xml.modify('replace value of (/root/Phys/gender/text())[1] with sql:variable("@gVa")');
END
Else
BEGIN
PRINT 'NOT WORK'
END
Keeps printing NOT WORK
Original column (chtml
) data:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<Phys>
<gender>M</gender>
</Phys>
</root>
After the above SP executes, the gender
should be F
and not M
回答1:
The error actually says it all, @XML
is null and that is not allowed.
Repro:
declare @X xml;
set @X.modify('replace value of text()[1] with "1"');
Msg 5302, Level 16, State 1, Line 4 Mutator 'modify()' on '@X' cannot be called on a null value.
Check for null before you modfy.
declare @X xml;
if @X is not null
set @X.modify('replace value of text()[1] with "1"');
来源:https://stackoverflow.com/questions/32974700/why-do-i-receive-a-mutator-error-when-modifying-an-xml-value-in-sql