SQL System Information Functions
System Information Functions
Several functions are available that extract session and system information.
Function | Description | ||
---|---|---|---|
CURRENT_SCHEMA() |
Returns the name of the schema that will be used for any tables or other named objects that are created without specifying a target schema.
|
||
CURRENT_USER() or USER() |
Returns the name of the user identifier that is applicable for permission checking.
|
||
DATABASE() |
Returns the name of the database.
|
||
GETCOMMITPROTOCOL() |
Returns the commit protocol the transaction is using.
|
||
GETCONNECTIONID() |
Returns the ID of the current database connection.
Details about database connections are stored in the
|
||
GETEFFECTIVEPLATFORMVERSION() |
Returns the effective version of the database.
This is different than the NuoDB product release version. However, these two versions are closely related.
|
||
GETNODEID() |
Returns the unique ID of the Transaction Engine (TE) that serves the local connection.
Details of system nodes are stored in the
|
||
GETPID() |
Returns the unique operating system process ID (PID) of the TE that serves the local connection.
|
||
GETRELEASEVERSION() |
Returns the current NuoDB release version
|
||
GETTRANSACTIONID() |
Returns the unique identifier for the current transaction.
|
||
GETUPDATECOUNT() |
Returns the number of rows that were affected by the last
|
||
IS_MEMBER( |
Indicates if the current user has been granted privileges specified by
|
||
LAST_INSERT_ID() |
Returns the last automatically generated ID for a column declared with
|
||
OPTIONAL_FIELD( |
Returns the value defined by the
|
||
ROWNUM() |
Returns a number indicating the order in which the row was selected from a table or set of joined rows.
|
||
SCOPE_IDENTITY() |
Returns the last identity values that are generated in any table in the current session, restricted to values within the current scope.
The current scope is a stored procedure, trigger, function, or batch.
Two statements are in the same scope if they are in the same stored procedure, function, or batch. Whereas
|
Examples
Example showing CURRENT_USER
SELECT CURRENT_USER() FROM dual;
[CURRENT_USER]
---------------
DBA
# Work log example
CREATE TABLE work_log (tablename STRING, columnname STRING, change_desc STRING);
INSERT INTO work_log VALUES ('HOCKEY','NUMBER','Changed from integer to string');
SELECT CURRENT_USER() AS changed_by,tablename,columnname,change_desc FROM work_log;
CHANGED_BY TABLENAME COLUMNNAME CHANGE_DESC
----------- ---------- ----------- ------------------------------
DBA HOCKEY NUMBER Changed from integer to string
Example for IS_MEMBER
CREATE ROLE qauser;
SELECT IS_MEMBER('qauser') FROM dual;
[IS_MEMBER]
------------
0
GRANT qauser TO dba;
SELECT IS_MEMBER('qauser') FROM dual;
[IS_MEMBER]
------------
1
Example for LAST_INSERT_ID
# LAST_INSERT_ID Examples
CREATE TABLE t (id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY, name VARCHAR(10));
INSERT INTO t (name) VALUES ('User1');
SELECT * FROM t;
ID NAME
--- -----
1 User1
SELECT LAST_INSERT_ID() FROM dual;
[LAST_INSERT_ID]
-----------------
1
# If multiple rows are inserted, returns the id of the first one.
INSERT INTO t (name) VALUES ('User2'), ('User3');
SELECT * FROM t;
ID NAME
--- -----
1 User1
2 User2
3 User3
SELECT LAST_INSERT_ID() FROM dual;
[LAST_INSERT_ID]
-----------------
2
Example showing LAST_INSERT_ID
versus SCOPE_IDENTITY
CREATE TABLE t1 (id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY, name VARCHAR(10));
CREATE TABLE t2 (id INT GENERATED ALWAYS AS IDENTITY NOT NULL PRIMARY KEY, name VARCHAR(10));
INSERT INTO t1 (name) VALUES ('greg');
INSERT INTO t2 (name) VALUES ('marcia'), ('peter'), ('jan'), ('bobby'), ('cindy');
SELECT LAST_INSERT_ID() FROM dual;
[LAST_INSERT_ID]
-----------------
1
SELECT SCOPE_IDENTITY() FROM dual;
[SCOPE_IDENTITY]
-----------------
5
Example for ROWNUM
CREATE TABLE t1 (a INT, b INT);
INSERT INTO t1 VALUES (1,1),(2,2),(3,3),(10,10),(11,11),(12,12);
SELECT a, ROWNUM() FROM t1;
A [ROWNUM]
-- ---------
1 1
2 2
3 3
10 4
11 5
12 6
Example for OPTIONAL_FIELD
CREATE TABLE my_table
(id INTEGER PRIMARY KEY
,description STRING);
INSERT INTO my_table VALUES
(1,'row-1')
,(2,'row-2');
SELECT id,description FROM my_table;
ID DESCRIPTION
--- ------------
1 row-1
2 row-2
/* Before schema Upgrade. Column SOURCE does not exist */
/* For now, we can execute code and provide a default value */
SELECT id,description,OPTIONAL_FIELD(source,'Batch') FROM my_table;
ID DESCRIPTION ['Batch']
--- ------------ ----------
1 row-1 Batch
2 row-2 Batch
/* Schema upgrade */
ALTER TABLE my_table ADD source STRING;
/* Column SOURCE now exists, but has NULL value */
/* The actual column value is returned */
SELECT id,description,OPTIONAL_FIELD(source,'batch') FROM my_table;
ID DESCRIPTION MY_TABLE.SOURCE
--- ------------ ----------------
1 row-1 <null>
2 row-2 <null>
UPDATE my_table SET source = CASE ID WHEN 1 then 'Batch' ELSE 'Online' END;
SELECT id,description,OPTIONAL_FIELD(source,'batch') FROM my_table;
ID DESCRIPTION MY_TABLE.SOURCE
--- ------------ ----------------
1 row-1 Batch
2 row-2 Online