Solved

Update text column converting to xml and updating via XPATH!

Posted on 2006-11-21
5
684 Views
Last Modified: 2008-03-04
Ok so I have a requirement to go through a table and copy a node from one place in the xml to another.


The table looks like this
dbo.LLCW18_DOCSET_TB
     FLDR_ID_NR(int, null)
     DOC_SET_XML_CO(text,null)

The DOC_SET_XML_CO is really xml stored as text.  So I have written the following sql

UPDATE dbo.LLCW18_DOCSET_TB
SET DOC_SET_XML_CO =
CASE
      WHEN cast(dbo.LLCW18_DOCSET_TB.DOC_SET_XML_CO as xml).exist('/BizTalk/Body/ConsumerLoan/TransferRequest/DocumentSet/DocumentData/Presentation/GAP/interestRate')=1
                  THEN cast([DOC_SET_XML_CO] as xml).modify('insert ' + cast(cast([DOC_SET_XML_CO] as xml).query('/BizTalk/Body/ConsumerLoan/TransferRequest/DocumentSet/DocumentData/Presentation/GAP/interestRate') as varchar(200))
        + ' after (/BizTalk/Body/ConsumerLoan/TransferRequest/Insurance/Product[@insuranceProductCode="GAP"])[1]')
      else DOC_SET_XML_CO
END


but I get the following error.

Msg 8137, Level 16, State 1, Line 1
Illegal use of xml data type method 'modify'. A non-mutator method is expected in this context.


Can anyone help me with this??

0
Comment
Question by:macros14
  • 2
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17987949
u can't update a text field usinf UPDATE command, u need to use WRITETEXT and UPDATETEXT
0
 
LVL 1

Author Comment

by:macros14
ID: 17988113
So would you know how to move a xml node in the text column to a different place the the xml doc stored in the text column?
0
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 500 total points
ID: 17989354
CREATE TABLE test (i int identity(1,1), j int, t text, c char(2))
go


delete test


INSERT INTO Test SELECT 1, 'asadf', 'I'
INSERT INTO Test SELECT 1, 'jjjjj', 'A'
INSERT INTO Test SELECT 1, 'kkkkksadf', 'A'
DECLARE @t varchar(8000)
SELECT @t = space(6000) + 'sadf'
INSERT INTO Test SELECT 1, @t, 'A'
SELECT @t = 'sadf'
INSERT INTO Test SELECT 1, @t, 'A'
SELECT @t = space(6000) + 'sadf'
INSERT INTO Test SELECT 1, @t, 'A'
SELECT @t = 'sadf'
INSERT INTO Test SELECT 1, @t, 'A'

INSERT INTO Test SELECT 2, 'asadf', 'I'
INSERT INTO Test SELECT 2, 'jjjjj', 'A'
INSERT INTO Test SELECT 2, 'kkkkksadf', 'A'
--DECLARE @t varchar(8000)
SELECT @t = space(6000) + 'sadf'
INSERT INTO Test SELECT 2, @t, 'A'
SELECT @t = 'sadf'
INSERT INTO Test SELECT 2, @t, 'A'
SELECT @t = space(6000) + 'sadf'
INSERT INTO Test SELECT 2, @t, 'A'
SELECT @t = 'sadf'
INSERT INTO Test SELECT 2, @t, 'A'






DECLARE @old varchar(20) ,
 @new varchar(20)
SELECT  @old = 'adf' ,
  @new = 'qsd'
 
DECLARE @i int

DECLARE @txtPtr binary(16) ,
 @offset int ,
 @dellen int
 
SELECT @dellen = len(@old)
SELECT @txtPtr = 0

WHILE @txtPtr IS NOT NULL
BEGIN
 SELECT @txtPtr = null
 SELECT  TOP 1 @i = i, @txtPtr = textptr(t)
 FROM test
 WHERE t LIKE '%' + @old + '%'
 
 IF @txtPtr IS NOT NULL
 BEGIN
  SELECT @offset = 1
  WHILE @offset <> 0
  BEGIN
   SELECT @offset = patindex('%' + @old + '%', t)
   FROM test
   WHERE i = @i
   --SELECT offset = @offset
   
   IF @offset > 0
   BEGIN
    SELECT @offset = @offset-1
    UPDATETEXT test.t @txtPtr @offset @dellen @new
   END
  END -- end while
 END-- End if
END-- end while
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question