Solved

Update multiple text rows with UPDATETEXT.

Posted on 2010-11-08
7
903 Views
Last Modified: 2012-05-10
How do I update multiple rows of text column using the Updatetext statement in SQL Server 2000
0
Comment
Question by:Bitadmin
  • 4
  • 3
7 Comments
 

Author Comment

by:Bitadmin
ID: 34089040
Here is what I've tried so far without success:

Declare @nTextPointer binary(16)

While Exists(Select * from contact1 where key1 IN('39528','41590','25228',
'41591',
'34833',
'40034',
'40725')

Begin
select @nTextPointer = TEXTPTR(notes)
from contact1
where contact1.notes LIKE  '%  Nov 10, 2010 %'

Updatetext contact1.notes @nTextPointer 76 13 ' Oct 10, 2010 '

End
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34089074
Unfortunately, the TEXT data type doesn't lend itself to bulk manipulation...
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34089178
You can loop through the table something like

Declare @nTextPointer binary(16)
declare @key1 varchar(10)
select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',
'41591',
'34833',
'40034',
'40725')
while @@rowcount > 0
Begin
select @nTextPointer = TEXTPTR(notes)
from contact1
where contact1.notes LIKE  '%  Nov 10, 2010 %'

Updatetext contact1.notes @nTextPointer 76 13 ' Oct 10, 2010 '

select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',
'41591',
'34833',
'40034',
'40725')
and key1 > @key1
End
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Bitadmin
ID: 34092002
I tried this but it didnot update all the rows.  

Declare @nTextPointer binary(16)
declare @key1 varchar(10)
select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',
'41591',
'34833',
'40034',
'40725')

while @@rowcount > 0
Begin
select @nTextPointer = TEXTPTR(notes)
from contact1
where contact1.key1 LIKE  @key1 --'%  Oct 10, 2010 %'

Updatetext contact1.notes @nTextPointer 74 15 ' Oct 10, 2010   '

select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',
'41591',
'34833',
'40034',
'40725')
and key1 > @key1
End

select key1, notes from contact1 where key1 in('39528','41590','25228',
'41591',
'34833',
'40034',
'40725')

Rows with key1 40034 and 34833 did not update.  Can you see anything in my code that is off?
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34092096
Maybe check the progress?
What do the text on those records look like?
Declare @nTextPointer binary(16)

declare @key1 varchar(10)

select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',

'41591',

'34833',

'40034',

'40725')



while @@rowcount > 0

Begin

select @nTextPointer = TEXTPTR(notes)

from contact1

where contact1.key1 LIKE  @key1 --'%  Oct 10, 2010 %'



select convert(varchar(100), notes) from contact1 where key1 = @key1 -- show what is being modified

Updatetext contact1.notes @nTextPointer 74 15 ' Oct 10, 2010   '



select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',

'41591',

'34833',

'40034',

'40725')

and key1 > @key1

End

Open in new window

0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 250 total points
ID: 34092102
Don't worry, I see it - was missing an order by clause to make it loop properly
Declare @nTextPointer binary(16)

declare @key1 varchar(10)

select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',

'41591',

'34833',

'40034',

'40725') order by key1 ASC



while @@rowcount > 0

Begin

select @nTextPointer = TEXTPTR(notes)

from contact1

where contact1.key1 LIKE  @key1 --'%  Oct 10, 2010 %'



--select convert(varchar(100), notes) from contact1 where key1 = @key1 -- show what is being modified

Updatetext contact1.notes @nTextPointer 74 15 ' Oct 10, 2010   '



select top 1 @key1 = key1 from contact1 where key1 IN('39528','41590','25228',

'41591',

'34833',

'40034',

'40725')

and key1 > @key1 order by key1 ASC

End

Open in new window

0
 

Author Closing Comment

by:Bitadmin
ID: 34095397
Thanks for all your help Cyberkiwi.  I didnot use your exact code but it gave me a similar idea using a cursor. I needed to update the text/ntext column for over 1500 records.  You get all the points and incase your curious the code I used is listed below.

USE mydatabase
GO
EXEC sp_dboption mydatabase, 'select into/bulkcopy', 'true'
GO

DECLARE @nTextpointer binary(16)
DECLARE c1 cursor for select key1 FROM contact1 where key1 IN('39528','41590','25228',
'41591',
'34833',
'40034',
'40725'.......  )

DECLARE @_id varchar(6)
DECLARE @key1 Varchar(6)

open c1
fetch c1 into @_id
while @@fetch_status = 0
begin
      SELECT @nTextpointer = TEXTPTR(notes)
      FROM contact1 WHERE key1 = @_id

      UPDATETEXT contact1.notes @nTextpointer 75 13 '% Oct 10, 2010 %'

      fetch c1 into @_id
end
close c1
deallocate c1
GO
EXEC sp_dboption mydatabase, 'select into/bulkcopy', 'false'
GO
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now