Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.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
在Postgresql过程中创建并运行查询_Sql_Postgresql_Stored Procedures - Fatal编程技术网

在Postgresql过程中创建并运行查询

在Postgresql过程中创建并运行查询,sql,postgresql,stored-procedures,Sql,Postgresql,Stored Procedures,我想创建一个postgresql过程,创建一个查询并运行它。我在论坛上尝试和研究。但是我不能解决我的问题。过程应该获取用户输入作为参数,并在查询中使用它。我的代码是这样的: create or replace function myProcedure(form_id int) returns form_field as $$ ( *****I have to delete the code because of privacy. This part does not effect the pr

我想创建一个postgresql过程,创建一个查询并运行它。我在论坛上尝试和研究。但是我不能解决我的问题。过程应该获取用户输入作为参数,并在查询中使用它。我的代码是这样的:


create or replace function myProcedure(form_id int)
returns form_field as $$
(
*****I have to delete the code because of privacy. This part does not effect the problem.******
)
$$
language sql




call myProcedure(2);

这就是错误:

SQL Error [42P13]: ERROR: return type mismatch in function declared to return form_field
  Detail: Final statement returns text instead of integer at column 1.
  Where: SQL function "myProcedure"
编辑:表单字段是一个表。 create语句:

CREATE TABLE public.form_field (
    col1 int4 NOT NULL,
    id varchar(255) NULL,
    col2 bool NOT NULL,
    col3 jsonb NULL,
    "col4" varchar(255) NULL,
    col5 varchar(255) NULL,
    col6 int4 NULL,
    CONSTRAINT form_field_pkey PRIMARY KEY (field_id)
);

首先,我创建了一个表格表单字段(为了避免所有错误,我只需要两个字段):

然后,我在您创建的函数中分析了您的查询,并意识到您正在选择一个字符字段,而表格表单_字段有两个字段。因此,我又创建了一个表:

create table return_test(return_col varchar(4000));
编辑了您的函数,而不是过程:

create or replace function myProcedure(form_id int)
returns setof return_test  as $$
(
SELECT distinct 'create or replace view v_form'||form_entity_id||' as'||
' with t1 as ( SELECT *, form_values as jj FROM  form_instance where form_entity_id='||form_entity_id || ')'
FROM form_field  where form_entity_id=form_id
union all
select ' Select '
union all
SELECT E' jj->0->>\'' || id || E'\'' || ' as ' || replace(replace(id,'/',''), '-','_') ||','
FROM form_field  where form_entity_id=form_id
union all
SELECT  column_name ||',' 
FROM information_schema.columns 
WHERE table_name='instance' and column_name!='values'
and ordinal_position!=(SELECT max(ordinal_position) FROM information_schema.columns where table_name='instance' ) 
union all
SELECT  column_name 
FROM information_schema.columns 
WHERE table_name='instance' --and column_name='values'
and ordinal_position=(SELECT max(ordinal_position) FROM information_schema.columns where table_name='instance' ) 
union all
select ' from t1 '
)
$$
language sql
然后调用您的函数:

select * from myProcedure(2);
所以现在,你有了一些有用的东西。。。你可以编辑你的问题,也许这会帮助别人解决你原来的问题

另外,请在问题的末尾写下,当您向函数发送值2时,您希望函数执行什么

下面是一个演示:


你能和我们分享你的类型吗:form_Field请与我们分享这个表格的创建状态。谢谢我看到你试图编辑答案。编辑信息在文章的编辑历史记录中始终可用,因此即使您的编辑被接受,任何人都可以看到它。
select * from myProcedure(2);