使用postgres函数创建视图 [英] Create view using postgres function

查看:162
本文介绍了使用postgres函数创建视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用postgres函数构建参数化视图:

I'm trying to build a parametrized view using a postgres function:

CREATE FUNCTION schemaB.testFunc(p INT)
RETURNS TABLE 
AS
RETURN (SELECT * FROM schemaZ.mainTable WHERE id=p)

问题始终相同:


SQL错误[42601]:错误:或附近的语法错误AS

SQL Error [42601]: ERROR: syntax error at or near "AS"

关于我可能做错什么的任何想法?

Any idea on what could I be doing wrong?

推荐答案

您需要指定返回表的列,可以使用

You need to specify the columns of the "return table", this is either done using

returns table(col_1 integer, col_2 text, ...)

在您的情况下,您要返回

In your case you are returning only rows of one table, so it's easier to use

returns setof maintable

作为手册中记录的功能功能b ody需要用单引号引起来,或使用美元报价

As documented in the manual the function body needs to be enclosed in single quotes, or using dollar quoting.

由于可以使用Postgres中的许多不同语言编写存储函数,因此您还需要指定一种语言-在这种情况下,语言sql 是合适的。

As stored functions can be written in many different languages in Postgres, you also need to specify a language - in this case language sql is suitable.

因此,所有这些都需要:

So putting all that together, you need:

CREATE FUNCTION schemaB.testFunc(p_id INT)
  RETURNS setof  schemaZ.mainTable
AS
$$
 SELECT * 
 FROM schemaZ.mainTable 
 WHERE id = p_id
$$
language sql;

A return 语句code>语言sql 函数。

A return statement is not required for language sql functions.

这篇关于使用postgres函数创建视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆