截断 postgres 数据库中的所有表
2010 年 5 月,Aaron 和 Henning 都提供了注册一个函数的代码,当稍后使用“用户名”参数调用该函数时,该函数将截断所有表。它在 Windows 7 上的 postgres 上运行良好。不幸的是,在 Ubuntu 上的 postgres 8.3 上两者都不起作用。
An error has occurred:
ERROR: syntax error at or near "$1"
LINE 1: $1
^
QUERY: $1
CONTEXT: SQL statement in PL/PgSQL function "truncate_tables" near line 6
我还尝试简化 select 语句以专注于 BEGIN For 子句, 删除我在 Windows 中使用的复杂 WHERE 子句。
你能看到这里的问题吗?谢谢。 检索表名后是否无法传递或读取? $1 的问题不是意味着它找不到输入吗?
DECLARE
stmt RECORD;
statements CURSOR FOR
SELECT tablename FROM pg_tables
WHERE tablename !~* 'sql_*' and tablename !~* 'pg_*' and tablename !~* 'schema_*';
BEGIN
FOR stmt IN statements LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
END;
In May 2010, Aaron and Henning both provided the code to register a function that when called later with a parameter for 'username' would truncate all the tables. It worked fine with postgres on Windows 7. Neither will work unfortunately for postgres 8.3 on Ubuntu.
An error has occurred:
ERROR: syntax error at or near "$1"
LINE 1: $1
^
QUERY: $1
CONTEXT: SQL statement in PL/PgSQL function "truncate_tables" near line 6
I have also tried simplifying the select statement to focus on the BEGIN For clause,
by removing the complicated WHERE clause I used in Windows.
Can you see the problem here? Thanks.
Is it unable to pass or read the tablenames after they are retrieved? Doesn't a problem with $1 mean it can't find its input?
DECLARE
stmt RECORD;
statements CURSOR FOR
SELECT tablename FROM pg_tables
WHERE tablename !~* 'sql_*' and tablename !~* 'pg_*' and tablename !~* 'schema_*';
BEGIN
FOR stmt IN statements LOOP
EXECUTE 'TRUNCATE TABLE ' || quote_ident(stmt.tablename) || ' CASCADE;';
END LOOP;
END;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
版本 8.3 和 8.4 之间似乎某些函数语法发生了变化。试试这个:
It appears that some of the function syntax changed between version 8.3 and 8.4. Try this: