Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在一个函数内循环变量的动态SQL之前截断表_Sql_Postgresql_Plpgsql_Dynamic Sql_Postgresql 8.2 - Fatal编程技术网

在一个函数内循环变量的动态SQL之前截断表

在一个函数内循环变量的动态SQL之前截断表,sql,postgresql,plpgsql,dynamic-sql,postgresql-8.2,Sql,Postgresql,Plpgsql,Dynamic Sql,Postgresql 8.2,我有一个函数,可以循环特定的模式名称,并将数据插入表中。我希望能够在insert循环发生之前截断该表。我尝试将truncate语句放在动态查询中,这导致它只将模式的数据保留在表中。我还尝试将其声明为自己的变量,然后与循环语句分开执行该语句,但结果是相同的 所以我的问题是-我应该把truncate table dwh.prod_table_notify语句放在这个函数中的什么位置?因此,每次运行此函数时,表都会被截断,然后insert将正确地循环遍历从FOR语句返回的每个模式 注意:我被迫使用po

我有一个函数,可以循环特定的模式名称,并将数据插入表中。我希望能够在insert循环发生之前截断该表。我尝试将truncate语句放在动态查询中,这导致它只将模式的数据保留在表中。我还尝试将其声明为自己的变量,然后与循环语句分开执行该语句,但结果是相同的

所以我的问题是-我应该把truncate table dwh.prod_table_notify语句放在这个函数中的什么位置?因此,每次运行此函数时,表都会被截断,然后insert将正确地循环遍历从FOR语句返回的每个模式

注意:我被迫使用postgres 8.2

CREATE OR REPLACE FUNCTION dwh.dim_table_notification()
 RETURNS void
 LANGUAGE plpgsql
AS $function$
Declare
       myschema varchar;
       sql2 text;                   
Begin 
for myschema in 
select distinct table_schema
from information_schema.tables
where table_name in ('dim_loan_type', 'dim_acct_type')
and table_schema NOT LIKE 'pg_%'
and table_schema NOT IN ('information_schema', 'ad_delivery', 'dwh', 'users', 'wand', 'ttd') 
order by table_schema
loop  
sql2 ='insert into dwh.prod_table_notify 
select '''|| myschema ||''' as userid, loan_type_id as acct_type_id, loan_type::varchar(10) as acct_type, loan_type_desc::varchar(50) as acct_type_desc, term_code, 1 as loan_type from '|| myschema || '.' ||'dim_loan_type where term_code is null
union
select '''|| myschema ||''' as userid, acct_type_id, acct_type::varchar(10), acct_type_desc::varchar(50), term_code, 0 as loan_type from '|| myschema || '.' ||'dim_acct_type where term_code is null';
execute sql2;
end loop;
END;
$function$
你确定你可以使用TRUNCATE吗?引述:

TRUNCATE不能用于具有来自其他表的外键引用的表,除非所有此类表也在同一命令中被截断

如果表很小,则首先删除比截断要快: 从dwh.prod_表中删除通知

你必须清理标识符!使用,也可在第8.2页中找到

在这里使用DISTINCT没有意义

为插入提供列定义列表。否则,当您稍后更改表格时,它可能会以混乱的方式中断

如果SELECT的两个分支中的行是唯一的,请使用UNION ALL而不是UNION。尝试折叠副本没有意义


我想我遗漏了什么:为什么不把截断放在循环之前呢?@Dmitri我无法让它工作。我应该把它放在哪里?第一句话:在开始之后,在开始之前。现在我看了一下,这里有两次函数声明-这只是一个复制/粘贴错误吗?很好,它可以工作。是的,表很小,所以我用deletefrom代替truncate。你是个了不起的人,欧文!
CREATE OR REPLACE FUNCTION dwh.dim_table_notification()
  RETURNS void LANGUAGE plpgsql AS
$func$
DECLARE
   myschema text;
BEGIN

-- truncate simply goes here:
TRUNCATE dwh.prod_table_notify;

FOR myschema IN
   SELECT quote_ident(table_schema)
   FROM   information_schema.tables
   WHERE  table_name IN ('dim_loan_type', 'dim_acct_type')
   AND    table_schema NOT LIKE 'pg_%'
   AND    table_schema NOT IN
          ('information_schema', 'ad_delivery', 'dwh', 'users', 'wand', 'ttd')
   ORDER  BY table_schema
LOOP
   EXECUTE '
   INSERT INTO dwh.prod_table_notify
              (userid, acct_type_id, acct_type, acct_type_desc, loan_type)
   SELECT '''|| myschema ||''', loan_type_id, loan_type::varchar(10)
        , loan_type_desc::varchar(50), term_code, 1 AS loan_type
   FROM   '|| myschema || '.dim_loan_type
   WHERE  term_code IS NULL
   UNION ALL
   SELECT '''|| myschema ||''' AS userid, acct_type_id, acct_type::varchar(10)
       , acct_type_desc::varchar(50), term_code, 0 AS loan_type
   FROM   '|| myschema || '.dim_acct_type
   WHERE term_code IS NULL';
END LOOP;
END
$func$