赞
踩
CREATE TABLE sequence (
seqname VARCHAR(50) NOT NULL,
currentValue INT NOT NULL,
increment INT NOT NULL DEFAULT 1,
PRIMARY KEY (seqname)
);
INSERT INTO sequence VALUES ('seq_host_id',100,1);
CREATE FUNCTION currval(seq_name VARCHAR(50))
RETURNS INT
BEGIN
DECLARE value INT;
SELECT currentValue INTO value
FROM sequence
WHERE upper(seqname) = upper(seq_name);
RETURN value;
END;
CREATE FUNCTION nextval (seq_name VARCHAR(50))
RETURNS INT
BEGIN
DECLARE value INT;
UPDATE sequence
SET currentValue = currentValue + increment
WHERE upper(seqname) = upper(seq_name);
RETURN currval(seq_name);
END;
CREATE FUNCTION setval (seq_name VARCHAR(50), value INT)
RETURNS INT
BEGIN
UPDATE sequence
SET currentValue = value
WHERE upper(seqname) = upper(seq_name);
RETURN currval(seq_name);
END;
使用示例:
select currval('seq_host_id');
select setval('seq_host_id',102);
select nextval('seq_host_id');
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。