2

ここで、レコードを挿入し、最後に挿入されたシーケンス ID を取得しようとしていますが、成功しませんでした。オラクルが 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;
        }
4

2 に答える 2

4

挿入ステートメントの後、実行できselect id.currval from dualます。これにより、最新の値が得られるはずです。これは、 nextval を取得した後の現在のセッションでのみ機能し、単独では使用できないことに注意してください。

于 2013-07-28T11:03:30.357 に答える
2

以下を挿入 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);

が実行され$idた後、最後に挿入された ID を持つようになりました。oci_execute($this->stmt)

このメソッドは OCI8 でうまく機能します。

于 2016-03-09T20:59:13.273 に答える