2
我需要遍历postgresql中数据库中的所有表。 是否有类似于mssql sp_msforeachtable for postgresql的存储过程?通过postgresql存储过程中的所有表进行处理
我需要遍历postgresql中数据库中的所有表。 是否有类似于mssql sp_msforeachtable for postgresql的存储过程?通过postgresql存储过程中的所有表进行处理
我通常使用此查询对information_schema.tables
:
SELECT table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema = 'public'
ORDER BY table_name
您可能要调整table_schema
满足您的需求,但。该查询应该(AFAIK)在遵循标准的任何数据库中工作。
查看信息模式:。
如何循环遍历所有表,然后处理表,如果我在information_schema.tables中查找,我仍然需要使用脚本来循环返回的结果并执行/处理每个表。在ms sql中,您可以在命令行(sqlcmd)中执行类似操作:sp_msforeachtable'delete from?'该命令将从数据库 – technomage
@technomage中的所有表中删除列,您需要了解如何使用UNIX。 'psql -At -c'SELECT table_name FROM information_schema.tables'| xargs -n 1 ....' – Sean