2016-02-13 49 views
1

我想创建检测脚本,并滴在我的posgresql数据库中的公共表...创建脚本,删除几个表(PostgreSQL的)

我建立请求如下:

SELECT CONCAT('DROP TABLE ', table_schema,'.',table_name,';') AS stmt FROM information_schema.TABLES 
WHERE table_schema='public' AND table_catalog='capsana' 

这里是我

enter image description here

我现在想以自动方式执行该命令(在语句列)的输出的屏幕截图..而不d oing复制粘贴!

有没有办法做到这一点?

回答

1

你可以使用动态sql来做到这一点;

DO $$ 
DECLARE 
    drop_stmt text; 
BEGIN 
    FOR drop_stmt IN 
    SELECT CONCAT('DROP TABLE ', table_schema,'.',table_name) AS stmt 
    FROM information_schema.TABLES 
    WHERE table_schema='public' AND table_catalog='capsana' LOOP 
     EXECUTE drop_stmt; 
    END LOOP; 
END$$; 
+0

当我执行这个命令,它说: 错误:循环超过行循环变量必须是一个记录或者行变量或标量变量 8号线的名单:因我 ^ *** ******* Erreur ********** – taboubim

+0

我使用PostgreSQL的pgAdmin ... – taboubim

+0

更新了我的答案。斯蒂尔得到错误? – Praveen