错误:查询的结构与函数结果类型不匹配

hc2pp10m  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(342)

我正在尝试将mysql存储过程转换为postgrsql存储函数。我对postgresql不熟悉。我第一次尝试将连接存储过程转换为存储函数。但我有以下错误

ERROR:  structure of query does not match function result type
DETAIL:  Returned type text does not match expected type character varying in column 3.
CONTEXT:  PL/pgSQL function getsummarypagecontentsurveyform(numeric) line 3 at RETURN QUERY

存储过程

CREATE PROCEDURE [dbo].[GetSummaryPageContentSurveyForm]     
    @nRoomAllocationID bigint
AS
BEGIN
   SELECT  Employee.sEmpName, RoomInvestigatorMapping.sComment,
           Employee.sEmpName + ' : ' + RoomInvestigatorMapping.sComment as CommentsToDisplay
   FROM    RoomInvestigatorMapping INNER JOIN Employee 
        ON RoomInvestigatorMapping.nInvestigatorID = Employee.nEmpID
   Where    RoomInvestigatorMapping.nRoomAllocationID = @nRoomAllocationID 
END   
GO

存储功能

CREATE OR REPLACE FUNCTION GetSummaryPageContentSurveyForm (        
    p_nroom_allocation_id numeric)

    RETURNS Table(res_semp_name character varying,res_scomment character varying,
                  res_comments_to_display character varying)
AS $$
BEGIN
 Return Query    
   SELECT  employee.semp_name, roominvestigatormapping.scomment,
           employee.semp_name || ' : ' || roominvestigatormapping.scomment as comments_to_display
   FROM    roominvestigatormapping INNER JOIN employee 
           ON roominvestigatormapping.ninvestigator_id = employee.nemp_id
   Where   roominvestigatormapping.nroom_allocation_id = p_nroom_allocation_id; 
END;

$$ LANGUAGE plpgsql;
mkshixfv

mkshixfv1#

postgresql函数必须定义结果类型。运行时检查输出是否与定义的类型相同。在您的例子中,第三个表达式返回文本而不是varchar。您需要将此表达式显式转换为varchar:

$$
BEGIN
  RETURN QUERY    
    SELECT  employee.semp_name, roominvestigatormapping.scomment,
         (employee.semp_name || ' : ' || roominvestigatormapping.scomment)::varchar as comments_to_display
      FROM    roominvestigatormapping INNER JOIN employee 
        ON roominvestigatormapping.ninvestigator_id = employee.nemp_id
     WHERE   roominvestigatormapping.nroom_allocation_id = p_nroom_allocation_id; 
END;
$$

我用过演员 :: :

somevalue::varchar

相关问题