Page 1 of 1

Current value in a sequence.

Posted: Thu Jan 15, 2009 11:09 am
by swraman
working in pgsql with PHP,

I have a table "tr_projects", it has a column "id", which are intigers.

What query/code do I have to run to get the last intiger from the column ID?

Can it be done with curval()? If so, what arguement do you use to retrieve it with curval?

Thanks

Re: Current value in a sequence.

Posted: Thu Jan 15, 2009 11:14 am
by VladSun
[sql]SELECT curval('sequence')[/sql]
is enough

Re: Current value in a sequence.

Posted: Thu Jan 15, 2009 11:20 am
by swraman
What is the arguement sequence?

I have the table name and the row name, but what is the sequence?

Thanks

Re: Current value in a sequence.

Posted: Thu Jan 15, 2009 11:44 am
by VladSun
You should set your id column is set by using nextval('sequence').
You have to create the sequence first (read the manuals).
Sequences are used for the same as auto_increment flag in MySQL.

Re: Current value in a sequence.

Posted: Thu Jan 15, 2009 1:59 pm
by swraman
Can you link me to the documentation? Im having trouble finding documentation for a lot of these pgsql functions.
The code I had that I believe is faulty is:

public function GetInsertID($table, $column) {
$result = $this->Query("SELECT curval('$table_$column_seq')");
return $data[0];
}
I was given this code, I didnt write it. I think it is wrong because 1) it errors, and 2) there is no variable data in the function. Are my assumptions correct?

Re: Current value in a sequence.

Posted: Thu Jan 15, 2009 2:29 pm
by VladSun