postgresql 如何使用plpgsql检查值是否为整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1399321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:51:28 来源:igfitidea点击:
How to check, if a value is an integer with plpgsql?
提问by return1.at
i am using this function in a trigger:
我在触发器中使用这个函数:
CREATE OR REPLACE FUNCTION xx() RETURNS trigger AS $xx$
BEGIN
INSERT INTO my_log (x, y, z) VALUES (NEW.x, NEW.y, current_setting('myvar.user'));
RETURN NULL;
END;
$xx$ LANGUAGE plpgsql;
now i would like to check, if 'myvar.user' is a valid integer, and if not, do another INSERT statement.
现在我想检查一下“myvar.user”是否是一个有效的整数,如果不是,再做一个 INSERT 语句。
how would i do this?
我该怎么做?
回答by Quassnoi
SELECT current_setting('myvar.user') ~ '^[0-9]+$'
回答by Tomalak
Taken from archives.postgresql.org:
CREATE FUNCTION isnumeric(text) RETURNS boolean AS '
SELECT ~ ''^[0-9]+$''
' LANGUAGE 'sql';