PGsql 查表字段名,字段类型

//判断表中是否存在该字段
   SELECT count(1)
   FROM (
     //获取字段名方法一
     SELECT attname 
     FROM pg_catalog.pg_attribute 
     WHERE attstattarget = - 1 
	 AND attrelid IN ( SELECT oid FROM pg_catalog.pg_class WHERE relname = '表名')
     //方法二
     //select * from  information_schema.columns where table_name = '表名' and column_name = '字段名'
    ) t
    where attname='字段名'

    //获取字段类型
    select t."type" 
    from (
       SELECT format_type ( a.atttypid, a.atttypmod ) AS type,
       a.attname AS fname
       FROM pg_class AS c,pg_attribute AS a
       WHERE c.relname = #{tableName}
       AND a.attrelid = c.oid AND a.attnum >0
    ) t
    where t.fname=#{field}

 

posted @   丶Ronnie  阅读(311)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示