I have read over the documentation and several SO posts, but it still feels unclear how variables are declared and assigned in Postgres... This is what I originally tried to do:
SET SEARCH_PATH = PsychoProductions;
declare var_person_id INT;
select var_person_id = (
select id
from person
where
first_name = prm_first_name AND
last_name = prm_last_name AND
organization = prm_organization
);
/*
Result:
ERROR: syntax error at or near "INT"
LINE 2: DECLARE var_person_id INT;
^
********** Error **********
ERROR: syntax error at or near "INT"
SQL state: 42601
Character: 70
*/
It is simple but it doesn't work... I'm kind of new at Postgres syntax, and I was hoping to get some help figuring this out, as I would like to explicitly declare data types, if possible, before setting data to variables.
Do any of you know what the heck I am missing to explicitly declare data types while setting a variable?
Some have pointed out that variables have to be used within a function. I don't believe that's completely the case with Postgres (I know it is with MySQL, and is not with T-SQL) but to the point, here is the above code, in context within a function. I will test what has been pointed out in one answer, shortly, and accept accordingly. So here is the full code so far:
set search_path = PsychoProductions;
create or replace function fcn_Insert_person (
-- "person" table
prm_role_id text, -- table default 'Customer'
prm_first_name text,
prm_last_name text,
prm_organization text,
prm_website text,
prm_default_Billing_Method_ID text, -- table default 'Net 30'
prm_active BOOLEAN,
-- "address" table
prm_address_type_id text, -- table default 'Unique'
prm_address text,
prm_city text,
prm_state text,
prm_zip_code text,
-- "email" table
prm_email_address text,
prm_email_type_id text, -- table default 'Business'
-- "phone" table
prm_phone_number text,
prm_phone_type_id text -- table default 'Main'
)
returns setof Person
as
$delimiter$
begin
set search_patch = PsychoProductions;
start transaction;
insert into person (
role_id,
first_name,
last_name,
organization,
website,
default_billing_method_id,
active
)
values (
prm_role_id,
prm_first_name,
prm_last_name,
prm_organization,
prm_website,
prm_default_Billing_Method_ID,
prm_active
);
commit;
start transaction;
declare var_person_id int;
select var_person_id = (
select id
from person
where
first_name = prm_first_name AND
last_name = prm_last_name AND
organization = prm_organization
);
-- and this is where I got stuck
I used the select into myVar ...
syntax but now I'm getting a completely different problem... Should I ask a new question?
Error:
ERROR: column "var_person_id" does not exist
LINE 81: var_person_id,
^
********** Error **********
ERROR: column "var_person_id" does not exist
SQL state: 42703
Character: 2220
Code:
start transaction;
-- declare var_person_id int;
select id into var_person_id
from person
where
first_name = prm_first_name AND
last_name = prm_last_name AND
organization = prm_organization;
insert into address (
person_id,
address_type_id,
address,
city,
state,
zip_code
)
values (
var_person_id, -- error here
prm_address_type_id,
prm_address,
prm_city,
prm_state,
per_zip_code
);
You can only declare variables inside a function (stored procedure). Try something like this:
CREATE FUNCTION my_func(prm_first_name text,prm_last_name text)
RETURNS SET OF int $$
DECLARE
var_person_id int;
BEGIN
SELECT id INTO var_person_id
FROM person,var_parameter
WHERE first_name = prm_first_name
AND last_name = prm_last_name
AND organization = prm_organization;
-- OR
var_person_id := (SELECT id FROM ...);
-- use it later like this:
RETURN QUERY SELECT ... FROM ... WHERE id=var_person_id;
END;
$$ LANGUAGE PLPGSQL;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With