asafp has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am trying to create dynamic SQL <query.
First, I build a query with placeholders, something like:
my $query = "INSERT INTO $table_name (F1,F2,F3...) VALUES (?,?,?,...)" +;
And then I bind the values using the following code:
my $sth = $dbh->prepare($query); my @params = @$bind_params; my ($i,$j); for ($i = 0,$j=1; $i <= $#params;$i++,$j++){ $sth->bind_param($j,$params[$i]); } $sth->execute();
Now, I get the followong DBA error:
ORA-01858: a non-numeric character was found where a numeric was expec +ted (DBD ERROR: error possibly near <*> indicator at char 173 in 'INS +ERT INTO user_session (auth_id,expiry_date,remote_address,num_days,ve +rification,creation_date,login_id,inst_code,handle,last_update_date) +VALUES (:p1,:p2,:p3,:p4,:p5,:<*>p6,:p7,:p8,:p9,:p10)') [for Statement + "INSERT INTO user_session (auth_id,expiry_date,remote_address,num_da +ys,verification,creation_date,login_id,inst_code,handle,last_update_d +ate) VALUES (?,?,?,?,?,?,?,?,?,?)" with ParamValues: :p1="user1name", + :p10='sysdate', :p2='to_date('22-05-2011 23:59:59','DD-MM-YYYY HH24: +MI:SS')', :p3='il-pr01.corp.mygroup.com:8991/prs;10.1.116.190;', :p4= +0, :p5='fencpw('user1pass')',:p6='sysdate', :p7='user1name', :p8='USM +50', :p9="22520111126163902333386268343"]

It seems that there is a problem in using reserved words e.g. SYSDATE or TO_DATE.
The bind function treats them as a string.
Is there a way to solve this?
Thanks,
Asaf

Replies are listed 'Best First'.
Re: DBI bind_param
by moritz (Cardinal) on May 22, 2011 at 09:42 UTC
    non-numeric character was found where a numeric was expected

    You are passing in a string ('sysdate') where the database wants a number (probably based on the type of the column). If you don't want to treat the argument verbatim, but rather have it interpreted as an SQL command, don't use a placeholder for it.

    Update: or even better, have a default in your DB schema and only insert a value for it if you want to pass a non-default value.