kingbase人大金仓常用sql命令(自用总结)
平时用mysql数据库,第一次用人大金仓数据库,有一些地方还是有区别,记录一下。
ALTER TABLE sys_notice MODIFY scope varchar(500)
ALTER TABLE sys_dict_data MODIFY remark varchar(800)
select comments from user_col_comments where table_name='sys_notice' and column_name='scope';
comment on table sys_notice is '通知公告表';
comment on column sys_notice.scope is '发布范围';
REATE SEQUENCE sp_menu_menu_ids_seq
INCREMENT BY 1
START WITH 1046
MINVALUE 1046
MAXVALUE 9999
CACHE 100
NOCYCLE;
然后把序列给id
ALTER TABLE 表名 ALTER COLUMN 字段名 SET DEFAULT NEXTVAL(‘序列名’::REGCLASS);
alter table sp_menu alter column menu_id set default nextval('sp_menu_menu_ids_seq'::REGCLASS)
select now();
SELECT TO_CHAR(now() ,'yyyy-MM-dd HH:mm:ss');
#当前年月日
SELECT TO_CHAR(now() ,'yyyy-MM-dd')
# 时间戳
select current_timestamp;
# 日期
select current_date;
# 时间
select current_time;
select now() - interval '30D';
select now() + interval '2 years';
select now() - interval '1 Months';
select now() - interval '120 Minutes';
select timestampdiff('minute','2018-07-01 09:00:00','2018-07-04 12:00:00')
10.# 时间间隔
age
select age(timestamp '2023-01-27');
# 时间截取
extract
select extract(doy from now());
# 注意是 day of year 的简写
select extract(week from now());
select extract(month from now());
# 部分简写以及含义
Y Years
M Months (in the date part)
W Weeks
D Days
H Hours
M Minutes (in the time part)
S Seconds
select timestampdiff('minute','2023-04-17 10:00:00',now())
select timestampdiff('day','2023-04-10 10:00:00',now())
select timestampdiff('month','2023-03-10 10:00:00',now())
select timestampdiff('year','2012-03-10 10:00:00',now())
需要计算两个日期之间相差的时间,人大金仓不支持TimeStampDiff 函数,需要我们自己在数据库创建TimeStampDiff 函数。要注意:在金仓中minute 这个要加单引号并且要小写,这个是当参数传进来的。
create or replace function timestampdiff(para1 text,para2 timestamp,para3 timestamp) return bigint
as
declare
diff interval day to second;
diffs bigint;
diffm bigint;
diffh bigint;
diffd bigint;
nyy bigint;
ny bigint;
nm bigint;
begin
diff:=para3-para2;
diffs:=extract(second from diff);
diffm:=extract(minute from diff);
diffh:=extract(hour from diff);
diffd:=extract(day from diff);
ny:=extract(year from age(para3,para2));
nm:=extract(month from age(para3,para2));
nyy:=extract(year from para3)-extract(year from para2);
if para1='second' then
return ((diffd*24+diffh)*60+diffm)*60+diffs;
elseif para1='minute' then
return (diffd*24+diffh)*60+diffm;
elseif para1='hour' then
return diffd*24+diffh;
elseif para1='day' then
return diffd;
elseif para1='month' then
return ny*12+nm;
elseif para1='year' then
return nyy;
else
raise warning 'para1 not support "%"!',para1;
return null;
end if;
end;