> From: Praveen G Siddavarapu > Sent: Thursday, May 10, 2007 8:45 PM > To: activeperl@listserv.ActiveState.com > Subject: how to run a sql by passing variable > > > All > > I have the below sql, it returns two rows, and I need to pass these > two dates and run another sql, how do I do that.. basically I need to > run the sql two times with two different time periods > > $csr_dates =<<"END_OF_SQL"; > SELECT sysdate beg_date > from dual > union > select sysdate -1 beg_date > from dual > END_OF_SQL > > $csr_dates = $dbh->prepare($sql_dates) > or die "Prepare of csr_dates failed due to $DBI::errstr\n$sql_main\nFile: ".__FILE__."\nLine:". __LINE__. > "\n"; > $csr_dates->execute > or die "Execute of csr_dates failed due to\n"; > ## Fetch ALL the dates data > $dates_ref = $csr_dates->fetchall_arrayref() > or die "Fetch of csr_dates failed due to $DBI::errstr"; > $csr_dates->finish(); > > $sql_contracts =<<"END_OF_SQL"; > select agrmt_id > from agreements > where date_id = '$beg_date' > END_OF_SQL > > Praveen
First, as an aside, when posting code, you should *always* copy/paste the actual code you're using, rather than retyping it (and possibly introducing additional errors). Your second SQL statement should be something like: $sql_contracts = <<END_OF_SQL; select agrmt_id from agreements where date_id = ? END_OF_SQL The "?" is a "placeholder". Notice that it is not surrounded by quotes -- it's not a string that becomes part of the SQL statement; it is passed to the SQL engine as a literal value separate from the statement. Prepare this statement as usual. Then pass the parameter you wish to use in the placeholder as an argument to 'execute': $st_handle->execute($dates_ref[0][1]); See http://search.cpan.org/~timb/DBI-1.55/DBI.pm#Placeholders_and_Bind_Value s HTH, Eric P.S.: You are maybe not aware that die() includes the file name and line number by default?
_______________________________________________ ActivePerl mailing list ActivePerl@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs