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
ntext
,text
, andimage
data types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Usenvarchar(max)
,varchar(max)
, andvarbinary(max)
instead. See details here And if your data is XML - why aren't you use theXML
datatype to store it? It's more efficient than a "simple"varchar
or even worse:ntext
– Quebecntext
unfortunately, hence why I am casting it at the end. My modify statement should work correctly, because there is a value there and not a null. – Bulldozer