当前位置:网站首页>The PG function generates the corresponding table creation statement according to the table name
The PG function generates the corresponding table creation statement according to the table name
2022-08-07 07:06:00 【tall boy】
CREATE OR REPLACE FUNCTION "ap"."generate_create_table_statement"("p_table_name" varchar)
RETURNS "pg_catalog"."text" AS $BODY$
DECLARE
v_table_ddl text;
column_record record;
BEGIN
FOR column_record IN
SELECT
b.nspname as schema_name,
b.relname as table_name,
a.attname as column_name,
pg_catalog.format_type(a.atttypid, a.atttypmod) as column_type,
CASE WHEN
(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) IS NOT NULL THEN
'DEFAULT '|| (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef)
ELSE
''
END as column_default_value,
CASE WHEN a.attnotnull = true THEN
'NOT NULL'
ELSE
'NULL'
END as column_not_null,
a.attnum as attnum,
e.max_attnum as max_attnum
FROM
pg_catalog.pg_attribute a
INNER JOIN
(SELECT c.oid,
n.nspname,
c.relname
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relname ~ ('^('||p_table_name||')$')
AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 2, 3) b
ON a.attrelid = b.oid
INNER JOIN
(SELECT
a.attrelid,
max(a.attnum) as max_attnum
FROM pg_catalog.pg_attribute a
WHERE a.attnum > 0
AND NOT a.attisdropped
GROUP BY a.attrelid) e
ON a.attrelid=e.attrelid
WHERE a.attnum > 0
AND NOT a.attisdropped
ORDER BY a.attnum
LOOP
IF column_record.attnum = 1 THEN
v_table_ddl:='CREATE TABLE '||column_record.schema_name||'.'||column_record.table_name||' (';
ELSE
v_table_ddl:=v_table_ddl||',';
END IF;
IF column_record.attnum <= column_record.max_attnum THEN
v_table_ddl:=v_table_ddl||chr(10)||
' '||column_record.column_name||' '||column_record.column_type||' '||column_record.column_default_value||' '||column_record.column_not_null;
END IF;
END LOOP;
v_table_ddl:=v_table_ddl||');';
RETURN v_table_ddl;
END;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100
边栏推荐
- Codeforces暑期训练周报(7.28~8.3)
- FPGA开发第四弹:触摸按键控制LED灯实验
- 好消息|Erda 加入中国开源社区 landscape
- 【Promise】Promise 使用 / 回调地狱问题 async-await /宏队列与微队列
- grid grid layout
- grid网格布局
- 【问题记录】一次由filter引发的血案,如何定位上游链路的问题,问题排查与定位思路分享
- DeFi Prospects: An Overview of Q2 Progress of Mainstream DeFi Protocols
- A Pursuit of Temporal Accuracy in General Activity Detection TAG论文阅读笔记
- Buffer and cache in memory
猜你喜欢

程序员福音,关于如何使用Markdown写出一份漂亮的简历 —— 程序员简历 | md文档简历制作教程

VoLTE Basic Self-Learning Series | IMS Network Overview

用户登录模块---Druid+JDBC+Servlet

神经网络ppt不足之处怎么写,神经网络ppt免费下载

Detailed explanation of fixture test fixture of pytest framework

哈希-闭散列

哈希——开散列

图论与网络模型——基于R

Some basic concepts in networking

WeChat applet--" applet global configuration and detailed explanation pull-down refresh and pull-up bottom page events
随机推荐
LeetCode 1163. 按字典序排在最后的子串
排序--冒泡排序
Routing, network, Internet, Internet, public network private network IP, NAT technology
Sort -- bubble sort
VoLTE Basic Self-Learning Series | What is Forking in SIP and IMS
inux安装软件命令yum,apt-get
8 月数据库排行榜:Oracle 分数大跌,MySQL 上涨最多
神经网络ppt不足之处怎么写,神经网络ppt免费下载
HPC技术:MPICH实现原理分析
OS模块中获取当前文件的绝对路径的相关方法
VoLTE basic self-study series | RTP and RTCP protocol principles
【IO复用】select(处理读/写/异常事件)
传输层(UDP协议,TCP协议三次握手、四次挥手)
HyperBDR云容灾深度解析二:自研Boot in Cloud技术,实现高度自动化云容灾
[NOIP2001 提高组] 数的划分
如何搭建神经网络模型,多层全连接神经网络
ansible当中模块的使用
剑指 Offer II 091. 粉刷房子
学神经网络需要什么基础,神经网络需要什么基础
图论与网络模型——基于R