如何从带参数的函数创建视图?
How to create a VIEW from function with parameters?
我在 Postgre 中有这个功能SQL:
CREATE OR REPLACE FUNCTION func1(a integer, b timestamp, c integer[])
RETURNS SETOF typ_new AS
$BODY$
declare
begin
CREATE OR REPLACE VIEW newView as (select * from func2(,,));
end;
$BODY$
LANGUAGE plpgsql VOLATILE
func2
也 returns SETOF typ_new
所以它们是兼容的。
当 运行 时,我得到一个错误:ERROR: there is no parameter
如果我将 </code> 更改为参数名称 <code>a
然后错误更改为
ERROR: column "a" does not exist
我也试过动态SQL:
sqlstr ='CREATE OR REPLACE VIEW newView (columns... ) as
(select * from func2('||||','||||','||||'))';
execute sqlstr;
但它不起作用,因为 </code> 是 <code>integer[]
并且 ||
不能使用数组。
我该如何解决这个问题?
我不确定。但是如果你只想将数组传递给动态 sql 字符串,你可以模拟它吗?..
do
$$
declare
a integer[];
begin
a := array[11,22,33,22,11];
raise info '%','you can mock up array in like this: array['||array_to_string(a,',')||']';
end;
$$
;
我的情况是这样的:
sqlstr ='CREATE OR REPLACE VIEW newView (columns... ) as
(select * from func2('||||','||||',array['||array_to_string(,',')||']))';
execute sqlstr;
CREATE OR REPLACE FUNCTION func1(a integer, b timestamp, c integer[]) RETURNS void AS
$BODY$
BEGIN
EXECUTE 'CREATE OR REPLACE VIEW newView AS ' ||
'SELECT * FROM func2(' || || ', ' || || ', ' || array_to_string(, ',') || ')';
RETURN;
END;
$BODY$ LANGUAGE plpgsql STRICT;
请注意,此函数 returns void
,而不是 SETOF typ_new
,因为您正在创建视图,而不是从视图返回数据。
由于 func2()
returns typ_new
您不必显式声明视图的列,它们将从 SELECT
语句中获取:元素typ_new
类型。
我在 Postgre 中有这个功能SQL:
CREATE OR REPLACE FUNCTION func1(a integer, b timestamp, c integer[])
RETURNS SETOF typ_new AS
$BODY$
declare
begin
CREATE OR REPLACE VIEW newView as (select * from func2(,,));
end;
$BODY$
LANGUAGE plpgsql VOLATILE
func2
也 returns SETOF typ_new
所以它们是兼容的。
当 运行 时,我得到一个错误:ERROR: there is no parameter
如果我将 </code> 更改为参数名称 <code>a
然后错误更改为
ERROR: column "a" does not exist
我也试过动态SQL:
sqlstr ='CREATE OR REPLACE VIEW newView (columns... ) as
(select * from func2('||||','||||','||||'))';
execute sqlstr;
但它不起作用,因为 </code> 是 <code>integer[]
并且 ||
不能使用数组。
我该如何解决这个问题?
我不确定。但是如果你只想将数组传递给动态 sql 字符串,你可以模拟它吗?..
do
$$
declare
a integer[];
begin
a := array[11,22,33,22,11];
raise info '%','you can mock up array in like this: array['||array_to_string(a,',')||']';
end;
$$
;
我的情况是这样的:
sqlstr ='CREATE OR REPLACE VIEW newView (columns... ) as
(select * from func2('||||','||||',array['||array_to_string(,',')||']))';
execute sqlstr;
CREATE OR REPLACE FUNCTION func1(a integer, b timestamp, c integer[]) RETURNS void AS
$BODY$
BEGIN
EXECUTE 'CREATE OR REPLACE VIEW newView AS ' ||
'SELECT * FROM func2(' || || ', ' || || ', ' || array_to_string(, ',') || ')';
RETURN;
END;
$BODY$ LANGUAGE plpgsql STRICT;
请注意,此函数 returns void
,而不是 SETOF typ_new
,因为您正在创建视图,而不是从视图返回数据。
由于 func2()
returns typ_new
您不必显式声明视图的列,它们将从 SELECT
语句中获取:元素typ_new
类型。