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

insert into statisticstable 
select * from table;

I want to write a function that update the table statistics only if name and date are not already in statistics table. If in statistics table there are name and date not update the table statistics, otherwise if in statistics table there aren't name and date update the table statistics.

Something like this:

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;

Excuse me but I am not an expert on functions.

James
  • 97
  • 1
  • 1
  • 8
  • Refre this - https://stackoverflow.com/questions/1009584/how-to-emulate-insert-ignore-and-on-duplicate-key-update-sql-merge-with-po – Ankit Bajpai Dec 12 '17 at 09:56

1 Answers1

1

Explore this

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
lat long
  • 850
  • 6
  • 15