From: | Kostas Maistrelis <maisk(at)ath(dot)forthnet(dot)gr> |
---|---|
To: | blackwater dev <blackwaterdev(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: mysql replace in postgreSQL? |
Date: | 2005-10-29 08:21:08 |
Message-ID: | 436330F4.3000909@ath.forthnet.gr |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
blackwater dev wrote:
>In MySQL, I can use the replace statement which either updates the
>data there or inserts it. Is there a comporable syntax to use in
>postgreSQL?
>
>I need to do an insert and don't want to have to worry about if the
>data is already there or not...so don't want to see if it there, if so
>do update if not insert...etc.
>
>
look this functions..
is not general solution..
CREATE TYPE mydata AS (
f1 integer ,
f2 integer,
);
CREATE OR REPLACE FUNCTION updatefoo(data mydata, myid bigint) RETURNS
boolean AS $$
DECLARE
BEGIN
update foo_table set
f1 = mydata.f1,
f2 = mydata.f2
WHERE id = myid;
IF NOT FOUND THEN
return false;
END IF;
return true;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION insertfoo(data mydata, myid bigint) RETURNS
boolean AS $$
DECLARE
rep boolean DEFAULT false;
BEGIN
insert into foo_table (
id ,
f1,
f2
) values (
mydata.id,
mydata.f1,
mydata.f2
);
return rep;
END
$$ LANGUAGE plpgsql;
CREATE OR REPLACE FUNCTION replaceFoo(data mydata, myid bigint) RETURNS
boolean AS $$
DECLARE
rep boolean = false;
BEGIN
rep = updatefoo(mydata,myid );
if not rep then
rep = insertfoo(mydata,myid );
end if;
return rep;
END
$$ LANGUAGE plpgsql;
From | Date | Subject | |
---|---|---|---|
Next Message | Zet | 2005-10-29 09:11:46 | which charset use for cyrilic? |
Previous Message | Bricklen Anderson | 2005-10-29 03:12:24 | Re: mysql replace in postgreSQL? |