$sql = "INSERT INTO table_name( field1, field2 ) VALUES ('foo','bar')
RETURNING ID INTO :mylastid";
$stmt = oci_parse($db, $sql);
oci_bind_by_name($stmt, "mylastid", $last_id, 8, SQLT_INT);
oci_execute($stmt);
echo "last inserted id is:".$last_id;
Tip: you have to use your id column name in {your_id_col_name} below...
"RETURNING {your_id_col_name} INTO :mylastid"
SELECT * FROM
MY_TABLE
WHERE
<your filters>
ORDER BY PRIMARY_KEY DESC FETCH FIRST ROW ONLY
The last row according to a strict total order over composite key K(k1, ..., kn):
SELECT *
FROM TableX AS o
WHERE NOT EXISTS (
SELECT *
FROM TableX AS i
WHERE i.k1 > o.k1
OR (i.k1 = o.k1 AND i.k2 > o.k2)
...
OR (i.k1 = o.k1 AND i.k2 = o.k2 AND i.k3 = o.k3 AND ... AND i.kn > o.kn)
)
;
Given the special case where K is simple (i.e. not composite), the above is shortened to:
SELECT *
FROM TableX AS o
WHERE NOT EXISTS (
SELECT *
FROM TableX AS i
WHERE i.k1 > o.k1
)
;
Note that for this query to return just one row the key must order without ties. If ties are allowed, this query will return all the rows tied with the greatest key.
select * from table_name ORDER BY primary_id DESC FETCH FIRST 1 ROWS ONLY;
That's the simplest one without doing sub queries
You can do it like this:
SELECT * FROM (SELECT your_table.your_field, versions_starttime
FROM your_table
VERSIONS BETWEEN TIMESTAMP MINVALUE AND MAXVALUE)
WHERE ROWNUM = 1;
Or:
SELECT your_field,ora_rowscn,scn_to_timestamp(ora_rowscn) from your_table WHERE ROWNUM = 1;
SELECT * FROM (
SELECT * FROM table_name ORDER BY sortable_column DESC
) WHERE ROWNUM = 1;
Source: Stackoverflow.com