[英]Selecting inserted record in Oracle from PHP OCI
I am executing a PLSQL Block using OCI from my PHP site which is running some procedures inside. 我正在使用PHP站点中的OCI执行PLSQL块,该站点中正在运行某些过程。 The final procedure is returning the inserted records rowid of a specific table. 最后的过程是返回特定表的插入记录rowid。
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? 我想做的是,我想获取该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? 有没有办法像下面这样运行它,并通过oci_fetch_row或其他方式将选择结果输出到PHP?
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, eg by a trigger. 本示例假定在执行INSERT
语句期间以某种方式(例如通过触发器)填充了名为PK_COL
的主键列。 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
. 该RETURNING
的条款INSERT
语句指定的值PK_COL
从插入行应放入指定的变量,在这种情况下nPK_col
。 You can specify multiple columns and variables in the RETURNING
clause - documentation here . 您可以在此处的RETURNING
子句文档中指定多个列和变量。 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
. 您可能需要将其放入执行实际INSERT
任何过程中,然后添加OUT
参数以允许将该值传递回调用方-或使用FUNCTION
而不是PROCEDURE
并将主键值作为返回值FUNCTION
。
Share and enjoy. 分享并享受。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.