Вопрос

I am executing a PLSQL Block using OCI from my PHP site which is running some procedures inside. The final procedure is returning the inserted records rowid of a specific table.

BEGIN

  proc1(1);
  proc2(2, rowid_);

END;

What I want to do is, I want to get the primary key values of the record for this rowid?

Is there a way to run it somehow like below and get the select results out to PHP with oci_fetch_row or something?

BEGIN

  proc1();
  proc2(rowid_); -- out variable

 SELECT column1, column2
 FROM my_table
 WHERE rowid = rowid_;

END;
Это было полезно?

Решение

There's a better way. Try something like:

DECLARE
  nPK_col  NUMBER;
  nCol1    NUMBER := 1;
  nCol2    NUMBER := 2;
BEGIN
  INSERT INTO SOME_TABLE(COL1, COL2)
    VALUES (nCol1, nCol2)
    RETURNING PK_COL INTO nPK_col;
END;

This example assumes that the primary key column named PK_COL is populated in some way during the execution of the INSERT statement, e.g. by a trigger. The RETURNING clause of the INSERT statement specifies that the value of PK_COL from the inserted row should be put into the variable specified, in this case nPK_col. You can specify multiple columns and variables in the RETURNING clause - documentation here. You may need to put this into whatever procedure performs the actual INSERT and then add an OUT parameter to allow the value to be passed back to the caller - or use a FUNCTION instead of a PROCEDURE and have the primary key value be the return value of the FUNCTION.

Share and enjoy.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top