Lists: | pgsql-general |
---|
From: | Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com> |
---|---|
To: | pgsql-general General <pgsql-general(at)postgresql(dot)org> |
Subject: | Returning Rows in Procedure |
Date: | 2011-05-24 08:08:39 |
Message-ID: | 4DDB6787.3060807@orkash.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Dear all,
I need to return the rows of a table which was also created in that
procedure.
I know it is very easy when the table is existed before and we can
specify like this to return
create function a(integer) returns setof exist_table as $$
But it gives error when the table is also created in the procedure like
below :
create function a(integer) returns setof record as $$
declare
a text;
begin
execute 'insert into a values('asdd');
execute 'insert into a values('affffsdd');
execute 'insert into a values('affsdd');
execute 'insert into a values('ashjgdd');
execute 'insert into a values('asfjfgddd');
---Now i want to return the rows of a
DECLARE
r a%ROWTYPE;
BEGIN
FOR r in SELECT * FROM a
LOOP
RETURN NEXT r;
END LOOP;
RETURN;
END;
END;
$$ LANGUAGE 'plpgsql' ;
ERROR: relation "user_news_tmp2" does not exist
CONTEXT: compilation of PL/pgSQL function "create_user_report2" near
line 22
How to achieve this ?
Thanks & best Regards,
Adarsh
From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com> |
Cc: | pgsql-general General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Returning Rows in Procedure |
Date: | 2011-05-24 08:12:26 |
Message-ID: | BANLkTimM_vdS1Z8qR82uEBT=EUxPEYu1ew@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Hello
you have to use a dynamic sql
look on statement
FOR r IN EXECUTE
or RETURN QUERY EXECUTE
Regards
Pavel Stehule
2011/5/24 Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com>:
> Dear all,
>
> I need to return the rows of a table which was also created in that
> procedure.
>
> I know it is very easy when the table is existed before and we can specify
> like this to return
>
> create function a(integer) returns setof exist_table as $$
>
> But it gives error when the table is also created in the procedure like
> below :
>
> create function a(integer) returns setof record as $$
> declare
> a text;
> begin
> execute 'insert into a values('asdd');
> execute 'insert into a values('affffsdd');
> execute 'insert into a values('affsdd');
> execute 'insert into a values('ashjgdd');
> execute 'insert into a values('asfjfgddd');
>
> ---Now i want to return the rows of a
> DECLARE
> r a%ROWTYPE;
> BEGIN
> FOR r in SELECT * FROM a
> LOOP
> RETURN NEXT r;
> END LOOP;
> RETURN;
> END;
> END;
> $$ LANGUAGE 'plpgsql' ;
>
> ERROR: relation "user_news_tmp2" does not exist
> CONTEXT: compilation of PL/pgSQL function "create_user_report2" near line
> 22
>
> How to achieve this ?
>
> Thanks & best Regards,
> Adarsh
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>
From: | Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com> |
---|---|
To: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
Cc: | pgsql-general General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Returning Rows in Procedure |
Date: | 2011-05-24 08:16:45 |
Message-ID: | 4DDB696D.4050707@orkash.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Pavel Stehule wrote:
> Hello
>
> you have to use a dynamic sql
>
> look on statement
>
> FOR r IN EXECUTE
> or RETURN QUERY EXECUTE
>
Can u explain in the example, I find it difficult to understand .
I think we have to specify return type while creating procedures.
Thanks
> Regards
>
> Pavel Stehule
>
> 2011/5/24 Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com>:
>
>> Dear all,
>>
>> I need to return the rows of a table which was also created in that
>> procedure.
>>
>> I know it is very easy when the table is existed before and we can specify
>> like this to return
>>
>> create function a(integer) returns setof exist_table as $$
>>
>> But it gives error when the table is also created in the procedure like
>> below :
>>
>> create function a(integer) returns setof record as $$
>> declare
>> a text;
>> begin
>> execute 'insert into a values('asdd');
>> execute 'insert into a values('affffsdd');
>> execute 'insert into a values('affsdd');
>> execute 'insert into a values('ashjgdd');
>> execute 'insert into a values('asfjfgddd');
>>
>> ---Now i want to return the rows of a
>> DECLARE
>> r a%ROWTYPE;
>> BEGIN
>> FOR r in SELECT * FROM a
>> LOOP
>> RETURN NEXT r;
>> END LOOP;
>> RETURN;
>> END;
>> END;
>> $$ LANGUAGE 'plpgsql' ;
>>
>> ERROR: relation "user_news_tmp2" does not exist
>> CONTEXT: compilation of PL/pgSQL function "create_user_report2" near line
>> 22
>>
>> How to achieve this ?
>>
>> Thanks & best Regards,
>> Adarsh
>>
>> --
>> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
>> To make changes to your subscription:
>> http://www.postgresql.org/mailpref/pgsql-general
>>
>>
From: | Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com> |
---|---|
To: | Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com> |
Cc: | pgsql-general General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Returning Rows in Procedure |
Date: | 2011-05-24 08:24:40 |
Message-ID: | BANLkTimh5=bNHdw_uL=u0OFt=-CRFbHqWA@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Hello
2011/5/24 Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com>:
> Pavel Stehule wrote:
>>
>> Hello
>>
>> you have to use a dynamic sql
>>
>> look on statement
>>
>> FOR r IN EXECUTE
>> or RETURN QUERY EXECUTE
>>
>
> Can u explain in the example, I find it difficult to understand .
>
> I think we have to specify return type while creating procedures.
>
a) is not good idea to write too general functions
b) when function returns setof record, you have to describe return type in query
create or replace function foo(c int)
returns setof record as $$
begin
return query execute 'SELECT ' || repeat(' i,', c - 1) || 'i FROM
generate_series(1,3) g(i)';
end
$$ language plpgsql;
postgres=# select * from foo(2) x(a int,b int);
a │ b
───┼───
1 │ 1
2 │ 2
3 │ 3
(3 rows)
postgres=# select * from foo(3) x(a int,b int, c int);
a │ b │ c
───┼───┼───
1 │ 1 │ 1
2 │ 2 │ 2
3 │ 3 │ 3
(3 rows)
Regards
Pavel Stehule
> Thanks
>>
>> Regards
>>
>> Pavel Stehule
>>
>> 2011/5/24 Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com>:
>>
>>>
>>> Dear all,
>>>
>>> I need to return the rows of a table which was also created in that
>>> procedure.
>>>
>>> I know it is very easy when the table is existed before and we can
>>> specify
>>> like this to return
>>>
>>> create function a(integer) returns setof exist_table as $$
>>>
>>> But it gives error when the table is also created in the procedure like
>>> below :
>>>
>>> create function a(integer) returns setof record as $$
>>> declare
>>> a text;
>>> begin
>>> execute 'insert into a values('asdd');
>>> execute 'insert into a values('affffsdd');
>>> execute 'insert into a values('affsdd');
>>> execute 'insert into a values('ashjgdd');
>>> execute 'insert into a values('asfjfgddd');
>>>
>>> ---Now i want to return the rows of a
>>> DECLARE
>>> r a%ROWTYPE;
>>> BEGIN
>>> FOR r in SELECT * FROM a
>>> LOOP
>>> RETURN NEXT r;
>>> END LOOP;
>>> RETURN;
>>> END;
>>> END;
>>> $$ LANGUAGE 'plpgsql' ;
>>>
>>> ERROR: relation "user_news_tmp2" does not exist
>>> CONTEXT: compilation of PL/pgSQL function "create_user_report2" near
>>> line
>>> 22
>>>
>>> How to achieve this ?
>>>
>>> Thanks & best Regards,
>>> Adarsh
>>>
>>> --
>>> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
>>> To make changes to your subscription:
>>> http://www.postgresql.org/mailpref/pgsql-general
>>>
>>>
>
>
From: | Alban Hertroys <dalroi(at)solfertje(dot)student(dot)utwente(dot)nl> |
---|---|
To: | Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com> |
Cc: | pgsql-general General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Returning Rows in Procedure |
Date: | 2011-05-24 09:26:53 |
Message-ID: | 8A00BC77-DFC6-4908-8589-CA9E57B3D621@solfertje.student.utwente.nl |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
On 24 May 2011, at 10:08, Adarsh Sharma wrote:
> Dear all,
>
> I need to return the rows of a table which was also created in that procedure.
>
> I know it is very easy when the table is existed before and we can specify like this to return
>
> create function a(integer) returns setof exist_table as $$
>
> But it gives error when the table is also created in the procedure like below :
>
> create function a(integer) returns setof record as $$
> declare
> a text;
> begin
> execute 'insert into a values('asdd');
> execute 'insert into a values('affffsdd');
> execute 'insert into a values('affsdd');
> execute 'insert into a values('ashjgdd');
> execute 'insert into a values('asfjfgddd');
You're skating on thin ice here, you have a function named "a", a table named "a" and a variable named "a" (that doesn't get used BTW) - are you sure they're not mixed up anywhere?
I also don't quite see the need to use dynamic SQL here for insertions into the "a" table.
Is this your actual function? I don't think it is.
>
> ---Now i want to return the rows of a
> DECLARE
> r a%ROWTYPE;
> BEGIN
> FOR r in SELECT * FROM a
> LOOP
> RETURN NEXT r;
> END LOOP;
> RETURN;
> END;
I'm not sure the above would work with the dynamic SQL from before. I'd try using either all static SQL in that function or all dynamic SQL and see if that makes a difference with respect to the error you're seeing. If you can do this in all static SQL it'll probably perform better.
> END;
> $$ LANGUAGE 'plpgsql' ;
>
> ERROR: relation "user_news_tmp2" does not exist
> CONTEXT: compilation of PL/pgSQL function "create_user_report2" near line 22
Well, according to the code you provided your table is named "a", and not "user_news_tmp2". There's obviously something different between this code and your actual code, and it seems a relevant difference too. Perhaps you could give us a better example, or show us the actual code even?
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.
!DSPAM:737,4ddb79f211928090216264!
From: | David Johnston <polobo(at)yahoo(dot)com> |
---|---|
To: | Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com> |
Cc: | pgsql-general General <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Returning Rows in Procedure |
Date: | 2011-05-24 11:46:07 |
Message-ID: | D5E93928-A5D2-4B89-A367-275F525E8412@yahoo.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Lists: | pgsql-general |
Create function a(int)
Returns TABLE(col1 text)
As $$
...
$$
Language 'plpgsql'
On May 24, 2011, at 4:08, Adarsh Sharma <adarsh(dot)sharma(at)orkash(dot)com> wrote:
> Dear all,
>
> I need to return the rows of a table which was also created in that procedure.
>
> I know it is very easy when the table is existed before and we can specify like this to return
>
> create function a(integer) returns setof exist_table as $$
>
> But it gives error when the table is also created in the procedure like below :
>
> create function a(integer) returns setof record as $$
> declare
> a text;
> begin
> execute 'insert into a values('asdd');
> execute 'insert into a values('affffsdd');
> execute 'insert into a values('affsdd');
> execute 'insert into a values('ashjgdd');
> execute 'insert into a values('asfjfgddd');
>
> ---Now i want to return the rows of a
> DECLARE
> r a%ROWTYPE;
> BEGIN
> FOR r in SELECT * FROM a
> LOOP
> RETURN NEXT r;
> END LOOP;
> RETURN;
> END;
> END;
> $$ LANGUAGE 'plpgsql' ;
>
> ERROR: relation "user_news_tmp2" does not exist
> CONTEXT: compilation of PL/pgSQL function "create_user_report2" near line 22
>
> How to achieve this ?
>
> Thanks & best Regards,
> Adarsh
>
> --
> Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general