检查Postgres(plpgsql)中是否存在序列 [英] Check if sequence exists in Postgres (plpgsql)

查看:650
本文介绍了检查Postgres(plpgsql)中是否存在序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在存储过程中测试序列是否已经存在.

I'm trying to test, within a stored procedure, whether a sequence already exists.

IF EXISTS SEQUENCE seq_name
    RAISE EXCEPTION 'sequence % already exists!', seq_name
END IF;

我尝试了上面几段代码的几种变体,但没有带来好运.我一定给Google加上了错误的字眼,因为我似乎找不到关于此主题的任何内容.感谢您的帮助!

I have tried several variations of the snippet above without luck. I must be giving Google the wrong terms because I can't seem to find anything on the topic. Any help is appreciated!

推荐答案

您应该能够查询pg_class表以查看relname是否存在.

You should be able query the pg_class table to see if the relname exists.

IF EXISTS (SELECT 0 FROM pg_class where relname = '<my sequence name here>' )
THEN
  --stuff here
END IF;

这篇关于检查Postgres(plpgsql)中是否存在序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆