如果那么函数更新表?

时间:2017-12-12 09:51:57

标签: postgresql

table:
name  date       object  
james 25/04/2017 bycicle

insert into statisticstable 
select * from table;

我想编写一个只在名称和日期不在统计表中时才更新表统计信息的函数。 如果在统计表中有名称和日期没有更新表统计信息, 否则,如果在统计表中没有名称和日期更新表统计信息。

这样的事情:

CREATE FUNCTION update_table()
DECLARE
name = &name
date = &date

if &name and &date are in statisticstable
then null;    
if &name and &date aren't in statisticstable
insert into statisticstable 
select * from table;

对不起,但我不是职能专家。

1 个答案:

答案 0 :(得分:1)

探索此

CREATE OR REPLACE FUNCTION update_table(name character varying, dte character varying)
RETURNS void  AS
  $BODY$
    BEGIN
    EXECUTE 'INSERT INTO statisticstable (name , date)
        (SELECT '''||name||''' , '''||dte||'''
            WHERE NOT EXISTS(
            SELECT 1 FROM statisticstable 
            WHERE name = '''||name||'''
            AND date= '''||dte||''')
        )';
    END;
  $BODY$
LANGUAGE plpgsql