Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL函数:“查询没有结果数据的目标”_Sql_Database_Postgresql_Psycopg2 - Fatal编程技术网

SQL函数:“查询没有结果数据的目标”

SQL函数:“查询没有结果数据的目标”,sql,database,postgresql,psycopg2,Sql,Database,Postgresql,Psycopg2,我正在研究一个SQL函数应该不会太难的功能:它需要一些参数来在表中查找特定课程,计算该课程的人数,将其与课程的最大容量进行比较,并根据需要返回1或0: drop function if exists room_for_more_students(the_class_name varchar, the_semester_code int); create function room_for_more_students(the_class_name varchar, the_semester_cod

我正在研究一个SQL函数应该不会太难的功能:它需要一些参数来在表中查找特定课程,计算该课程的人数,将其与课程的最大容量进行比较,并根据需要返回1或0:

drop function if exists room_for_more_students(the_class_name varchar, the_semester_code int);
create function room_for_more_students(the_class_name varchar, the_semester_code int) 
returns int as $BODY$
begin
    select * from class_offerings as match_table 
    where class_name = the_class_name and semester_code = the_semester_code;
    select count(student_id) from match_table as num_students_in_class;
    select avg(maximum_capacity) from match_table as num_students_allowed_in_class;
    --These will all be the same so "avg" just means "the maximum capacity for the class"
    if  num_students_in_class < num_students_allowed_in_class then return 1;
    else return 0;
    end if;
end
$BODY$
language plpgsql;

我尝试过用PERFORM来代替,但我尝试的任何组合似乎要么保留了相同的问题,要么创造了大量新的问题。我也对此做了一些研究,因为还有其他一些关于同一问题的帖子,但大多数时候答案似乎是用户没有添加具体的返回语句,我已经添加了。我完全没有主意了,如果有任何可能的输入,我将不胜感激。

对于您的情况,您必须声明一些变量并将其与查询结果一起分配。如果不将查询结果分配给nowhere,则无法运行查询

我更新您的功能如下:

drop function if exists room_for_more_students(the_class_name varchar, the_semester_code int); create function room_for_more_students(the_class_name varchar, the_semester_code int) returns int as $BODY$ DECLARE num_students_allowed_in_class numeric; num_students_in_class numeric; begin WITH match_table AS ( select * from class_offerings where class_name = the_class_name and semester_code = the_semester_code ) select count(student_id), avg(maximum_capacity) INTO num_students_in_class, num_students_allowed_in_class from match_table; if num_students_in_class 希望它符合你的要求

添加return查询,并在它之后放入您想要返回的内容 drop function if exists room_for_more_students(the_class_name varchar, the_semester_code int); create function room_for_more_students(the_class_name varchar, the_semester_code int) returns int as $BODY$ DECLARE num_students_allowed_in_class numeric; num_students_in_class numeric; begin WITH match_table AS ( select * from class_offerings where class_name = the_class_name and semester_code = the_semester_code ) select count(student_id), avg(maximum_capacity) INTO num_students_in_class, num_students_allowed_in_class from match_table; if num_students_in_class