我是Postgresql的初学者,我正在尝试用内部联接创建一个简单的函数
CREATE OR REPLACE FUNCTION GetBooks()
RETURNS TABLE( Id int, Title text, AuthorName text) as
$BODY$
BEGIN
SELECT bo.Id , bo.Title , au.Name
FROM "Book" bo INNER JOIN Author au ON (au.Id = bo.AuthorId);
END;
$BODY$
LANGUAGE plpgsql;
当我执行以下
时SELECT public.getbooks()
我收到此错误
ERROR: relation "author" does not exist
LINE 2: FROM "Book" bo INNER JOIN Author au ON (au.Id = bo.Aut...
^
QUERY: SELECT bo.Id , bo.Title , au.Name
FROM "Book" bo INNER JOIN Author au ON (au.Id = bo.AuthorId)
CONTEXT: PL/pgSQL function getbooks() line 3 at SQL statement
********** Error **********
ERROR: relation "author" does not exist
SQL state: 42P01
Context: PL/pgSQL function getbooks() line 3 at SQL statement
答案 0 :(得分:1)
CREATE OR REPLACE FUNCTION GetBooks()
RETURNS TABLE( Id int, Title text, AuthorName text) as
$BODY$
BEGIN
RETURN QUERY
SELECT bo."Id" , bo."Title" , au."Name"
FROM "Book" bo INNER JOIN "Author" au ON (au."Id" = bo."AuthorId");
END;
$BODY$
LANGUAGE plpgsql;