Update: I can no longer replicate the original problem with inserting strings with ampersands. The prepare/execute approach with bind variables (suggested by [id://pajout] and [id://kwaping]) worked, but then, so did my original insert. I am tentatively concluding that, as [id://Roy Johnson] and [id://jhourcle] suggest, my problem was entirely within SQLPlus, and that the error I saw in my Perl DBI script execution was something else (probably a referential integrity issue). Presumably when I followed up on that original error by pasting the offending statement into SQLPlus, I was fooled by the &var replacement red herring.

Either that or evil database gnomes have been at work during the night, in an unnecessary effort to discredit me.

Thank you for all of your suggestions and help.

=================================

Original post: I have some data which I need to insert into an Oracle table; the data has embedded ampersands in it, which causes Oracle to prompt for a parameter (and my insert to fail). Using SQLPlus, I see that I can either:

"set define off" or

"set escape \" and then escape the ampersand with a preceding backslash.

But I can't seem to get this to work through the DBI. Here's what I have tried so far:

my $data_source = "dbi::Oracle:$sid"; my $sqlhandle = DBI->connect("dbi:Oracle:host=$host;sid=$sid;port=15 +21", $user, $password, { RaiseError => 1, AutoCommit => 0 }) or die "Can't connect to $data_source: $DBI::errstr"; my $func = $sqlhandle->prepare(q{ BEGIN set define off; END; }); my $result = $func->execute();
I'm getting an error message much like this one:
DBD::Oracle::st execute failed: ORA-06550: line 3, column 17: PL/SQL: ORA-00922: missing or invalid option ORA-06550: line 3, column 13: PL/SQL: SQL Statement ignored (DBD ERROR: error possibly near <*> indi +cator at char 31 in ' BEGIN set <*>define off; END; ') [for Statement " BEGIN set define off; END; "] at /blah/blah/Blah.pm line 348, <BLAHFILE> line 104133.

Any help or suggestions would be greatly appreciated.


No good deed goes unpunished. -- (attributed to) Oscar Wilde

In reply to Executing Oracle PL/SQL statements through the DBI by ptum

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.