Posted - 06/06/2013 : 17:58:25
Hello Everyone,
I am new to sql server, I am trying to come up with a sql block through which i can update the XML stored in a table as a text. In order to do that, i am converting the text to XML and also getting the values from other tables dynamically and then use those values o update the XML and then convert the XML back to text and then update the base table with the modified text (XML).While i am doing this, i have encountered this error i.e.
An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown.
and also the procedure is very slow.Please go throw my code and let me know if there is any other way to accomplish which does not throw the memory error and also is faster in execution .
Please find the code below.
DECLARE @uniqueId int
DECLARE @claimnum claimnum
DECLARE @TEMP TABLE (uniqueId int)
DECLARE @XML_AFTERCAST XML
DECLARE @NAME VARCHAR(MAX)
DECLARE @NAME1 VARCHAR(MAX)
-- Insert into the temporary table a list of the records to be updated
INSERT INTO @TEMP (uniqueId)
SELECT EDI_Assessment_ID FROM ED_TEST
-- Start looping through the records
WHILE EXISTS (SELECT * FROM @TEMP)
BEGIN
-- Grab the first record out
SELECT Top 1 @uniqueId = uniqueId FROM @TEMP
Select @XML_AFTERCAST = CAST(XML_Text AS XML),@claimnum = Claim_Num from ED_TEST where ED_ID = @uniqueId
Select @NAME = cl.First_Name + Case When Coalesce(cl.Middle_Name, '') = '' Then ' ' Else ' ' + cl.Middle_Name + '.' End ,
@NAME1 = cl.Last_Name
From
Customer_Role cr Join Customer cl On
cr.Client_Id = cl.Client_Id
Where
cr.Claim_Num =(SELECT CLAIM_NUM FROM ED_TEST WHERE ED_ID=@uniqueId)
And
cr.test_Code = '1'
Select @Name
Select @Name1
-- Perform some Replace on the Xml
Select @uniqueId
Select @NAME
Select @XML_AFTERCAST
SET
@XML_AFTERCAST.modify('replace value of (/Assessment/Insured/First_Name/text())[1] with sql:variable("@NAME")')
SET
@XML_AFTERCAST.modify('replace value of (/Assessment/Insured/Last_Name/text())[1] with sql:variable("@NAME1")')
UPDATE
dbo.ED_test
SET
XML_TEXT = CAST(CAST(@XML_AFTERCAST AS VARCHAR(MAX)) AS TEXT)
WHERE
ED_ID= @uniqueId
select * from ED_test where ED_id = @uniqueId
-- Drop the record so we can move onto the next one
DELETE FROM @TEMP WHERE uniqueId = @uniqueId
END
Thanks for the help
Hello Everyone,
I am new to sql server, I am trying to come up with a sql block through which i can update the XML stored in a table as a text. In order to do that, i am converting the text to XML and also getting the values from other tables dynamically and then use those values o update the XML and then convert the XML back to text and then update the base table with the modified text (XML).While i am doing this, i have encountered this error i.e.
An error occurred while executing batch. Error message is: Exception of type 'System.OutOfMemoryException' was thrown.
and also the procedure is very slow.Please go throw my code and let me know if there is any other way to accomplish which does not throw the memory error and also is faster in execution .
Please find the code below.
DECLARE @uniqueId int
DECLARE @claimnum claimnum
DECLARE @TEMP TABLE (uniqueId int)
DECLARE @XML_AFTERCAST XML
DECLARE @NAME VARCHAR(MAX)
DECLARE @NAME1 VARCHAR(MAX)
-- Insert into the temporary table a list of the records to be updated
INSERT INTO @TEMP (uniqueId)
SELECT EDI_Assessment_ID FROM ED_TEST
-- Start looping through the records
WHILE EXISTS (SELECT * FROM @TEMP)
BEGIN
-- Grab the first record out
SELECT Top 1 @uniqueId = uniqueId FROM @TEMP
Select @XML_AFTERCAST = CAST(XML_Text AS XML),@claimnum = Claim_Num from ED_TEST where ED_ID = @uniqueId
Select @NAME = cl.First_Name + Case When Coalesce(cl.Middle_Name, '') = '' Then ' ' Else ' ' + cl.Middle_Name + '.' End ,
@NAME1 = cl.Last_Name
From
Customer_Role cr Join Customer cl On
cr.Client_Id = cl.Client_Id
Where
cr.Claim_Num =(SELECT CLAIM_NUM FROM ED_TEST WHERE ED_ID=@uniqueId)
And
cr.test_Code = '1'
Select @Name
Select @Name1
-- Perform some Replace on the Xml
Select @uniqueId
Select @NAME
Select @XML_AFTERCAST
SET
@XML_AFTERCAST.modify('replace value of (/Assessment/Insured/First_Name/text())[1] with sql:variable("@NAME")')
SET
@XML_AFTERCAST.modify('replace value of (/Assessment/Insured/Last_Name/text())[1] with sql:variable("@NAME1")')
UPDATE
dbo.ED_test
SET
XML_TEXT = CAST(CAST(@XML_AFTERCAST AS VARCHAR(MAX)) AS TEXT)
WHERE
ED_ID= @uniqueId
select * from ED_test where ED_id = @uniqueId
-- Drop the record so we can move onto the next one
DELETE FROM @TEMP WHERE uniqueId = @uniqueId
END
Thanks for the help