一个好用的函数DBINFO

  在日常SinoDB运维工作中,发现一个很便捷很强大的函数DBINFO,通过SQL语句和function等获取数据库相关信息,结合运维和开发工作可带来很多便利,介绍给大家。

常用的几种方式如下:

1.获取dbspace信息:
>select dbinfo(‘dbspace’,partnum) from systables where tabname=‘h_history’;
(expression) datadbs

2.获取当前连接数据库的会话信息:

select dbinfo(‘sessionid’) from sysmaster:sysdual;
(expression) 1739808

3.获取时区信息:

select dbinfo(‘get_tz’) from sysmaster:sysdual;
(expression) Asia/Shanghai

4.获取当前的数据库名:

select dbinfo(‘dbname’) from sysmaster:sysdual;
(expression) testdb

5.获取DML和SELECT作用于表返回的条数
dbinfo(‘sqlca.sqlerrd2’)

应用例子:
CREATE FUNCTION sel_rows ()
RETURNING INT;
DEFINE nrows INT;
select * FROM t1 into temp tmp_t1 with no log;
LET nrows = DBINFO(‘sqlca.sqlerrd2’);
RETURN nrows;
END FUNCTION;

执行函数:

execute function sel_rows();
(expression) 3

6.获取数据库当前时间:

select dbinfo(‘UTC_TO_DATETIME’,sh_curtime) from sysmaster:sysshmvals;
(expression) 2021-08-08 15:16:50

比较全的用法,可参考以下表格:

Arguments Information Returned Topic
(‘dbhostname’) The host name of the database server to which a client application is connected Using the ‘dbhostname’ Option
(‘dbname’) The identifier of the database to which a client application is connected Using the ‘dbname’ Option
(‘dbspace’ tblspace_num) The name of a dbspace corresponding to a tblspace number Using the (‘dbspace’, tblspace_num) Option
(‘get_tz’) The time zone of the session, $TZ, as specified as a string by the client. Using the ‘get_tz’ Option
(‘serial8’) The last SERIAL8 value inserted in a table Using the ‘serial8’ and ‘bigserial’ options
(‘bigserial’) The last BIGSERIAL value inserted in a table Using the ‘serial8’ and ‘bigserial’ options
(‘sessionid’) The session ID number of the current session Using the ‘sessionid’ Option
(‘cdrsession’) Whether a thread is performing an Enterprise Replication operation Using the ‘cdrsession’ option
(‘sqlca.sqlerrd1’) The last SERIAL value inserted in a table Using the ‘sqlca.sqlerrd1’ Option
(‘sqlca.sqlerrd2’) The number of rows processed by SELECT, INSERT, DELETE, UPDATE, EXECUTE PROCEDURE, and EXECUTE FUNCTION statements Using the ‘sqlca.sqlerrd2’ Option
(‘utc_current’) The current UTC time value (as an integer number of seconds since 1970-01-01 00:00:00+00:00) when the SQL statement began to execute. Using the ‘utc_current’ Option
(‘utc_to_datetime’, table.column) The DATETIME value corresponding to a specified integer column containing a UTC time value (as an integer number of seconds since 1970-01-01 00:00:00+00:00). Using the ‘utc_to_datetime’ Option
(‘utc_to_datetime’, utc_value) The DATETIME value corresponding to a specified UTC time value (as an integer number of seconds since 1970-01-01 00:00:00+00:00). Using the ‘utc_to_datetime’ Option
(‘version’, ‘parameter’) Type of the database server and its release version to which the client application is connected. (The call to DBINFO fails with an error if no parameter specifies a format for the version information.) Using the ‘version’ Option