【发布时间】:2017-02-13 16:56:05
【问题描述】:
我正在编写一个 perl 脚本,它从文件中读取社会安全号码,在多个表中查找信息并输出到分隔文件。我正在使用 PERL 5 编写并处理 IBM informix。我觉得问题出在我的正则表达式上。我收到多行以下错误:
DBD::Informix::st 执行失败:SQL:-1213:字符到数字的转换过程在 ./corylist.pl 第 61 行第 461 行失败。 DBD::Informix::st fetchrow_array 失败:SQL:-400:在未打开的游标上尝试获取。在 ./corylist.pl 第 63 行,第 461 行。
有人可以将我推向正确的方向吗?谢谢!
while(<IN>) {
$id = $_;
chomp $id;
$id =~ m/^\d{3}-\d{2}-\d{4}$/;
#print "$id\n";
$STMT = <<EOF;
select i.ss_no,
i.fullname, i.firstname,i.lastname,i.addr_line1,i.addr_line2,i.city,i.st,i.zip,r.res_ctry,r.res_cty,
i.phone,NVL(aa.phone," ") cell,NVL(a.line1," ") stuemail,NVL(pa.line1," ") peremail
from id i,
prof r,
outer aa_rec a,
outer aa_rec aa,
outer aa_rec pa
where i.ss_no = $id
and i.id = r.id
and i.decsd <> "Y"
and a.id = i.id and a.aa = "EML" and a.end_date is null
and pa.id = i.id and pa.aa = "OEML" and pa.end_date is null
and pa.beg_date = (select max(beg_date) from aa_rec where aa = "OEML" and id=$id and end_date is null)
and aa.id = i.id and aa.aa = "CELL" and aa.end_date is null
group by ss_no,fullname,firstname,lastname,addr_line1,addr_line2,city,st,zip,res_ctry,res_cty,phone,cell,stuemail,peremail
order by fullname, ss_no
EOF
$sth = $db1->prepare($STMT);
$sth->execute();
while (($id,$fullname,$fname,$lname,$addr1,$addr2,$city,$st,$zip,$ctry,$cnty,$phone,$cell,$stuemail,$peremail) = $sth->fetchrow_array()) {
$x = $id." | ". $fullname." | ";
$x .= $fname." | ".$lname." | ".$addr1." | ".$addr2." | ".$city." | ".$st." | ".$zip." | ".$ctry." | ".$cnty." | ";
$x .= $phone." | ".$cell." | ".$stuemail." | ".$peremail." | \n";
print $out_fh $x;
}
【问题讨论】:
-
你的正则表达式只匹配某些东西,但你从不对其采取行动。你不替代,你不捕获。那条线基本上没用。您在使用
$id的SQL 中也没有引号。您应该改用占位符。从错误消息中我会说它是一个数字字段,所以你需要去掉破折号。 -
你知道
$x .= $fname." | ".$lname." | ".$addr1." | ".$addr2." | ".$city." | ".$st." | ".$zip." | ".$ctry." | ".$cnty." | "可以写成$x .= "$fname|$lname|$addr1|$addr2|$city|$st|$zip|$ctry|$cnty|"吗? -
在
while循环中每次都使用相同的语句prepare是一种浪费。$sth = $db1->prepare($STMT)应该移到块外。 -
@Borodin 我什至没有看到
prepare在循环中。我考虑过建议join '|', ...,但是很好。 Text::CSV 是。 -
@simbabque:我并不感到惊讶。最后一个右括号实际上是向下一层。大括号不匹配。
标签: sql regex perl informix isql