简体   繁体   English

是否可以在PostgreSQL中创建一个带有变量名的表?

[英]Is it possible to create a table with a variable name in PostgreSQL?

Using PL/pgSQL or (some other mechanism), is it possible to create a table with a variable name? 使用PL / pgSQL或(其他一些机制),是否可以创建一个带有变量名的表?

I would like to create multiple tables named table_1, table_2, table_3, etc... and it would be simpler if I could use a loop to create them, instead of explicitly creating each one. 我想创建多个名为table_1,table_2,table_3等的表...如果我可以使用循环来创建它们,而不是显式创建每个表,那将更简单。

I suspect the answer to this is no, but I would like to confirm it. 我怀疑答案是否定的,但我想确认一下。

While I would question your design if you're relying on such tricks, your question is general and you didn't give specific information to judge either way. 虽然我会质疑你的设计,如果你依赖这些技巧,你的问题是一般的,你没有提供具体的信息来判断任何一种方式。

It should be possible, actually. 实际上应该是可能的。 It's only a matter of programmatically building the table names as strings and then, in a loop, execute your CREATE statements as dynamic queries. 这只是以编程方式将表名构建为字符串,然后在循环中将CREATE语句作为动态查询执行。

See this page in the postgres documentation: http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN 在postgres文档中查看此页面: http//www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

通过在循环中动态构建命令来生成脚本,然后运行生成的输出脚本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM