dbTalk Databases Forums  

UPDATETEXT, WRITETEXT

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss UPDATETEXT, WRITETEXT in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Zamdrist
 
Posts: n/a

Default UPDATETEXT, WRITETEXT - 06-11-2007 , 03:20 PM






I need to update/change the text in a table column of data type TEXT.
It's a smaller set of records (72) where I need to append onto the end
a string of text, the same for all records.

I don't quite understand how UPDATETEXT and WRITETEXT work, but here
is how I would write the Update query were the field not a TEXT type.

Update Matters Set Description = Description + ' (Stylized)'
Quote:
From Matters
Inner Join cwconnect On cwconnect.mmatter = matters.matterid

Thoughts how I might do this using WRITETEXT or UPDATETEXT?

Thanks,

Steve



Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: UPDATETEXT, WRITETEXT - 06-11-2007 , 04:52 PM






Zamdrist (zamdrist (AT) gmail (DOT) com) writes:
Quote:
I need to update/change the text in a table column of data type TEXT.
It's a smaller set of records (72) where I need to append onto the end
a string of text, the same for all records.

I don't quite understand how UPDATETEXT and WRITETEXT work, but here
is how I would write the Update query were the field not a TEXT type.

Update Matters Set Description = Description + ' (Stylized)'
From Matters
Inner Join cwconnect On cwconnect.mmatter = matters.matterid

Thoughts how I might do this using WRITETEXT or UPDATETEXT?
Here is a quick example on using UPDATETEXT. Note that you need to
work with one row at a time.

The script retrieves a text pointer which is, well, let's call it a
technology of the eighties. No wonder that Microsoft has deprecated the
text data type in SQL 2005 in favour of varchar(MAX), which is a lot
easier to work with. Anyway, you need this text point when you work
with READTEXT, WRITETEXT and UPDATETEXT. You also need to know the
current length of the string, to tell where to add the new text. The
parameter that is NULL, is a delete-length where you could specify
how many characters that are to be deleted.

CREATE TABLE textie (id int NOT NULL,
thetext text NULL)
go
INSERT textie(id, thetext)
VALUES (1, 'This is the original text')
go
DECLARE @ptr varbinary(16),
@offset int
SELECT @ptr = textptr(thetext), @offset = datalength(thetext)
FROM textie
WHERE id = 1

UPDATETEXT textie.thetext @ptr @offset NULL ' (Stylized)'
go
SELECT * FROM textie
go
DROP TABLE textie




--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
  #3  
Old   
ZeldorBlat
 
Posts: n/a

Default Re: UPDATETEXT, WRITETEXT - 06-11-2007 , 10:00 PM



On Jun 11, 5:52 pm, Erland Sommarskog <esq... (AT) sommarskog (DOT) se> wrote:
Quote:
Zamdrist (zamdr... (AT) gmail (DOT) com) writes:
I need to update/change the text in a table column of data type TEXT.
It's a smaller set of records (72) where I need to append onto the end
a string of text, the same for all records.

I don't quite understand how UPDATETEXT and WRITETEXT work, but here
is how I would write the Update query were the field not a TEXT type.

Update Matters Set Description = Description + ' (Stylized)'
From Matters
Inner Join cwconnect On cwconnect.mmatter = matters.matterid

Thoughts how I might do this using WRITETEXT or UPDATETEXT?

Here is a quick example on using UPDATETEXT. Note that you need to
work with one row at a time.

The script retrieves a text pointer which is, well, let's call it a
technology of the eighties. No wonder that Microsoft has deprecated the
text data type in SQL 2005 in favour of varchar(MAX), which is a lot
easier to work with. Anyway, you need this text point when you work
with READTEXT, WRITETEXT and UPDATETEXT. You also need to know the
current length of the string, to tell where to add the new text. The
parameter that is NULL, is a delete-length where you could specify
how many characters that are to be deleted.

CREATE TABLE textie (id int NOT NULL,
thetext text NULL)
go
INSERT textie(id, thetext)
VALUES (1, 'This is the original text')
go
DECLARE @ptr varbinary(16),
@offset int
SELECT @ptr = textptr(thetext), @offset = datalength(thetext)
FROM textie
WHERE id = 1

UPDATETEXT textie.thetext @ptr @offset NULL ' (Stylized)'
go
SELECT * FROM textie
go
DROP TABLE textie

--
Erland Sommarskog, SQL Server MVP, esq... (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 athttp://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books...
Books Online for SQL Server 2000 athttp://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Erland, in my experience I've been able to use normal INSERT and
UPDATE statements on columns of type TEXT without any problem. Why do
you even need to bother with all the READTEXT, WRITETEXT, and
UPDATETEXT stuff anyway? Your insight is appreciated...



Reply With Quote
  #4  
Old   
Zamdrist
 
Posts: n/a

Default Re: UPDATETEXT, WRITETEXT - 06-12-2007 , 08:13 AM



On Jun 11, 10:00 pm, ZeldorBlat <zeldorb... (AT) gmail (DOT) com> wrote:
Quote:
Erland, in my experience I've been able to use normal INSERT and
UPDATE statements on columns of type TEXT without any problem. Why do
you even need to bother with all the READTEXT, WRITETEXT, and
UPDATETEXT stuff anyway? Your insight is appreciated...
It appears my problem comes in when I try to use the + as a
concactenation operator while trying to update a TEXT filed. It
interprets it as an addition operator (lame).



Reply With Quote
  #5  
Old   
Zamdrist
 
Posts: n/a

Default Re: UPDATETEXT, WRITETEXT - 06-12-2007 , 11:09 AM



The answer was to create a table using Select Into containing the new
value of the column I wanted, and to then do a straight up update
column to column, i.e.

Select mmatter, mdesc1 + ' (Stylized)' As mdesc
Into CWStylized from matter
Where (mmatter Like '10799.0062%'
Or mmatter Like '10799.0063%'
Or mmatter Like '10799.0061%')
And Right(mmatter, 2) Not Like 'T[aA-zZ]'
And Right(mmatter, 2) Not Like 'A[aA-zZ]'
And mdesc1 Not Like '%(Stylized)%'

Begin Transaction
Update matter Set mdesc1 = CWStylized.mdesc
Quote:
From CWStylized
Inner Join matter On matter.mmatter = CWStylized.mmatter
Commit Transaction



Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: UPDATETEXT, WRITETEXT - 06-12-2007 , 04:07 PM



Zamdrist (zamdrist (AT) gmail (DOT) com) writes:
Quote:
The answer was to create a table using Select Into containing the new
value of the column I wanted, and to then do a straight up update
column to column, i.e.

Select mmatter, mdesc1 + ' (Stylized)' As mdesc
Into CWStylized from matter
Where (mmatter Like '10799.0062%'
Or mmatter Like '10799.0063%'
Or mmatter Like '10799.0061%')
And Right(mmatter, 2) Not Like 'T[aA-zZ]'
And Right(mmatter, 2) Not Like 'A[aA-zZ]'
And mdesc1 Not Like '%(Stylized)%'

Begin Transaction
Update matter Set mdesc1 = CWStylized.mdesc
From CWStylized
Inner Join matter On matter.mmatter = CWStylized.mmatter
Commit Transaction
I would expect fail for the same reason that the UPDATE failed. I ran:

CREATE TABLE textie (id int NOT NULL,
thetext text NULL)
go
INSERT textie(id, thetext)
VALUES (1, replicate('This is the original text', 2000))
go
SELECT id, thetext + '(Stylized)' AS ggg INTO newtextie FROM textie
go
SELECT datalength(thetext) FROM textie
SELECT datalength(thetext) FROM newtextie
go
DROP TABLE textie, newtextie

And I got:

(1 row(s) affected)
Msg 402, Level 16, State 1, Line 1
The data types text and varchar are incompatible in the add operator.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.