I need to insert a column in a stored procedure which reads XML as input and reads the XML value using XQuery in SQL Server.
Consider a table dbo.emailDetails
Create Table dbo.emailDetails
(
EmailId int,
HomeEmail varchar(250),
);
XML file:
<EmailDetails xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<EmailId>1</EmailId>
<HomeEmail>test@sample.com</HomeEmail>
</EmailDetails>
SQL Server stored procedure:
CREATE PROCEDURE [apply].[UpdateEmailDetails]
@EmailDetails XML
(DOCUMENT [dbo].[EmailDetails])
AS
BEGIN
DECLARE @EmailId INT, @HomeEmail NVARCHAR(250)
SET @HomeEmail = @EmailDetails.value(N'(//EmailDetails/HomeEmail)[1]',N'NVARCHAR(255)');
SET @EmailId = @EmailDetails.value(N'(//EmailDetails/EmailID)[1]',N'INT');
INSERT INTO dbo.emails (emailid, homeemail)
VALUES (@EmailId, @HomeEmail )
END
Now I would like to include the IsConfirmed
in the above stored procedure.
alter table dbo.Emaildetails
add IsConfirmed bit not null default 0
----- Executed successfully and added the new column in the table.
While I tried the below changes in the stored procedure:
Declare @IsConfirmed bit
SET @IsConfirmed = @EmailDetails.value(N'(//EmailDetails/IsConfirmed)[1]',N'BIT');
and got the error:
XQuery [value()]: There is no element named 'IsConfirmed' in the type 'element(EmailDetails,#anonymous) *'.
Could someone help me with this?
Kindly let me know if any details required further.
Regards, Viswa V.