Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Appending text to a column in SQL

by imp (Priest)
on Oct 18, 2006 at 02:42 UTC ( [id://578942]=note: print w/replies, xml ) Need Help??


in reply to Appending text to a column in SQL

I no longer have access to Microsoft SQL server 2000 to test with, but you can append to text columns using UPDATETEXT and TEXTPTR

Something like this:

DECLARE @ptrval binary(16) SELECT @ptrval = TEXTPTR(item) FROM table WHERE whatever criteria UPDATETEXT table.item @ptrval NULL 0 'new text';
Not exactly elegant, but it works.

Replies are listed 'Best First'.
Re^2: Appending text to a column in SQL
by sgifford (Prior) on Oct 18, 2006 at 03:17 UTC
    Hi imp,

    Any pointers on how to do that with Perl/DBI? I can't seem to find any examples, and haven't been able to figure out the right syntax...

    Thanks!

      I used a stored procedure for encapsulating that behaviour when I had a similar problem in the past, but if you don't have permission to add stored procedures you should be able to run the sequence of commands inline. I don't have access to a server to test with currently though, so your mileage may vary.

      Something like this:

      my $sql = 'DECLARE @ptrval binary(16);' . ' SELECT @ptrval = TEXTPTR(item)' . ' FROM table ' . ' WHERE whatever criteria;' . ' UPDATETEXT table.item @ptrval NULL 0 ?'; $dbh->do($sql,{},"new text");
      But of course that's horribly ugly.

      So, a stored procedure to encapsulate the behaviour:

      CREATE PROCEDURE dbo.APPEND_THINGY @id int, @new_text varchar(8000) AS DECLARE @ptrval binary(16) SELECT @ptrval = TEXTPTR(item) FROM table WHERE id = @id UPDATETEXT table.item @ptrval NULL 0 @new_text; GO
      And then in your code:
      my $sql = q{exec append_thingy @new_text = ?}; $dbh->do($sql,{},"new text");
        Thanks imp! I finally got to my client's site to try that code out, and it worked perfectly!
        CREATE PROCEDURE dbo.APPEND_THINGY @id int, @new_text varchar(8000) AS Update table SET field = cast(field as varchar(max)) + @new_text where id = @id GO

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://578942]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-24 02:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found