In Postgres, I'm writing a test script for a function (stored procedure) I wrote. The function returns and integer, the id of the row inserted. In the test script I want to select that id into a variable and show everything has been inserted correctly.
The function looks like this:
CREATE FUNCTION create_factor(p_name VARCHAR(255))
RETURNS integer AS
$$
DECLARE v_insert_id INTEGER;
BEGIN
....
RETURN v_insert_id AS id;
END;
$$ LANGUAGE plpgsql;
The psql script looks like this:
BEGIN;
\i create_factor.sql
DO $$
declare factorId integer;
select create_factor into factorId from /* have tried selecting * as well */
create_factor(
'my factor'::VARCHAR(255)
);
\x
select * from factors where name='my factor' and id=factorId;
\x
select k.key_name, v.value
from factors f
join factor_type_key_store k on k.factor_type_id = f.factor_type_id
join factor_key_value_store v on v.factor_type_key_store_id=k.id ;
END$$;
ROLLBACK;
The error I get is as follows:
psql:create_factor_test.sql:31: ERROR: invalid type name "create_factor.id into factorId from
clearly referring to the select into
statement. I've switched up both the function definition and the script quite a bit. Not sure what I'm missing.
create_factor()
actually inserts a row. You should have said so explicitly. Also, you may want to start another question asking how to optimize that. It looks like what you have is not the optimum. And remember to mention your version of Postgres. – Erwin Brandstetter 17 hours ago