【问题标题】:truncate script tables in schema does not work截断模式中的脚本表不起作用
【发布时间】:2017-12-16 12:16:05
【问题描述】:

我正在寻找解决方案。我正在尝试截断我的 postgres 数据库中的所有表:

我正在使用一个简单的 SQL 脚本

SELECT 'TRUNCATE ' || table_name || ';'
FROM information_schema.tables WHERE table_schema='sda' AND table_type='BASE TABLE';

不幸的是它不起作用,因为许多关系不存在。

请帮忙。 (我使用的是 postgresql 9.2)

【问题讨论】:

  • 您可以尝试使用SELECT tablename FROM pg_catalog.pg_tables where shemaname = 'sda',但这或多或少与您所做的相同。您能否提供更多信息 - 例如示例输出,或者 information_schema 中存在哪些表而不存在于数据库中?
  • 你会检查this question

标签: sql postgresql truncate


【解决方案1】:

准备:

t=# create schema sda;
CREATE SCHEMA
t=# create table sda."BASE TABLE"();
CREATE TABLE

尝试:

t=# SELECT format('TRUNCATE %I;',table_name)
    FROM information_schema.tables WHERE table_schema='sda' AND table_type = 'BASE TABLE';
         format
------------------------
 TRUNCATE "BASE TABLE";
(1 row)
t=# TRUNCATE "BASE TABLE";
TRUNCATE TABLE

所以我假设您只是没有将表视为标识符,例如:

t=# TRUNCATE BASE TABLE;
ERROR:  syntax error at or near "TABLE"
LINE 1: TRUNCATE BASE TABLE;

还有 - 名称中带有空格的大写字母通常会导致人为错误,最好使用标准的无大小写名称...

【讨论】:

  • 我修好了。因为我没有以对给定表负责的用户身份登录而找不到的关系
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
相关资源
最近更新 更多