sql脚本函数编写postgresql数据库实现解析

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

sql脚本函数编写postgresql数据库实现解析

火鸦哈士奇   2020-12-15 我要评论
这篇文章主要介绍了sql脚本函数编写postgresql数据库实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

这篇文章主要介绍了sql脚本函数编写postgresql数据库实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

postgresql创建Long自增函数

CREATE SEQUENCE global_id_sequence;

CREATE OR REPLACE FUNCTION seq_id(OUT result bigint) AS $$
DECLARE
  our_epoch bigint := 1314220021721;
  seq_id bigint;
  now_millis bigint;
  -- the id of this DB shard, must be set for each
  -- schema shard you have - you could pass this as a parameter too
  shard_id int := 1;
BEGIN
  SELECT nextval('global_id_sequence') % 1024 INTO seq_id;

  SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
  result := (now_millis - our_epoch) << 23;
  result := result | (shard_id << 10);
  result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;

<!--alter function seq_id(out bigint) owner to postgres;-->

创建sql脚本postgresql

do language plpgsql
$$
begin
update grade set grade = 90 where sno = '20161003';
end
$$

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们