如何用php获取oracle11gxe中最后一条插入记录的序列id


how to get sequence id of last inserted record in oracle 11g xe with php?

在这里,我试图插入一条记录以及检索上次插入的序列id,但没有成功。有人能帮助我,指导我oracle如何使用php吗?

$query = 'INSERT INTO hist_news (id,headline,reportedon,reportedby,loc,story,more,helperjson,refjson,createdt,createdby) VALUES(id.nextval, :headline, :reportedon, :reportedby , :loc , :story , :more , :helper , :ref , sysdate , :createdby) return id.nextval';
        $this->stmt = oci_parse($this->oci,$query);
        oci_bind_by_name($this->stmt,':headline',$headline);
        oci_bind_by_name($this->stmt,':reportedon',$reportedon);
        oci_bind_by_name($this->stmt,':reportedby',$reportedby);
        oci_bind_by_name($this->stmt,':loc',$loc);
        oci_bind_by_name($this->stmt,':story',$story);
        oci_bind_by_name($this->stmt,':more',$more);
        oci_bind_by_name($this->stmt,':helper',$helperjson);
        oci_bind_by_name($this->stmt,':ref',$refjson);
        if($re = oci_execute($this->stmt)){
            return $re;
        } else {
            return false;
        }

插入语句后,可以执行select id.currval from dual。这将为您提供最新的值。请注意,这仅在获取nextval后的当前会话中有效,不能单独使用。

将以下内容添加到插入SQL查询中:

returning id into :id

示例:

$query = 'INSERT INTO hist_news (id,headline,reportedon,reportedby,loc,story,more,helperjson,refjson,createdt,createdby) VALUES(id.nextval, :headline, :reportedon, :reportedby , :loc , :story , :more , :helper , :ref , sysdate , :createdby) returning id into :id';

并将其与您的报表绑定

oci_bind_by_name($this->stmt,":ID",$id);

现在CCD_ 2将在执行CCD_ 3之后具有最后插入的ID;

这种方法适用于OCI8。