我已阅读过文档和几篇SO帖子,但仍然感觉不清楚如何在Postgres中声明和分配变量......这就是我最初尝试做的事情:
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
*/
这很简单,但它不起作用......我是Postgres语法的新手,我希望得到一些帮助来解决这个问题,因为我想明确声明数据类型如果可能,在将数据设置为变量之前。
你们是否知道在设置变量时明确声明数据类型我缺少什么?
有些人指出必须在函数中使用变量。我不相信Postgres的情况完全如此(我知道它与MySQL有关,而不是T-SQL)但到了这一点,这里是上面的代码,在函数的上下文中。我将在短期内测试一个答案中指出的内容,并据此接受。所以这是目前为止的完整代码:
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
我使用了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;
-- 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
);
答案 0 :(得分:6)
您只能在函数(存储过程)中声明变量。尝试这样的事情:
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;