我制作了名为t_user_type的复合类型:
CREATE TYPE t_user_type AS
(uid integer,
firstname character varying,
lastname character varying,
companyname character varying,
email character varying,
sip_phone integer);
...我需要将字符串转换为该类型,所以我做
SELECT '(11423, FirstName, LastName, Company, email@gmail.com, 204)' :: t_user_type;
每一件事都很好。没有错误,没有。但是,当我使用过程执行相同的操作时,我收到此错误:整数的输入语法无效:“(11423,”FirstName“,”LastName“,”Company“,”email@gmail.com“,204) “
这是我的程序:
CREATE OR REPLACE FUNCTION change_type(p_user character varying)
RETURNS void AS
$BODY$DECLARE
v_user_type t_user_type;
BEGIN
SELECT p_user :: t_user_type INTO v_user_type;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION change_type(character varying)
OWNER TO postgres;
这是使用过程的查询:
SELECT change_type(
'(11423, FirstName, LastName, Company, email@gmail.com, 204)');
有谁可以告诉我,我做错了什么?
答案 0 :(得分:1)
不确定为什么失败我做了一些小改动,我只是将值转换为你的局部变量,它似乎工作。但我不知道为什么。 SELECT INTO变量是否合法(我认为我可以将常规文本字符串转换为文本变量)?我通常会像下面的例子那样做它只是一个没有源表的变量。
CREATE TYPE t_user_type AS
(uid integer,
firstname character varying,
lastname character varying,
companyname character varying,
email character varying,
sip_phone integer);
SELECT '(11423, FirstName, LastName, Company, email@gmail.com, 204)' :: t_user_type;
CREATE OR REPLACE FUNCTION change_type(p_user character varying)
RETURNS t_user_type AS
$BODY$DECLARE
v_user_type t_user_type;
BEGIN
v_user_type := CAST(p_user AS t_user_type);
return v_user_type;
END;$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION change_type(character varying)
OWNER TO postgres;
SELECT change_type(
'(11423, FirstName, LastName, Company, email@gmail.com, 204)');