简体   繁体   English

如何使用名称创建视图,该视图可以作为postgresql中的参数传递函数?

[英]How to create view with the name which can be pass through function as a parameter in postgresql?

I am trying to create a view in postgreSQL using function. 我试图在postgreSQL中使用函数创建一个视图。 I want a view name in such a way that, which should be pass by the function as a parameter. 我想要一个视图名称,它应该作为参数传递给函数。 For example.. 例如..

--Table - 表

create table test
( rollno int,
  name text);

--Function to create a view - 创建视图的功能

create or replace function fun_view(roll int)
returns void as
$Body$
declare
       rec record;
begin
       for rec in select * from test where rollno=roll loop
           create or replace view "---Name of view should be roll---" as 
           select rollno from test;
       end loop;
$Body$
language plpgsql;

You need PL/PgSQL's dynamic SQL EXECUTE command: 您需要PL / PgSQL的动态SQL EXECUTE命令:

EXECUTE format('create or replace view %I as ...', roll::text);

See: dynamic SQL in PL/PgSQL in the docs. 请参阅:文档中的PL / PgSQL中的动态SQL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过视图将参数传递给控制器​​? - How to pass a parameter to controller through view? Oracle SQL:如何通过视图中的列将参数传递给视图中使用的 function? - Oracle SQL: how to pass parameter to a function that is used in a view through a column in a view? 如何通过传递参数名称来创建视图? (例如:CREATE VIEW @ViewName) - How can I create a view by passing the name from a parameter? (like: CREATE VIEW @ViewName ) 如何在PostgreSQL 8中重用函数名作为输出参数? - How can I re-use a function name as an out-parameter in PostgreSQL 8? 如何在 PostgreSQL 函数中多次传递相同的参数? - How do I pass the same parameter multiple times in a PostgreSQL function? 如何将参数传递给函数? - How can I pass a parameter to a function? 如何创建一个SSIS包,该包在名称中带有特定关键字的excel文件之间循环? - How can I create an SSIS package which loops through excel files with a specific keyword in their name? 将额外的参数传递给PostgreSQL聚合最终函数 - Pass extra parameter to PostgreSQL aggregate final function PostgreSQL-如何通过选择创建整数数组以作为参数传递给函数 - PostgreSQL - How to create an integer array from a select to pass as an argument to a function 是否可以更改 PostgresQL 函数中的参数名称? - Is it possible to change the name of a parameter in a PostgresQL function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM