Sorry about that - PostgreSQL does let you concatenate ('||') text fields. It's been awhile since I had to deal with SQLServer weirdness.

Well then you're either left with running a transaction around your read/append/write or CAST the text field to varchar then concat (assuming the data can be cast).

I would go for the transaction approach, you'll be sure it'll work with any kind of data.

As for the ugliness it should be easy to make a method to hide the $dbh->{AutoCommit} = 0 (or $dbh->begin_work), read, append, update, $dbh->commit. Don't forget to turn AutoCommit off if it's on. In fact I would just add that in there to be safe.

local $dbh->{AutoCommit} = 0; local $dbh->{RaiseError} = 1; # You can RaiseError and eval the transaction # or you can check $sth->err each time and rollback eval { ##Do your read and write here $dbh->commit; }; if ($@) { warn "append failed $@"; $dbh->rollback; }
UPDATE: something similar to this
sub append_to_text { my $dbh = shift; my $table = shift; my $id = shift; my $col = shift; my $data = shift; local $dbh->{RaiseError} = 1; eval { $dbh->begin_work(); my $read = qq| SELECT $col FROM $table WHERE id = ? |; my $update = qq| UPDATE $table SET $col = ? WHERE id = ? |; my $sth_read = $dbh->prepare($read); $sth_read->execute($id); my $row = $sth_read->fetchrow_hashref(); $sth_read->finish(); my $current = $row->{$col}; $current .= $data; my $rtn = $dbh->do($update,undef,$current,$id); $dbh->commit() if ($rtn); }; if ($@) { warn "Append Failed: $@\nStatement: $DBI::lasth->{Statement} +"; eval { $dbh->rollback }; } }


grep
One dead unjugged rabbit fish later

In reply to Re^3: Appending text to a column in SQL by grep
in thread Appending text to a column in SQL by sgifford

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.