I read the documentation and some SO posts, but it is still 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's simple, but it won’t work ... I'm kind of new to Postgres syntax, and I was hoping to get some help with this, as I would like to explicitly declare data types, if possible, before setting the data to variables.
Do any of you know what I am missing to explicitly declare data types when setting a variable?
EDIT 1: 09-05-2013
, . , Postgres ( , MySQL, T-SQL), , , . , , , . , :
set search_path = PsychoProductions;
create or replace function fcn_Insert_person (
prm_role_id text,
prm_first_name text,
prm_last_name text,
prm_organization text,
prm_website text,
prm_default_Billing_Method_ID text,
prm_active BOOLEAN,
prm_address_type_id text,
prm_address text,
prm_city text,
prm_state text,
prm_zip_code text,
prm_email_address text,
prm_email_type_id text,
prm_phone_number text,
prm_phone_type_id text
)
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
);
2: 09-06-2013
select into myVar ..., ... ?
:
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
:
start transaction;
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,
prm_address_type_id,
prm_address,
prm_city,
prm_state,
per_zip_code
);