Today I found a sql to check the modification of a table, it looks like below:
SELECT MODEL_ID, STATUS_ID, COUNT(*) as QUANTITY, ORA_ROWSCN as CHANGE_NUM
FROM DEVICE
GROUP BY MODEL_ID, STATUS_ID
When I added one record to DEVICE table, the QUANTITY of the result increased but CHANGE_NUM didn’t change.
Then I was surprised to found that the ORA_ROWSCN was not part of GROUP BY clause, is it because ORA_ROWSCN is a pseudocolumn?
When I changed ORA_ROWSCN to MAX(ORA_ROWSCN), the CHANGE_NUM increased as expected.
So what’s the ORA_ROWSCN we got in the first sql? The first change num of a group? And what’s the meaning of this usage?
PS: I’m using Oracle Database 11g
Yes. You could replace ORA_ROWSCN with another pseudo-column like SYSDATE and your query would still work.
According to the documentation, “Oracle tracks SCNs by transaction committed for the block in which the row resides.” (emphasis mine). So, if you are not tracking the SCN with ROWDEPENDENCIES (and that is the default) then the value you’ll see for ORA_ROWSCN is the SCN of the last committed transaction. The CHANGE_NUM won’t change until someone (not necessarily you) issues a COMMIT.
Note that if you had have been using a table with ROWDEPENDENCIES the ORA_ROWSCN function would have returned null, until you issued a commit.
In either case, when you execute an aggregated query the value of ORA_ROWSCN is the maximum for the block (or table – I think – when using ROWDEPENDENCIES).