是否可以在PostgreSQL中创建一个带有变量名的表? [英] Is it possible to create a table with a variable name in PostgreSQL?
问题描述
使用PL/pgSQL或(某种其他机制),是否可以创建具有变量名的表?
Using PL/pgSQL or (some other mechanism), is it possible to create a table with a variable name?
我想创建多个名为table_1,table_2,table_3等的表,如果可以使用循环来创建它们,而不是显式地创建每个表,则会更简单.
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.
我怀疑答案是否定的,但我想确认一下.
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.
实际上应该有可能.只需以编程方式将表名构建为字符串,然后循环执行CREATE语句作为动态查询即可.
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.
See this page in the postgres documentation: http://www.postgresql.org/docs/current/interactive/plpgsql-statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN
这篇关于是否可以在PostgreSQL中创建一个带有变量名的表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!