I'm using postgresql and I want to insert or update records with a function in my program. But the thing I have to know is, if a record with conditions I look for is already in database, I will update it, else I will insert a new record. With details:
Table :
CREATE TABLE running_check
(
"UID" character varying(100) NOT NULL,
"CameraIP" character varying(100),
"ErrorStatus" integer,
"IsRunning" boolean,
"CheckTime" timestamp without time zone
);
Some example records:
UID CameraIP ErrorStatus IsRunnning CheckTime
------------------------------------------------------------------
12E 10.0.0.26 0 true now()
C26 10.0.0.22 0 true now()
454 10.0.0.13 3 false now()
I need a function like:
InsertRunningCheckInfo(character varying, character varying, integer, boolean )
And when I call the function, firstly I need to check the records in table if a record with the same UID already exist, then if its "IsRunning" value is true, just update the "CheckTime", else update its ErrorStatus, IsRunning and CheckTime values, if a record with the same UID doesn't exist, insert a new record.
Actually, the problem I face is about not knowing how to use a Select query in a function to check its fields then do work, because I'm too new to Postgresql, searched for it for a while but couldn't find something useful for me. Maybe another way is available for this task in Postgresql that I don't know, so wanted to ask you.
Thanks in advance.