I am trying to create a function to populate 2 tables, the second query containing the new id from the first insert.
an example of some of my tables:
CREATE TABLE message
(
message_id bigserial NOT NULL,
member_id bigint NOT NULL,
message character varying(8192) NOT NULL,
...
)
CREATE TABLE feed_message
(
feed_id bigint NOT NULL,
message_id bigint NOT NULL
)
what i am trying to do is insert a new message in the message table, and use the generated message_id to populate the feed_message table
i tried writing a function using the pltcl language but i cant figure out how to use the SPI_getvalue to get the just created tupil
this is what i have so far:
/* message_post(entity, id, member_id, title, message, reactionTo) */
CREATE OR REPLACE FUNCTION message_post ()
RETURNS VOID
LANGUAGE pltcl
AS $BODY$
ret_status = spi_exec -count 1 "INSERT INTO message (member_id, title, message) VALUES ($3, $4, $5)"
IF (ret_status == SPI_OK_SELECT && SPI_processed > 0) {
//get the tupil from *SPI_tuptable
set message_id <the new message_id>
}
spi_exec -count 1 "INSERT INTO $1_message ($1_id, message_id) VALUES ($2,$message_id)"
$BODY$;
/* useage */
SELECT message_post('feed',12,1,'title','message');