Este es el equivalente de postgres
Especifique TABLE_SCHEMA
y MASKTABLE
debajo
Estoy creando una MASKTABLE
con una primera entrada ficticia
DELIMITER $$
CREATE OR REPLACE FUNCTION generate_next_id() RETURNS bigint NOT DETERMINISTIC
MAIN: BEGIN
DECLARE our_epoch bigint;
DECLARE seq_id bigint;
DECLARE now_millis bigint;
DECLARE shard_id int;
DECLARE param bigint ;
SET @our_epoch = 1568873367231;
SET @shard_id = 1;
SELECT AUTO_INCREMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = "SCHEMANAME" AND TABLE_NAME = "MASKTABLE" into @seq_id;
SELECT FLOOR(UNIX_TIMESTAMP()) * 1000 into @now_millis;
SELECT (@now_millis - @our_epoch) << 23 into @param;
SELECT @param | (@shard_id <<10) into @param;
select @param | (@seq_id) into @param;
RETURN @param;
END MAIN;$$
DELIMITER ;
Uso
select generate_next_id()
Puedes usarlo en disparadores como
CREATE TRIGGER trigger_name
BEFORE INSERT ON TableName
FOR EACH ROW
SET new.id = generate_next_id();