1. 程式人生 > 資料庫 >sql指令碼函式編寫postgresql資料庫實現解析

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
$$

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。