William Scott Jordan said:
> ns_db dml $db "INSERT INTO test (test_column) SELECT
> COALESCE(MAX(test_column),0) + 1 FROM test"
Ehrm, that is a very, very bad way of creating keys. It is no unlikely
that on a very loaded site two people might insert at the same time and
get the same key!
The correct way of doing this is using a sequence:
create sequence test_id;
You can either first select an ID from this and use it manually in your
query:
select nextval('test_id') as test_id;
Or use it in a default:
create table test (
test_id primary key default nextval('test_id')
);
If using that method, you can use "curval('test_id')" to get the last
assigned id and use it for further processing after you do the insert.
curval gives back the last sequence handed out in the current session, so
it is perfectly safe to use as long as you stick with the same database
handle.
Cheers,
Bas.
--
AOLserver - http://www.aolserver.com/
To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]>
with the
body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject:
field of your email blank.