Sql Postgres在创建函数时出错

Sql Postgres在创建函数时出错,sql,postgresql,user-defined-functions,Sql,Postgresql,User Defined Functions,我试图在Postgres中创建一个函数,如下所示: Create function Samplefunc() returns resultdata( Tested int, Score int, Growth int) as $BODY$ Select Count(distinct student_id) Tested, Cast(Avg(R.raw_score) as Int) Score, Avg(R.growth) as Gr

我试图在Postgres中创建一个函数,如下所示:

Create function Samplefunc() 
returns resultdata( Tested int, Score int,
       Growth int) as
$BODY$
Select 
       Count(distinct student_id) Tested, 
       Cast(Avg(R.raw_score) as Int)  Score,
       Avg(R.growth) as Growth
from results R
where R.id=1 and test_id='ME04';
$BODY$
LANGUAGE sql;
但我得到了以下错误:

ERROR:  syntax error at or near "int"
LINE 2: returns resultdata( NTested int, RawScore int,
                                    ^

********** Error **********

ERROR: syntax error at or near "int"
SQL state: 42601
Character: 59

哪里出错了?

请尝试不指定复合返回类型的详细信息。然而,我认为回报结构必须首先存在

Create table resultdata (NTested int, RawScore int, Growth int);

Create function Samplefunc() returns resultdata as
$BODY$
Select 
   Count(distinct student_id) as NTested, 
   Cast(Avg(R.raw_score) as Int) as RawScore,
   Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;
或者尝试显式返回表:

Create function Samplefunc() 
    returns Table (NTested int, RawScore int, Growth int) as
$BODY$
Select 
   Count(distinct student_id) as NTested, 
   Cast(Avg(R.raw_score) as Int) as RawScore,
   Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;
我认为您还可以使用输出参数返回一组记录:

Create function Samplefunc(OUT NTested int, OUT RawScore int, OUT Growth int) 
    returns SetOf Record as
$BODY$
Select 
   Count(distinct student_id) as NTested, 
   Cast(Avg(R.raw_score) as Int) as RawScore,
   Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;

尝试不指定复合返回类型的详细信息。然而,我认为回报结构必须首先存在

Create table resultdata (NTested int, RawScore int, Growth int);

Create function Samplefunc() returns resultdata as
$BODY$
Select 
   Count(distinct student_id) as NTested, 
   Cast(Avg(R.raw_score) as Int) as RawScore,
   Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;
或者尝试显式返回表:

Create function Samplefunc() 
    returns Table (NTested int, RawScore int, Growth int) as
$BODY$
Select 
   Count(distinct student_id) as NTested, 
   Cast(Avg(R.raw_score) as Int) as RawScore,
   Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;
我认为您还可以使用输出参数返回一组记录:

Create function Samplefunc(OUT NTested int, OUT RawScore int, OUT Growth int) 
    returns SetOf Record as
$BODY$
Select 
   Count(distinct student_id) as NTested, 
   Cast(Avg(R.raw_score) as Int) as RawScore,
   Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;
返回表的子句为

小心避免OUT参数和列名之间的冲突。我的初稿中有这样的冲突。表限定要消除歧义的列。 RETURNS表中的所有字段名实际上都是OUT参数,在函数中几乎处处可见

此外:

avggrowth将导致类型与声明的返回类型int不匹配。您也需要强制转换该类型。使用简短的,顺便说一句。 更好的方法是:返回以保留平均值中的小数位数

列别名仅在函数内部可见。如果您不打算在函数中引用它们,那么它们只是文档

资本化是怎么回事

如果保证查询返回一行,则可能需要将OUT参数与RETURNS记录组合在一起:

微妙的区别是:这样,如果没有找到任何内容,您将得到一个具有空值的单行,其中第一个表单将返回nothing/no row

注释中的外接程序参数请求 这里有许多相关的答案,还有更多的代码示例。比如:

返回表的子句为

小心避免OUT参数和列名之间的冲突。我的初稿中有这样的冲突。表限定要消除歧义的列。 RETURNS表中的所有字段名实际上都是OUT参数,在函数中几乎处处可见

此外:

avggrowth将导致类型与声明的返回类型int不匹配。您也需要强制转换该类型。使用简短的,顺便说一句。 更好的方法是:返回以保留平均值中的小数位数

列别名仅在函数内部可见。如果您不打算在函数中引用它们,那么它们只是文档

资本化是怎么回事

如果保证查询返回一行,则可能需要将OUT参数与RETURNS记录组合在一起:

微妙的区别是:这样,如果没有找到任何内容,您将得到一个具有空值的单行,其中第一个表单将返回nothing/no row

注释中的外接程序参数请求 这里有许多相关的答案,还有更多的代码示例。比如:


如果您没有像假设的那样定义类型resultset,那么请尝试返回一个表

Create function Samplefunc() 
returns table( NTested int, RawScore int,
       Growth int) as
$BODY$
Select 
       Count(distinct student_id) NTested, 
       Cast(Avg(R.raw_score) as Int)  RawScore,
       Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;

如果您没有像假设的那样定义类型resultset,那么请尝试返回一个表

Create function Samplefunc() 
returns table( NTested int, RawScore int,
       Growth int) as
$BODY$
Select 
       Count(distinct student_id) NTested, 
       Cast(Avg(R.raw_score) as Int)  RawScore,
       Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;

您正在尝试返回包含多个输出参数的记录。你应该喜欢:

Create function Samplefunc(out NTested int, out RawScore int, out Growth int) as
$BODY$
Select 
       Count(distinct student_id) NTested, 
       Cast(Avg(R.raw_score) as Int)  RawScore,
       Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;
您可以使用显式命名的复合类型更详细地执行相同的操作;像

CREATE TYPE resultdata AS (NTested int, RawScore int, Growth int);

CREATE FUNCTION Samplefunc() RETURNS resultdata
    AS .......
或者使用表函数,如

CREATE FUNCTION Samplefunc() RETURNS TABLE(NTested int, RawScore int, Growth int)
AS
......

有关尝试返回包含多个输出参数的记录的同一记录的更多信息,请参见。你应该喜欢:

Create function Samplefunc(out NTested int, out RawScore int, out Growth int) as
$BODY$
Select 
       Count(distinct student_id) NTested, 
       Cast(Avg(R.raw_score) as Int)  RawScore,
       Avg(R.growth) as Growth
from reports_results R
where R.test_type_id=1 and test_id='201403MAME04';
$BODY$
LANGUAGE sql;
您可以使用显式命名的复合类型更详细地执行相同的操作;像

CREATE TYPE resultdata AS (NTested int, RawScore int, Growth int);

CREATE FUNCTION Samplefunc() RETURNS resultdata
    AS .......
或者使用表函数,如

CREATE FUNCTION Samplefunc() RETURNS TABLE(NTested int, RawScore int, Growth int)
AS
......

有关同一问题的更多信息,请参见。

hi..谢谢您的回答。它工作得很好!如何修改它以传递一些参数,这样就不必像R.test_type_id=1和test_id='201403MAME04'那样定义它;还有吗?@crozzfire:我在我的答案中添加了另一个答案。嗨,你能帮我吗??嗨,谢谢你的回答。它工作得很好!如何修改它以传递一些参数,这样就不必像R.test_type_id=1和test_id='201403MAME04'那样定义它;还有吗?@crozzfire:我在我的答案中添加了另一个答案。嗨,你能帮我吗??