占位符使用DBI

时间:2017-01-06 05:55:36

标签: sql database dbi prepare

我想在下面的查询中使用占位符(3-01-2017)。如何做到这一点?下面是我在脚本中使用DBI的查询。

$sth = $dbh->prepare("select  CODE_ID,NAME_CODE,SUM(INR_COL + OUT_COL) AS \"TOTAL SUM\"from nwsa where trunc(REPORT_DATE)=to_date('3-01-2017,'dd-mm-yyyy) AND (CODE_ID='A12A' OR CODE_ID='A12B'OR CODE_ID='A12C' OR CODE_ID='A12D' OR CODE_ID='A12E' OR CODE_ID='A12EB' OR CODE_ID='A12F' OR CODE_ID='A12G' OR CODE_ID='A12I' OR CODE_ID='A12O' OR CODE_ID='A12U' ) group by  CODE_ID,NAME_CODE");  # your query here
$sth->execute( );
----this is my complete query ---
SUM(INR_COL + OUT_COL) AS "TOTAL SUM"
FROM nwsa
WHERE trunc(REPORT_DATE) = to_date('3-01-2017','dd-mm-yyyy')
AND (CODE_ID='A12A'
OR CODE_ID   ='A12B'
OR CODE_ID   ='A12C'
OR CODE_ID   ='A12D'
OR CODE_ID   ='A12E'
OR CODE_ID   ='A12EB'
OR CODE_ID   ='A12F'
OR CODE_ID   ='A12G'
OR CODE_ID   ='A12I'
OR CODE_ID   ='A12O'
OR CODE_ID   ='A12U')
GROUP BY CODE_ID,
  NAME_CODE
  ;

1 个答案:

答案 0 :(得分:0)

my $t= '06-01-2017';
my $sql2 = "SELECT CODE_ID,NAME_CODE,SUM(INR_COL + OUT_COL) AS \"TOTAL SUM\" FROM nwsa WHERE trunc(REPORT_DATE) = to_date(?,'dd-mm-yyyy')AND CODE_ID IN (?,?,?,?,?,?,?,?,?,?,?)GROUP BY CODE_ID,NAME_CODE"; 
 $sth =$dbh->prepare($sql2);


 $sth->execute($t,'A12A','A12B','A12C','A12D','A12E','A12EB','A12F','A12G','A12I','A12O','A12U');
 $out = DBIx::Dump->new('format' => csv,   # excel or csv
                        'output' => $FILENAME1, # file to save as
                        'sth'    => $sth);
 $out->dump();